2019-10-11 12:43:35 +01:00
|
|
|
"""LDAP Sync tasks"""
|
2020-09-19 14:25:17 +01:00
|
|
|
from time import time
|
|
|
|
|
|
|
|
from django.core.cache import cache
|
|
|
|
|
2019-10-11 11:53:48 +01:00
|
|
|
from passbook.root.celery import CELERY_APP
|
|
|
|
from passbook.sources.ldap.models import LDAPSource
|
2020-09-21 20:35:50 +01:00
|
|
|
from passbook.sources.ldap.sync import LDAPSynchronizer
|
2019-10-11 11:53:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
@CELERY_APP.task()
|
|
|
|
def sync():
|
|
|
|
"""Sync all sources"""
|
|
|
|
for source in LDAPSource.objects.filter(enabled=True):
|
2020-09-14 22:35:01 +01:00
|
|
|
sync_single.delay(source.pk)
|
|
|
|
|
|
|
|
|
|
|
|
@CELERY_APP.task()
|
|
|
|
def sync_single(source_pk):
|
|
|
|
"""Sync a single source"""
|
2020-09-19 14:25:17 +01:00
|
|
|
source: LDAPSource = LDAPSource.objects.get(pk=source_pk)
|
2020-09-21 20:35:50 +01:00
|
|
|
syncer = LDAPSynchronizer(source)
|
|
|
|
syncer.sync_users()
|
|
|
|
syncer.sync_groups()
|
|
|
|
syncer.sync_membership()
|
2020-09-19 14:25:17 +01:00
|
|
|
cache_key = source.state_cache_prefix("last_sync")
|
|
|
|
cache.set(cache_key, time(), timeout=60 * 60)
|