2020-05-08 14:43:22 +00:00
|
|
|
# Generated by Django 3.0.3 on 2020-05-08 14:30
|
|
|
|
|
|
|
|
from django.apps.registry import Apps
|
|
|
|
from django.db import migrations
|
|
|
|
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
|
|
|
|
|
|
|
|
from passbook.flows.models import FlowDesignation
|
2020-05-09 19:31:29 +00:00
|
|
|
from passbook.stages.identification.models import Templates, UserFields
|
2020-05-08 14:43:22 +00:00
|
|
|
|
|
|
|
|
2020-05-10 23:12:57 +00:00
|
|
|
def create_default_authentication_flow(
|
|
|
|
apps: Apps, schema_editor: BaseDatabaseSchemaEditor
|
|
|
|
):
|
2020-05-08 14:43:22 +00:00
|
|
|
Flow = apps.get_model("passbook_flows", "Flow")
|
2020-05-08 17:46:39 +00:00
|
|
|
FlowStageBinding = apps.get_model("passbook_flows", "FlowStageBinding")
|
|
|
|
PasswordStage = apps.get_model("passbook_stages_password", "PasswordStage")
|
2020-05-10 14:20:44 +00:00
|
|
|
UserLoginStage = apps.get_model("passbook_stages_user_login", "UserLoginStage")
|
2020-05-09 19:31:29 +00:00
|
|
|
IdentificationStage = apps.get_model(
|
|
|
|
"passbook_stages_identification", "IdentificationStage"
|
|
|
|
)
|
2020-05-08 14:43:22 +00:00
|
|
|
db_alias = schema_editor.connection.alias
|
|
|
|
|
2020-05-10 23:12:57 +00:00
|
|
|
if (
|
|
|
|
Flow.objects.using(db_alias)
|
|
|
|
.filter(designation=FlowDesignation.AUTHENTICATION)
|
|
|
|
.exists()
|
|
|
|
):
|
2020-05-08 14:43:22 +00:00
|
|
|
# Only create default flow when none exist
|
|
|
|
return
|
|
|
|
|
2020-05-09 19:31:29 +00:00
|
|
|
if not IdentificationStage.objects.using(db_alias).exists():
|
|
|
|
IdentificationStage.objects.using(db_alias).create(
|
|
|
|
name="identification",
|
|
|
|
user_fields=[UserFields.E_MAIL],
|
|
|
|
template=Templates.DEFAULT_LOGIN,
|
|
|
|
)
|
|
|
|
|
2020-05-08 17:46:39 +00:00
|
|
|
if not PasswordStage.objects.using(db_alias).exists():
|
|
|
|
PasswordStage.objects.using(db_alias).create(
|
|
|
|
name="password", backends=["django.contrib.auth.backends.ModelBackend"],
|
|
|
|
)
|
|
|
|
|
2020-05-10 14:20:44 +00:00
|
|
|
if not UserLoginStage.objects.using(db_alias).exists():
|
|
|
|
UserLoginStage.objects.using(db_alias).create(name="authentication")
|
2020-05-09 21:19:36 +00:00
|
|
|
|
2020-05-08 14:43:22 +00:00
|
|
|
flow = Flow.objects.using(db_alias).create(
|
|
|
|
name="default-authentication-flow",
|
|
|
|
slug="default-authentication-flow",
|
|
|
|
designation=FlowDesignation.AUTHENTICATION,
|
|
|
|
)
|
2020-05-08 17:46:39 +00:00
|
|
|
FlowStageBinding.objects.using(db_alias).create(
|
2020-05-10 21:38:15 +00:00
|
|
|
flow=flow, stage=IdentificationStage.objects.using(db_alias).first(), order=0,
|
2020-05-09 19:31:29 +00:00
|
|
|
)
|
|
|
|
FlowStageBinding.objects.using(db_alias).create(
|
2020-05-10 21:38:15 +00:00
|
|
|
flow=flow, stage=PasswordStage.objects.using(db_alias).first(), order=1,
|
2020-05-08 14:43:22 +00:00
|
|
|
)
|
2020-05-09 21:19:36 +00:00
|
|
|
FlowStageBinding.objects.using(db_alias).create(
|
2020-05-10 21:38:15 +00:00
|
|
|
flow=flow, stage=UserLoginStage.objects.using(db_alias).first(), order=2,
|
2020-05-09 21:19:36 +00:00
|
|
|
)
|
2020-05-08 14:43:22 +00:00
|
|
|
|
|
|
|
|
2020-05-10 23:12:57 +00:00
|
|
|
def create_default_invalidation_flow(
|
|
|
|
apps: Apps, schema_editor: BaseDatabaseSchemaEditor
|
|
|
|
):
|
|
|
|
Flow = apps.get_model("passbook_flows", "Flow")
|
|
|
|
FlowStageBinding = apps.get_model("passbook_flows", "FlowStageBinding")
|
|
|
|
UserLogoutStage = apps.get_model("passbook_stages_user_logout", "UserLogoutStage")
|
|
|
|
db_alias = schema_editor.connection.alias
|
|
|
|
|
|
|
|
if (
|
|
|
|
Flow.objects.using(db_alias)
|
|
|
|
.filter(designation=FlowDesignation.INVALIDATION)
|
|
|
|
.exists()
|
|
|
|
):
|
|
|
|
# Only create default flow when none exist
|
|
|
|
return
|
|
|
|
|
|
|
|
if not UserLogoutStage.objects.using(db_alias).exists():
|
2020-05-16 17:55:59 +00:00
|
|
|
UserLogoutStage.objects.using(db_alias).create(name="logout")
|
2020-05-10 23:12:57 +00:00
|
|
|
|
|
|
|
flow = Flow.objects.using(db_alias).create(
|
|
|
|
name="default-invalidation-flow",
|
|
|
|
slug="default-invalidation-flow",
|
|
|
|
designation=FlowDesignation.INVALIDATION,
|
|
|
|
)
|
|
|
|
FlowStageBinding.objects.using(db_alias).create(
|
|
|
|
flow=flow, stage=UserLogoutStage.objects.using(db_alias).first(), order=0,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-05-08 14:43:22 +00:00
|
|
|
class Migration(migrations.Migration):
|
|
|
|
|
|
|
|
dependencies = [
|
2020-05-08 17:46:39 +00:00
|
|
|
("passbook_flows", "0001_initial"),
|
2020-05-10 14:20:44 +00:00
|
|
|
("passbook_stages_user_login", "0001_initial"),
|
2020-05-10 23:12:57 +00:00
|
|
|
("passbook_stages_user_logout", "0001_initial"),
|
2020-05-08 17:46:39 +00:00
|
|
|
("passbook_stages_password", "0001_initial"),
|
2020-05-09 19:31:29 +00:00
|
|
|
("passbook_stages_identification", "0001_initial"),
|
2020-05-08 14:43:22 +00:00
|
|
|
]
|
|
|
|
|
2020-05-10 23:12:57 +00:00
|
|
|
operations = [
|
|
|
|
migrations.RunPython(create_default_authentication_flow),
|
|
|
|
migrations.RunPython(create_default_invalidation_flow),
|
|
|
|
]
|