sources/ldap: use merger that only appends unique items to list

closes #2211

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2022-02-13 14:20:13 +01:00
parent 21efee8f44
commit bb4be944dc
2 changed files with 9 additions and 3 deletions

6
authentik/lib/merge.py Normal file
View File

@ -0,0 +1,6 @@
"""merge utils"""
from deepmerge import Merger
MERGE_LIST_UNIQUE = Merger(
[(list, ["append_unique"]), (dict, ["merge"]), (set, ["union"])], ["override"], ["override"]
)

View File

@ -1,13 +1,13 @@
"""Sync LDAP Users and groups into authentik"""
from typing import Any
from deepmerge import always_merger
from django.db.models.base import Model
from django.db.models.query import QuerySet
from structlog.stdlib import BoundLogger, get_logger
from authentik.core.exceptions import PropertyMappingExpressionException
from authentik.events.models import Event, EventAction
from authentik.lib.merge import MERGE_LIST_UNIQUE
from authentik.sources.ldap.auth import LDAP_DISTINGUISHED_NAME
from authentik.sources.ldap.models import LDAPPropertyMapping, LDAPSource
@ -123,8 +123,8 @@ class BaseLDAPSynchronizer:
continue
setattr(instance, key, value)
final_atttributes = {}
always_merger.merge(final_atttributes, instance.attributes)
always_merger.merge(final_atttributes, data.get("attributes", {}))
MERGE_LIST_UNIQUE.merge(final_atttributes, instance.attributes)
MERGE_LIST_UNIQUE.merge(final_atttributes, data.get("attributes", {}))
instance.attributes = final_atttributes
instance.save()
return (instance, False)