-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
25 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
# SPDX-FileCopyrightText: 2022 Tim Hawes <[email protected]> | ||
# SPDX-FileCopyrightText: 2022-2024 Tim Hawes <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
import logging | ||
|
||
from django.conf import settings | ||
from django.contrib.auth import get_user_model | ||
from django.contrib.auth.models import Group | ||
|
@@ -13,69 +15,72 @@ | |
from .utils import sync_group, sync_user | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@receiver(post_save, sender=get_user_model()) | ||
def sync_ldap_user(sender, instance, update_fields=[], **kwargs): | ||
if update_fields and list(update_fields) == ["last_login"]: | ||
# skip update if last_login was the only change | ||
return | ||
try: | ||
print(f"ldapsync user {instance}") | ||
logger.info(f"ldapsync user {instance}") | ||
sync_user(instance) | ||
except Exception as e: | ||
print(f"Exception in ldapsync of {instance}: {e}") | ||
except Exception: | ||
logger.exception(f"Exception in ldapsync of {instance}") | ||
|
||
|
||
@receiver(post_save, sender=PosixUser) | ||
def sync_ldap_posix_user(sender, instance, **kwargs): | ||
try: | ||
print(f"ldapsync user {instance.user} (for PosixUser)") | ||
logger.info(f"ldapsync user {instance.user} (for PosixUser)") | ||
sync_user(instance.user) | ||
except Exception as e: | ||
print(f"Exception in ldapsync of {instance}: {e}") | ||
except Exception: | ||
logger.exception(f"Exception in ldapsync of {instance}") | ||
|
||
|
||
@receiver(post_save, sender=SSHKey) | ||
def sync_ldap_posix_sshkey(sender, instance, **kwargs): | ||
try: | ||
print(f"ldapsync user {instance.user} (for SSHKey)") | ||
logger.info(f"ldapsync user {instance.user} (for SSHKey)") | ||
sync_user(instance.user) | ||
except Exception as e: | ||
print(f"Exception in ldapsync of {instance}: {e}") | ||
except Exception: | ||
logger.exception(f"Exception in ldapsync of {instance}") | ||
|
||
|
||
@receiver(post_save, sender=Group) | ||
def sync_ldap_group(sender, instance, **kwargs): | ||
try: | ||
print(f"ldapsync group {instance}") | ||
logger.info(f"ldapsync group {instance}") | ||
sync_group(instance) | ||
except Exception as e: | ||
print(f"Exception in ldapsync of {instance}: {e}") | ||
except Exception: | ||
logger.exception(f"Exception in ldapsync of {instance}") | ||
|
||
|
||
@receiver(post_save, sender=PosixGroup) | ||
def sync_ldap_posix_group(sender, instance, **kwargs): | ||
try: | ||
print(f"ldapsync user {instance.group} (for PosixGroup)") | ||
logger.info(f"ldapsync user {instance.group} (for PosixGroup)") | ||
sync_group(instance.group) | ||
except Exception as e: | ||
print(f"Exception in ldapsync of {instance}: {e}") | ||
except Exception: | ||
logger.exception(f"Exception in ldapsync of {instance}") | ||
|
||
|
||
@receiver(m2m_changed, sender=Group.user_set.through) | ||
def sync_ldap_group_m2m(sender, instance, *, action=None, **kwargs): | ||
if action in ["post_add", "post_remove"]: | ||
try: | ||
print(f"ldapsync group {instance} (for membership)") | ||
logger.info(f"ldapsync group {instance} (for membership)") | ||
sync_group(instance) | ||
except Exception as e: | ||
print(f"Exception in ldapsync of {instance}: {e}") | ||
logger.exception(f"Exception in ldapsync of {instance}") | ||
|
||
|
||
# @receiver(post_save) | ||
# def test_post_save(sender, instance, **kwargs): | ||
# print(f"POST_SAVE sender={sender} instance={instance} kwargs={kwargs}") | ||
# logger.debug(f"POST_SAVE sender={sender} instance={instance} kwargs={kwargs}") | ||
|
||
|
||
# @receiver(m2m_changed) | ||
# def test_m2m_changed(sender, instance, **kwargs): | ||
# print(f"M2M_CHANGED sender={sender} instance={instance} kwargs={kwargs}") | ||
# logger.debug(f"M2M_CHANGED sender={sender} instance={instance} kwargs={kwargs}") |