2017-12-13 01:45:57 +01:00
|
|
|
import logging
|
2019-01-11 11:25:36 +01:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
from typing import Any, List
|
|
|
|
|
2017-11-16 00:43:27 +01:00
|
|
|
from django.conf import settings
|
2020-01-14 21:59:46 +01:00
|
|
|
from django.db import transaction
|
2013-11-25 22:22:17 +01:00
|
|
|
|
2017-12-13 01:45:57 +01:00
|
|
|
from zerver.lib.logging_util import log_to_file
|
2019-01-11 11:25:36 +01:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2017-11-16 00:43:27 +01:00
|
|
|
from zerver.models import UserProfile
|
2019-01-12 12:32:54 +01:00
|
|
|
from zproject.backends import ZulipLDAPException, sync_user_from_ldap
|
2013-11-25 22:22:17 +01:00
|
|
|
|
|
|
|
## Setup ##
|
2019-01-14 16:14:50 +01:00
|
|
|
logger = logging.getLogger('zulip.sync_ldap_user_data')
|
2017-12-13 01:45:57 +01:00
|
|
|
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.
|
2020-01-03 05:45:05 +01:00
|
|
|
def sync_ldap_user_data(user_profiles: List[UserProfile], deactivation_protection: bool=True) -> None:
|
2013-11-25 22:22:17 +01:00
|
|
|
logger.info("Starting update.")
|
2020-01-03 05:45:05 +01:00
|
|
|
with transaction.atomic():
|
|
|
|
realms = set([u.realm.string_id for u in user_profiles])
|
|
|
|
|
|
|
|
for u in user_profiles:
|
|
|
|
# This will save the user if relevant, and will do nothing if the user
|
|
|
|
# does not exist.
|
|
|
|
try:
|
|
|
|
sync_user_from_ldap(u, logger)
|
|
|
|
except ZulipLDAPException as e:
|
|
|
|
logger.error("Error attempting to update user %s:" % (u.delivery_email,))
|
|
|
|
logger.error(e)
|
|
|
|
|
|
|
|
if deactivation_protection:
|
|
|
|
if not UserProfile.objects.filter(is_bot=False, is_active=True).exists():
|
|
|
|
error_msg = ("Ldap sync would have deactivated all users. This is most likely due " +
|
|
|
|
"to a misconfiguration of ldap settings. Rolling back...\n" +
|
|
|
|
"Use the --force option if the mass deactivation is intended.")
|
|
|
|
logger.error(error_msg)
|
|
|
|
# Raising an exception in this atomic block will rollback the transaction.
|
|
|
|
raise Exception(error_msg)
|
|
|
|
for string_id in realms:
|
|
|
|
if not UserProfile.objects.filter(is_bot=False, is_active=True, realm__string_id=string_id,
|
|
|
|
role__gte=UserProfile.ROLE_REALM_ADMINISTRATOR).exists():
|
|
|
|
error_msg = ("Ldap sync would have deactivated all administrators of realm %s. " +
|
|
|
|
"This is most likely due " +
|
|
|
|
"to a misconfiguration of ldap settings. Rolling back...\n" +
|
|
|
|
"Use the --force option if the mass deactivation is intended.")
|
|
|
|
error_msg = error_msg % (string_id,)
|
|
|
|
logger.error(error_msg)
|
|
|
|
raise Exception(error_msg)
|
|
|
|
|
2013-11-25 22:22:17 +01:00
|
|
|
logger.info("Finished update.")
|
|
|
|
|
2019-01-11 11:25:36 +01:00
|
|
|
class Command(ZulipBaseCommand):
|
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2020-01-03 05:45:05 +01:00
|
|
|
parser.add_argument('-f', '--force',
|
|
|
|
dest='force',
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help='Disable the protection against deactivating all users.')
|
|
|
|
|
2019-01-11 11:25:36 +01:00
|
|
|
self.add_realm_args(parser)
|
|
|
|
self.add_user_list_args(parser)
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2019-01-14 16:45:03 +01:00
|
|
|
if options.get('realm_id') is not None:
|
2019-01-11 11:25:36 +01:00
|
|
|
realm = self.get_realm(options)
|
2019-08-14 19:48:54 +02:00
|
|
|
user_profiles = self.get_users(options, realm, is_bot=False,
|
|
|
|
include_deactivated=True)
|
2019-01-11 11:25:36 +01:00
|
|
|
else:
|
|
|
|
user_profiles = UserProfile.objects.select_related().filter(is_bot=False)
|
2020-01-03 05:45:05 +01:00
|
|
|
sync_ldap_user_data(user_profiles, not options['force'])
|