From e7472de4bff1e908fa66b49edcce63a0a93cc044 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 14 Sep 2020 23:35:01 +0200 Subject: [PATCH] sources/ldap: sync source on save --- passbook/admin/views/overview.py | 2 +- passbook/sources/ldap/signals.py | 14 ++++++++++++++ passbook/sources/ldap/tasks.py | 15 +++++++++++---- 3 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 passbook/sources/ldap/signals.py diff --git a/passbook/admin/views/overview.py b/passbook/admin/views/overview.py index 92439ff1a..e1331d34c 100644 --- a/passbook/admin/views/overview.py +++ b/passbook/admin/views/overview.py @@ -45,7 +45,7 @@ class AdministrationOverviewView(AdminRequiredMixin, TemplateView): def get_context_data(self, **kwargs): kwargs["application_count"] = len(Application.objects.all()) kwargs["policy_count"] = len(Policy.objects.all()) - kwargs["user_count"] = len(User.objects.all()) - 1 # Remove anonymous user + kwargs["user_count"] = len(User.objects.all()) - 1 # Remove anonymous user kwargs["provider_count"] = len(Provider.objects.all()) kwargs["source_count"] = len(Source.objects.all()) kwargs["stage_count"] = len(Stage.objects.all()) diff --git a/passbook/sources/ldap/signals.py b/passbook/sources/ldap/signals.py new file mode 100644 index 000000000..7e9b4cb69 --- /dev/null +++ b/passbook/sources/ldap/signals.py @@ -0,0 +1,14 @@ +"""passbook ldap source signals""" +from django.db.models.signals import post_save +from django.dispatch import receiver + +from passbook.sources.ldap.models import LDAPSource +from passbook.sources.ldap.tasks import sync_single + + +@receiver(post_save, sender=LDAPSource) +# pylint: disable=unused-argument +def sync_ldap_source_on_save(sender, instance: LDAPSource, **_): + """Ensure that source is synced on save (if enabled)""" + if instance.enabled: + sync_single.delay(instance.pk) diff --git a/passbook/sources/ldap/tasks.py b/passbook/sources/ldap/tasks.py index 71f263073..a200258cd 100644 --- a/passbook/sources/ldap/tasks.py +++ b/passbook/sources/ldap/tasks.py @@ -8,7 +8,14 @@ from passbook.sources.ldap.models import LDAPSource def sync(): """Sync all sources""" for source in LDAPSource.objects.filter(enabled=True): - connector = Connector(source) - connector.sync_users() - connector.sync_groups() - connector.sync_membership() + sync_single.delay(source.pk) + + +@CELERY_APP.task() +def sync_single(source_pk): + """Sync a single source""" + source = LDAPSource.objects.get(pk=source_pk) + connector = Connector(source) + connector.sync_users() + connector.sync_groups() + connector.sync_membership()