From 491dcc1159df717fc5d35e555365c712b2adf5ea Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 15 Sep 2020 21:51:08 +0200 Subject: [PATCH] sources/ldap: improve default Property Mappings --- .../migrations/0006_auto_20200915_1919.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 passbook/sources/ldap/migrations/0006_auto_20200915_1919.py diff --git a/passbook/sources/ldap/migrations/0006_auto_20200915_1919.py b/passbook/sources/ldap/migrations/0006_auto_20200915_1919.py new file mode 100644 index 000000000..5fd67e0b3 --- /dev/null +++ b/passbook/sources/ldap/migrations/0006_auto_20200915_1919.py @@ -0,0 +1,48 @@ +# Generated by Django 3.1.1 on 2020-09-15 19:19 + +from django.apps.registry import Apps +from django.db import migrations + + +def create_default_property_mappings(apps: Apps, schema_editor): + LDAPPropertyMapping = apps.get_model("passbook_sources_ldap", "LDAPPropertyMapping") + db_alias = schema_editor.connection.alias + mapping = { + "name": "name", + "first_name": "givenName", + "last_name": "sn", + "email": "mail", + } + for object_field, ldap_field in mapping.items(): + expression = f"return ldap.get('{ldap_field}')" + LDAPPropertyMapping.objects.using(db_alias).get_or_create( + expression=expression, + object_field=object_field, + defaults={ + "name": f"Autogenerated LDAP Mapping: {ldap_field} -> {object_field}" + }, + ) + ad_mapping = { + "username": "sAMAccountName", + "attributes.upn": "userPrincipalName", + } + for object_field, ldap_field in ad_mapping.items(): + expression = f"return ldap.get('{ldap_field}')" + LDAPPropertyMapping.objects.using(db_alias).get_or_create( + expression=expression, + object_field=object_field, + defaults={ + "name": f"Autogenerated Active Directory Mapping: {ldap_field} -> {object_field}" + }, + ) + + +class Migration(migrations.Migration): + + dependencies = [ + ("passbook_sources_ldap", "0005_auto_20200913_1947"), + ] + + operations = [ + migrations.RunPython(create_default_property_mappings), + ]