diff --git a/zerver/management/commands/sync_ldap_user_data.py b/zerver/management/commands/sync_ldap_user_data.py index c8251d93ae..8c3e2872f3 100644 --- a/zerver/management/commands/sync_ldap_user_data.py +++ b/zerver/management/commands/sync_ldap_user_data.py @@ -11,7 +11,7 @@ from django.db.utils import IntegrityError from zerver.lib.logging_util import log_to_file from zerver.lib.management import ZulipBaseCommand from zerver.models import UserProfile -from zproject.backends import ZulipLDAPUserPopulator, ZulipLDAPException +from zproject.backends import ZulipLDAPException, sync_user_from_ldap ## Setup ## logger = logging.getLogger(__name__) @@ -20,12 +20,11 @@ log_to_file(logger, settings.LDAP_SYNC_LOG_PATH) # Run this on a cronjob to pick up on name changes. def sync_ldap_user_data(user_profiles: List[UserProfile]) -> None: logger.info("Starting update.") - backend = ZulipLDAPUserPopulator() for u in user_profiles: # This will save the user if relevant, and will do nothing if the user # does not exist. try: - if backend.populate_user(backend.django_to_ldap_username(u.email)) is not None: + if sync_user_from_ldap(u): logger.info("Updated %s." % (u.email,)) else: logger.warning("Did not find %s in LDAP." % (u.email,)) diff --git a/zproject/backends.py b/zproject/backends.py index f35bade39c..61a01dad30 100644 --- a/zproject/backends.py +++ b/zproject/backends.py @@ -414,6 +414,11 @@ class ZulipLDAPUserPopulator(ZulipLDAPAuthBackendBase): return_data: Optional[Dict[str, Any]]=None) -> None: return None +def sync_user_from_ldap(user_profile: UserProfile) -> bool: + backend = ZulipLDAPUserPopulator() + updated_user = backend.populate_user(backend.django_to_ldap_username(user_profile.email)) + return updated_user is not None + class DevAuthBackend(ZulipAuthMixin): # Allow logging in as any user without a password. # This is used for convenience when developing Zulip.