management: Extract `sync_user_from_ldap()`.

This commit is contained in:
Harshit Bansal 2019-01-12 11:32:54 +00:00 committed by Tim Abbott
parent 6e20a9a419
commit 348f370b79
2 changed files with 7 additions and 3 deletions

View File

@ -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,))

View File

@ -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.