Browse Source

Avoid LDAP sync on irrelevant changes

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

8
bam/ldap_sync.py

@ -51,6 +51,14 @@ class LDAPUserEntry():
} }
passw_attr = 'userPassword' 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): def __init__(self, user, ldap_conn, base_dn):
self.user = user self.user = user
self.base_dn = base_dn self.base_dn = base_dn

9
bam/signals.py

@ -2,7 +2,7 @@ from django.dispatch import receiver
from django.db.models.signals import post_save, post_delete, m2m_changed from django.db.models.signals import post_save, post_delete, m2m_changed
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.conf import settings 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() User = get_user_model()
@ -25,8 +25,11 @@ def synchronous_user_sync(user, remove=False):
ldap_conn.unbind_s() ldap_conn.unbind_s()
@receiver(post_save, sender=User) @receiver(post_save, sender=User)
def handle_user_save(sender, instance, **kwargs): def handle_user_save(sender, instance, created, raw, using, update_fields,
synchronous_user_sync(instance) **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) @receiver(post_delete, sender=User)
def handle_user_delete(sender, instance, **kwargs): def handle_user_delete(sender, instance, **kwargs):

Loading…
Cancel
Save