From e57da71dcf5dcaae2c100300cd2664bbcb2f76b4 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 17 Feb 2020 17:55:48 +0100 Subject: [PATCH] sources/ldap: update LDAP source to use new property mappings --- passbook/sources/ldap/api.py | 2 +- passbook/sources/ldap/connector.py | 7 ++- passbook/sources/ldap/forms.py | 2 +- ...emove_ldappropertymapping_ldap_property.py | 45 +++++++++++++++++++ passbook/sources/ldap/models.py | 3 +- 5 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 passbook/sources/ldap/migrations/0007_remove_ldappropertymapping_ldap_property.py diff --git a/passbook/sources/ldap/api.py b/passbook/sources/ldap/api.py index 50f65f098..a09ab3ca9 100644 --- a/passbook/sources/ldap/api.py +++ b/passbook/sources/ldap/api.py @@ -35,7 +35,7 @@ class LDAPPropertyMappingSerializer(ModelSerializer): class Meta: model = LDAPPropertyMapping - fields = ["pk", "name", "ldap_property", "object_field"] + fields = ["pk", "name", "template", "object_field"] class LDAPSourceViewSet(ModelViewSet): diff --git a/passbook/sources/ldap/connector.py b/passbook/sources/ldap/connector.py index 40904122d..9dcd9748e 100644 --- a/passbook/sources/ldap/connector.py +++ b/passbook/sources/ldap/connector.py @@ -6,7 +6,7 @@ import ldap3.core.exceptions from structlog import get_logger from passbook.core.models import Group, User -from passbook.sources.ldap.models import LDAPSource +from passbook.sources.ldap.models import LDAPSource, LDAPPropertyMapping LOGGER = get_logger() @@ -154,7 +154,10 @@ class Connector: ) -> Dict[str, Dict[Any, Any]]: properties = {"attributes": {}} for mapping in self._source.property_mappings.all().select_subclasses(): - properties[mapping.object_field] = attributes.get(mapping.ldap_property, "") + mapping: LDAPPropertyMapping + properties[mapping.object_field] = mapping.render( + user=None, request=None, ldap=attributes + ) if self._source.object_uniqueness_field in attributes: properties["attributes"]["ldap_uniq"] = attributes.get( self._source.object_uniqueness_field diff --git a/passbook/sources/ldap/forms.py b/passbook/sources/ldap/forms.py index e940b132c..d7d463757 100644 --- a/passbook/sources/ldap/forms.py +++ b/passbook/sources/ldap/forms.py @@ -53,7 +53,7 @@ class LDAPPropertyMappingForm(forms.ModelForm): class Meta: model = LDAPPropertyMapping - fields = ["name", "ldap_property", "object_field"] + fields = ["name", "object_field", "template"] widgets = { "name": forms.TextInput(), "ldap_property": forms.TextInput(), diff --git a/passbook/sources/ldap/migrations/0007_remove_ldappropertymapping_ldap_property.py b/passbook/sources/ldap/migrations/0007_remove_ldappropertymapping_ldap_property.py new file mode 100644 index 000000000..c33da5f59 --- /dev/null +++ b/passbook/sources/ldap/migrations/0007_remove_ldappropertymapping_ldap_property.py @@ -0,0 +1,45 @@ +# Generated by Django 3.0.3 on 2020-02-17 16:19 + +from django.apps.registry import Apps +from django.db import migrations + + +def cleanup_old_autogenerated(apps, schema_editor): + LDAPPropertyMapping = apps.get_model("passbook_sources_ldap", "LDAPPropertyMapping") + db_alias = schema_editor.connection.alias + LDAPPropertyMapping.objects.using(db_alias).filter( + name__startswith="Autogenerated" + ).delete() + + +def create_default_ad_property_mappings(apps: Apps, schema_editor): + LDAPPropertyMapping = apps.get_model("passbook_sources_ldap", "LDAPPropertyMapping") + mapping = { + "name": "{{ ldap.name }}", + "first_name": "{{ ldap.givenName }}", + "last_name": "{{ ldap.sn }}", + "username": "{{ ldap.sAMAccountName }}", + "email": "{{ ldap.mail }}", + } + db_alias = schema_editor.connection.alias + for object_field, template in mapping.items(): + LDAPPropertyMapping.objects.using(db_alias).get_or_create( + template=template, + object_field=object_field, + defaults={ + "name": f"Autogenerated LDAP Mapping: {template} -> {object_field}" + }, + ) + + +class Migration(migrations.Migration): + + dependencies = [ + ("passbook_sources_ldap", "0006_auto_20200216_1116"), + ] + + operations = [ + migrations.RunPython(cleanup_old_autogenerated), + migrations.RemoveField(model_name="ldappropertymapping", name="ldap_property",), + migrations.RunPython(create_default_ad_property_mappings), + ] diff --git a/passbook/sources/ldap/models.py b/passbook/sources/ldap/models.py index 045864dc3..063dce319 100644 --- a/passbook/sources/ldap/models.py +++ b/passbook/sources/ldap/models.py @@ -59,13 +59,12 @@ class LDAPSource(Source): class LDAPPropertyMapping(PropertyMapping): """Map LDAP Property to User or Group object""" - ldap_property = models.TextField(verbose_name=_("LDAP Property")) object_field = models.TextField() form = "passbook.sources.ldap.forms.LDAPPropertyMappingForm" def __str__(self): - return f"LDAP Property Mapping {self.ldap_property} -> {self.object_field}" + return f"LDAP Property Mapping {self.template} -> {self.object_field}" class Meta: