Skip to content
Snippets Groups Projects
Verified Commit ce641b19 authored by Nik | Klampfradler's avatar Nik | Klampfradler Committed by Tom Teichler
Browse files

Implement type conversion for additional fields

Advances #3
parent 37d205a2
No related branches found
No related tags found
1 merge request!2Resolve "Sync additional LDAP fields after login"
from django.apps import apps
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db.models import fields
from constance import config
......@@ -17,6 +18,24 @@ def syncable_fields(model):
return [field for field in model._meta.fields if field.editable and not field.auto_created]
def from_ldap(value, field):
""" Convert an LDAP value to the Python type of the target field
This conversion is prone to error because LDAP deliberately breaks
standards to cope with ASN.1 limitations.
"""
from ldapdb.models.fields import datetime_from_ldap # noqa
# Pre-convert DateTimeField and DateField due to ISO 8601 limitations in RFC 4517
if type(field) in (fields.DateField, fields.DateTimeField):
# Be opportunistic, but keep old value if conversion fails
value = datetime_from_ldap(value) or value
# Finally, use field's conversion method as default
return field.to_python(value)
def update_constance_config_fields():
""" Auto-generate sync field settings from models """
......@@ -73,9 +92,10 @@ def ldap_sync_from_user(sender, instance, created, raw, using, update_fields, **
setting_name = setting_name_from_field(Person, field)
# Try sync if constance setting for this field is non-empty
ldap_field = getattr(config, setting_name, "")
ldap_field = getattr(config, setting_name, "").lower()
if ldap_field and ldap_field in instance.ldap_user.attrs.data:
setattr(instance.person, field.name, instance.ldap_user.attrs.data[ldap_field][0])
setattr(instance.person, field.name,
from_ldap(instance.ldap_user.attrs.data[ldap_field][0], field))
instance.person.save()
......
......@@ -20,6 +20,7 @@ classifiers = [
[tool.poetry.dependencies]
python = "^3.7"
django-ldapdb = "^1.4.0"
AlekSIS = { path = "../../.." }
[build-system]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment