Skip to content

Commit

Permalink
ldapsync: trigger on m2m changes
Browse files Browse the repository at this point in the history
  • Loading branch information
timhawes committed Jul 20, 2024
1 parent 64f84b2 commit bb4ea3c
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions ldapsync/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from django.db.models.signals import post_save
from django.db.models.signals import m2m_changed, post_save
from django.dispatch import receiver

from posixusers.models import PosixGroup, PosixUser
from posixusers.models import PosixGroup, PosixUser, SSHKey

from .utils import sync_group, sync_user

Expand All @@ -19,6 +19,7 @@ def sync_ldap_user(sender, instance, update_fields=[], **kwargs):
# skip update if last_login was the only change
return
try:
print(f"ldapsync user {instance}")
sync_user(instance)
except Exception as e:
print(f"Exception in ldapsync of {instance}: {e}")
Expand All @@ -27,6 +28,16 @@ def sync_ldap_user(sender, instance, update_fields=[], **kwargs):
@receiver(post_save, sender=PosixUser)
def sync_ldap_posix_user(sender, instance, **kwargs):
try:
print(f"ldapsync user {instance.user} (for PosixUser)")
sync_user(instance.user)
except Exception as e:
print(f"Exception in ldapsync of {instance}: {e}")


@receiver(post_save, sender=SSHKey)
def sync_ldap_posix_sshkey(sender, instance, **kwargs):
try:
print(f"ldapsync user {instance.user} (for SSHKey)")
sync_user(instance.user)
except Exception as e:
print(f"Exception in ldapsync of {instance}: {e}")
Expand All @@ -35,6 +46,7 @@ def sync_ldap_posix_user(sender, instance, **kwargs):
@receiver(post_save, sender=Group)
def sync_ldap_group(sender, instance, **kwargs):
try:
print(f"ldapsync group {instance}")
sync_group(instance)
except Exception as e:
print(f"Exception in ldapsync of {instance}: {e}")
Expand All @@ -43,6 +55,27 @@ def sync_ldap_group(sender, instance, **kwargs):
@receiver(post_save, sender=PosixGroup)
def sync_ldap_posix_group(sender, instance, **kwargs):
try:
print(f"ldapsync user {instance.group} (for PosixGroup)")
sync_group(instance.group)
except Exception as e:
print(f"Exception in ldapsync of {instance}: {e}")


@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)")
sync_group(instance)
except Exception as e:
print(f"Exception in ldapsync of {instance}: {e}")


# @receiver(post_save)
# def test_post_save(sender, instance, **kwargs):
# print(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}")

0 comments on commit bb4ea3c

Please sign in to comment.