sources/ldap: update LDAP source to use new property mappings

This commit is contained in:
Jens Langhammer 2020-02-17 17:55:48 +01:00
parent 7268afaaf9
commit e57da71dcf
5 changed files with 53 additions and 6 deletions

View File

@ -35,7 +35,7 @@ class LDAPPropertyMappingSerializer(ModelSerializer):
class Meta: class Meta:
model = LDAPPropertyMapping model = LDAPPropertyMapping
fields = ["pk", "name", "ldap_property", "object_field"] fields = ["pk", "name", "template", "object_field"]
class LDAPSourceViewSet(ModelViewSet): class LDAPSourceViewSet(ModelViewSet):

View File

@ -6,7 +6,7 @@ import ldap3.core.exceptions
from structlog import get_logger from structlog import get_logger
from passbook.core.models import Group, User 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() LOGGER = get_logger()
@ -154,7 +154,10 @@ class Connector:
) -> Dict[str, Dict[Any, Any]]: ) -> Dict[str, Dict[Any, Any]]:
properties = {"attributes": {}} properties = {"attributes": {}}
for mapping in self._source.property_mappings.all().select_subclasses(): 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: if self._source.object_uniqueness_field in attributes:
properties["attributes"]["ldap_uniq"] = attributes.get( properties["attributes"]["ldap_uniq"] = attributes.get(
self._source.object_uniqueness_field self._source.object_uniqueness_field

View File

@ -53,7 +53,7 @@ class LDAPPropertyMappingForm(forms.ModelForm):
class Meta: class Meta:
model = LDAPPropertyMapping model = LDAPPropertyMapping
fields = ["name", "ldap_property", "object_field"] fields = ["name", "object_field", "template"]
widgets = { widgets = {
"name": forms.TextInput(), "name": forms.TextInput(),
"ldap_property": forms.TextInput(), "ldap_property": forms.TextInput(),

View File

@ -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),
]

View File

@ -59,13 +59,12 @@ class LDAPSource(Source):
class LDAPPropertyMapping(PropertyMapping): class LDAPPropertyMapping(PropertyMapping):
"""Map LDAP Property to User or Group object""" """Map LDAP Property to User or Group object"""
ldap_property = models.TextField(verbose_name=_("LDAP Property"))
object_field = models.TextField() object_field = models.TextField()
form = "passbook.sources.ldap.forms.LDAPPropertyMappingForm" form = "passbook.sources.ldap.forms.LDAPPropertyMappingForm"
def __str__(self): 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: class Meta: