From bb4be944dcf61b4d2cd146e7724491035e87bf51 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sun, 13 Feb 2022 14:20:13 +0100 Subject: [PATCH] sources/ldap: use merger that only appends unique items to list closes #2211 Signed-off-by: Jens Langhammer --- authentik/lib/merge.py | 6 ++++++ authentik/sources/ldap/sync/base.py | 6 +++--- 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 authentik/lib/merge.py diff --git a/authentik/lib/merge.py b/authentik/lib/merge.py new file mode 100644 index 000000000..99a6dab66 --- /dev/null +++ b/authentik/lib/merge.py @@ -0,0 +1,6 @@ +"""merge utils""" +from deepmerge import Merger + +MERGE_LIST_UNIQUE = Merger( + [(list, ["append_unique"]), (dict, ["merge"]), (set, ["union"])], ["override"], ["override"] +) diff --git a/authentik/sources/ldap/sync/base.py b/authentik/sources/ldap/sync/base.py index e8b9cd36e..16b812a50 100644 --- a/authentik/sources/ldap/sync/base.py +++ b/authentik/sources/ldap/sync/base.py @@ -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)