Run sync when creating source via API
Signed-off-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
parent
d092093e94
commit
e0355b13cd
|
@ -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 + [
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
||||
|
|
Reference in New Issue