2013-11-25 22:22:17 +01:00
|
|
|
|
2017-12-13 01:45:57 +01:00
|
|
|
import logging
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
2013-11-25 22:22:17 +01:00
|
|
|
|
2017-11-16 00:43:27 +01:00
|
|
|
from django.conf import settings
|
2013-11-25 22:22:17 +01:00
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.db.utils import IntegrityError
|
|
|
|
|
2017-12-13 01:45:57 +01:00
|
|
|
from zerver.lib.logging_util import log_to_file
|
2017-11-16 00:43:27 +01:00
|
|
|
from zerver.models import UserProfile
|
2018-08-20 19:30:19 +02:00
|
|
|
from zproject.backends import ZulipLDAPUserPopulator, ZulipLDAPException
|
2013-11-25 22:22:17 +01:00
|
|
|
|
|
|
|
## Setup ##
|
2017-12-13 01:45:57 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
log_to_file(logger, settings.LDAP_SYNC_LOG_PATH)
|
2013-11-25 22:22:17 +01:00
|
|
|
|
|
|
|
# Run this on a cronjob to pick up on name changes.
|
2017-10-26 11:35:57 +02:00
|
|
|
def sync_ldap_user_data() -> None:
|
2013-11-25 22:22:17 +01:00
|
|
|
logger.info("Starting update.")
|
|
|
|
backend = ZulipLDAPUserPopulator()
|
|
|
|
for u in UserProfile.objects.select_related().filter(is_active=True, is_bot=False).all():
|
|
|
|
# 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:
|
|
|
|
logger.info("Updated %s." % (u.email,))
|
|
|
|
else:
|
|
|
|
logger.warning("Did not find %s in LDAP." % (u.email,))
|
2018-08-20 19:30:19 +02:00
|
|
|
except ZulipLDAPException as e:
|
|
|
|
logger.error("Error attempting to update user %s:" % (u.email,))
|
|
|
|
logger.error(e)
|
2013-11-25 22:22:17 +01:00
|
|
|
logger.info("Finished update.")
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2013-11-25 22:22:17 +01:00
|
|
|
sync_ldap_user_data()
|