From e0355b13cd6271e0c331c9c6b6cefa96c0cea611 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 13 Nov 2023 14:35:38 +0100 Subject: [PATCH] Run sync when creating source via API Signed-off-by: Jens Langhammer --- authentik/sources/ldap/api/sources.py | 16 +++++++++++++++- authentik/sources/ldap/signals.py | 13 +++---------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/authentik/sources/ldap/api/sources.py b/authentik/sources/ldap/api/sources.py index d91ab025a..c9cb53e80 100644 --- a/authentik/sources/ldap/api/sources.py +++ b/authentik/sources/ldap/api/sources.py @@ -18,7 +18,7 @@ from authentik.core.api.utils import PassiveSerializer from authentik.crypto.models import CertificateKeyPair from authentik.events.monitored_tasks import TaskInfo from authentik.sources.ldap.models import LDAPSource -from authentik.sources.ldap.tasks import CACHE_KEY_STATUS, SYNC_CLASSES +from authentik.sources.ldap.tasks import CACHE_KEY_STATUS, SYNC_CLASSES, ldap_sync_single class LDAPSourceSerializer(SourceSerializer): @@ -55,6 +55,20 @@ class LDAPSourceSerializer(SourceSerializer): ) return super().validate(attrs) + def create(self, validated_data) -> LDAPSource: + # Create both creates the actual model and assigns m2m fields + instance: LDAPSource = super().create(validated_data) + if not instance.enabled: + return instance + # Don't sync sources when they don't have any property mappings. This will only happen if: + # - the user forgets to set them or + # - the source is newly created, this is the first save event + # and the mappings are created with an m2m event + if not instance.property_mappings.exists() or not instance.property_mappings_group.exists(): + return instance + ldap_sync_single.delay(instance.pk) + return instance + class Meta: model = LDAPSource fields = SourceSerializer.Meta.fields + [ diff --git a/authentik/sources/ldap/signals.py b/authentik/sources/ldap/signals.py index f95662e33..d84282407 100644 --- a/authentik/sources/ldap/signals.py +++ b/authentik/sources/ldap/signals.py @@ -14,24 +14,17 @@ from authentik.events.models import Event, EventAction from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER from authentik.sources.ldap.models import LDAPSource from authentik.sources.ldap.password import LDAPPasswordChanger -from authentik.sources.ldap.tasks import ldap_connectivity_check, ldap_sync_single +from authentik.sources.ldap.tasks import ldap_connectivity_check from authentik.stages.prompt.signals import password_validate LOGGER = get_logger() @receiver(post_save, sender=LDAPSource) -def sync_ldap_source_on_save(sender, instance: LDAPSource, **_): - """Ensure that source is synced on save (if enabled)""" +def check_ldap_source_on_save(sender, instance: LDAPSource, **_): + """Check LDAP source's connectivity on save (if enabled)""" if not instance.enabled: return - # Don't sync sources when they don't have any property mappings. This will only happen if: - # - the user forgets to set them or - # - the source is newly created, this is the first save event - # and the mappings are created with an m2m event - if not instance.property_mappings.exists() or not instance.property_mappings_group.exists(): - return - ldap_sync_single.delay(instance.pk) ldap_connectivity_check.delay(instance.pk)