mirror of
https://github.com/zulip/zulip.git
synced 2025-11-10 17:07:07 +00:00
Management command to update names from the LDAP database
(imported from commit 1e1b12c055926899fdca3e484df6c9437c800c6c)
This commit is contained in:
43
zerver/management/commands/sync_ldap_user_data.py
Normal file
43
zerver/management/commands/sync_ldap_user_data.py
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from django.core.management.base import BaseCommand
|
||||||
|
from django.db.utils import IntegrityError
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
|
from zproject.backends import ZulipLDAPUserPopulator
|
||||||
|
from zerver.models import UserProfile
|
||||||
|
|
||||||
|
## Setup ##
|
||||||
|
|
||||||
|
log_format = "%(asctime)s: %(message)s"
|
||||||
|
logging.basicConfig(format=log_format)
|
||||||
|
|
||||||
|
formatter = logging.Formatter(log_format)
|
||||||
|
file_handler = logging.FileHandler(settings.LDAP_SYNC_LOG_PATH)
|
||||||
|
file_handler.setFormatter(formatter)
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
logger.setLevel(logging.INFO)
|
||||||
|
logger.addHandler(file_handler)
|
||||||
|
|
||||||
|
# Run this on a cronjob to pick up on name changes.
|
||||||
|
def sync_ldap_user_data():
|
||||||
|
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,))
|
||||||
|
except IntegrityError:
|
||||||
|
logger.warning("User populated did not match an existing user.")
|
||||||
|
logger.info("Finished update.")
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
sync_ldap_user_data()
|
||||||
@@ -600,6 +600,7 @@ if DEPLOYED:
|
|||||||
JSON_PERSISTENT_QUEUE_FILENAME = "/home/zulip/tornado/event_queues.json"
|
JSON_PERSISTENT_QUEUE_FILENAME = "/home/zulip/tornado/event_queues.json"
|
||||||
EMAIL_MIRROR_LOG_PATH = "/var/log/zulip/email-mirror.log"
|
EMAIL_MIRROR_LOG_PATH = "/var/log/zulip/email-mirror.log"
|
||||||
EMAIL_DELIVERER_LOG_PATH = "/var/log/zulip/email-deliverer.log"
|
EMAIL_DELIVERER_LOG_PATH = "/var/log/zulip/email-deliverer.log"
|
||||||
|
LDAP_SYNC_LOG_PATH = "/var/log/zulip/sync_names_from_ldap.log"
|
||||||
QUEUE_ERROR_DIR = '/var/log/zulip/queue_error'
|
QUEUE_ERROR_DIR = '/var/log/zulip/queue_error'
|
||||||
else:
|
else:
|
||||||
EVENT_LOG_DIR = 'event_log'
|
EVENT_LOG_DIR = 'event_log'
|
||||||
@@ -611,6 +612,7 @@ else:
|
|||||||
JSON_PERSISTENT_QUEUE_FILENAME = "event_queues.json"
|
JSON_PERSISTENT_QUEUE_FILENAME = "event_queues.json"
|
||||||
EMAIL_MIRROR_LOG_PATH = "email-mirror.log"
|
EMAIL_MIRROR_LOG_PATH = "email-mirror.log"
|
||||||
EMAIL_DELIVERER_LOG_PATH = "email-deliverer.log"
|
EMAIL_DELIVERER_LOG_PATH = "email-deliverer.log"
|
||||||
|
LDAP_SYNC_LOG_PATH = "sync_names_from_ldap.log"
|
||||||
QUEUE_ERROR_DIR = 'queue_error'
|
QUEUE_ERROR_DIR = 'queue_error'
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user