authentik.sources.ldap.migrations.0005_remove_ldappropertymapping_object_field_and_more
1# Generated by Django 5.0.2 on 2024-02-29 11:21 2 3import textwrap 4 5from django.db import migrations 6 7 8def migrate_ldap_property_mappings_object_field(apps, schema_editor): 9 db_alias = schema_editor.connection.alias 10 LDAPPropertyMapping = apps.get_model("authentik_sources_ldap", "LDAPPropertyMapping") 11 for mapping in LDAPPropertyMapping.objects.using(db_alias).all(): 12 mapping.expression = f""" 13# This property mapping has been automatically changed to 14# match the new semantics of source property mappings. 15# You can simplify it if you want. 16# You should return a dictionary of fields to set on the user or the group. 17# For instance: 18# return {{ 19# "{mapping.object_field}": ldap.get("{mapping.object_field}") 20# }} 21# Note that this example has been generated and should not be used as-is. 22def get_field(): 23 {textwrap.indent(mapping.expression, prefix=' ')} 24 25from authentik.lib.utils.dict import set_path_in_dict 26 27field = "{mapping.object_field}" 28result = {{"attributes": {{}}}} 29if field.startswith("attributes."): 30 set_path_in_dict(result, field, get_field(), sep=".") 31else: 32 result[field] = get_field() 33return result 34 """ 35 mapping.save() 36 37 38def migrate_ldap_property_mappings_to_new_fields(apps, schema_editor): 39 db_alias = schema_editor.connection.alias 40 LDAPSource = apps.get_model("authentik_sources_ldap", "LDAPSource") 41 for source in LDAPSource.objects.using(db_alias).all(): 42 source.user_property_mappings.set(source.property_mappings.using(db_alias).all()) 43 source.group_property_mappings.set(source.property_mappings_group.using(db_alias).all()) 44 45 46class Migration(migrations.Migration): 47 48 dependencies = [ 49 ("authentik_sources_ldap", "0004_ldapsource_password_login_update_internal_password"), 50 ("authentik_core", "0036_source_group_property_mappings_and_more"), 51 ] 52 53 operations = [ 54 migrations.RunPython(migrate_ldap_property_mappings_object_field), 55 migrations.RunPython(migrate_ldap_property_mappings_to_new_fields), 56 migrations.RemoveField( 57 model_name="ldappropertymapping", 58 name="object_field", 59 ), 60 migrations.RemoveField( 61 model_name="ldapsource", 62 name="property_mappings_group", 63 ), 64 ]
def
migrate_ldap_property_mappings_object_field(apps, schema_editor):
9def migrate_ldap_property_mappings_object_field(apps, schema_editor): 10 db_alias = schema_editor.connection.alias 11 LDAPPropertyMapping = apps.get_model("authentik_sources_ldap", "LDAPPropertyMapping") 12 for mapping in LDAPPropertyMapping.objects.using(db_alias).all(): 13 mapping.expression = f""" 14# This property mapping has been automatically changed to 15# match the new semantics of source property mappings. 16# You can simplify it if you want. 17# You should return a dictionary of fields to set on the user or the group. 18# For instance: 19# return {{ 20# "{mapping.object_field}": ldap.get("{mapping.object_field}") 21# }} 22# Note that this example has been generated and should not be used as-is. 23def get_field(): 24 {textwrap.indent(mapping.expression, prefix=' ')} 25 26from authentik.lib.utils.dict import set_path_in_dict 27 28field = "{mapping.object_field}" 29result = {{"attributes": {{}}}} 30if field.startswith("attributes."): 31 set_path_in_dict(result, field, get_field(), sep=".") 32else: 33 result[field] = get_field() 34return result 35 """ 36 mapping.save()
def
migrate_ldap_property_mappings_to_new_fields(apps, schema_editor):
39def migrate_ldap_property_mappings_to_new_fields(apps, schema_editor): 40 db_alias = schema_editor.connection.alias 41 LDAPSource = apps.get_model("authentik_sources_ldap", "LDAPSource") 42 for source in LDAPSource.objects.using(db_alias).all(): 43 source.user_property_mappings.set(source.property_mappings.using(db_alias).all()) 44 source.group_property_mappings.set(source.property_mappings_group.using(db_alias).all())
class
Migration(django.db.migrations.migration.Migration):
47class Migration(migrations.Migration): 48 49 dependencies = [ 50 ("authentik_sources_ldap", "0004_ldapsource_password_login_update_internal_password"), 51 ("authentik_core", "0036_source_group_property_mappings_and_more"), 52 ] 53 54 operations = [ 55 migrations.RunPython(migrate_ldap_property_mappings_object_field), 56 migrations.RunPython(migrate_ldap_property_mappings_to_new_fields), 57 migrations.RemoveField( 58 model_name="ldappropertymapping", 59 name="object_field", 60 ), 61 migrations.RemoveField( 62 model_name="ldapsource", 63 name="property_mappings_group", 64 ), 65 ]
The base class for all migrations.
Migration files will import this from django.db.migrations.Migration and subclass it as a class called Migration. It will have one or more of the following attributes:
- operations: A list of Operation instances, probably from django.db.migrations.operations
- dependencies: A list of tuples of (app_path, migration_name)
- run_before: A list of tuples of (app_path, migration_name)
- replaces: A list of migration_names
Note that all migrations come out of migrations and into the Loader or Graph as instances, having been initialized with their app label and name.