Browse Source

Avoid LDAP sync on irrelevant changes

master
klonfish 6 years ago
parent
commit
31cbf7be0f
  1. 8
      bam/ldap_sync.py
  2. 7
      bam/signals.py

8
bam/ldap_sync.py

@ -51,6 +51,14 @@ class LDAPUserEntry(): @@ -51,6 +51,14 @@ class LDAPUserEntry():
}
passw_attr = 'userPassword'
@classmethod
def get_mapped_fields(cls):
fields = set()
fields.update(cls.attr_map.values())
fields.update(cls.mandatory_attr_fallbacks.values())
fields.add(cls.passw_attr)
return fields
def __init__(self, user, ldap_conn, base_dn):
self.user = user
self.base_dn = base_dn

7
bam/signals.py

@ -2,7 +2,7 @@ from django.dispatch import receiver @@ -2,7 +2,7 @@ from django.dispatch import receiver
from django.db.models.signals import post_save, post_delete, m2m_changed
from django.contrib.auth import get_user_model
from django.conf import settings
from .ldap_sync import make_ldap_conn, LDAPAction, LDAPUserSyncer
from .ldap_sync import make_ldap_conn, LDAPUserEntry, LDAPUserSyncer
User = get_user_model()
@ -25,7 +25,10 @@ def synchronous_user_sync(user, remove=False): @@ -25,7 +25,10 @@ def synchronous_user_sync(user, remove=False):
ldap_conn.unbind_s()
@receiver(post_save, sender=User)
def handle_user_save(sender, instance, **kwargs):
def handle_user_save(sender, instance, created, raw, using, update_fields,
**kwargs):
relevant_fields = LDAPUserEntry.get_mapped_fields()
if update_fields is None or not update_fields.isdisjoint(relevant_fields):
synchronous_user_sync(instance)
@receiver(post_delete, sender=User)

Loading…
Cancel
Save