This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/passbook/flows/migrations/0002_default_flows.py

71 lines
2.5 KiB
Python
Raw Normal View History

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
from passbook.stages.identification.models import Templates, UserFields
2020-05-08 14:43:22 +00:00
def create_default_flow(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
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")
LoginStage = apps.get_model(
"passbook_stages_login", "LoginStage"
)
IdentificationStage = apps.get_model(
"passbook_stages_identification", "IdentificationStage"
)
2020-05-08 14:43:22 +00:00
db_alias = schema_editor.connection.alias
if Flow.objects.using(db_alias).all().exists():
# Only create default flow when none exist
return
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"],
)
if not LoginStage.objects.using(db_alias).exists():
LoginStage.objects.using(db_alias).create(name="authentication")
ident_stage = IdentificationStage.objects.using(db_alias).first()
2020-05-08 17:46:39 +00:00
pw_stage = PasswordStage.objects.using(db_alias).first()
login_stage = LoginStage.objects.using(db_alias).first()
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(
flow=flow, stage=ident_stage, order=0,
)
FlowStageBinding.objects.using(db_alias).create(
flow=flow, stage=pw_stage, order=1,
2020-05-08 14:43:22 +00:00
)
FlowStageBinding.objects.using(db_alias).create(
flow=flow, stage=login_stage, order=2,
)
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"),
("passbook_stages_login", "0001_initial"),
2020-05-08 17:46:39 +00:00
("passbook_stages_password", "0001_initial"),
("passbook_stages_identification", "0001_initial"),
2020-05-08 14:43:22 +00:00
]
operations = [migrations.RunPython(create_default_flow)]