diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 012ea08182..48d281e899 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -3,6 +3,7 @@ from __future__ import absolute_import from django.conf import settings from django.core import validators from django.contrib.sessions.models import Session +from zerver.lib.cache import update_user_profile_cache from zerver.lib.context_managers import lockfile from zerver.models import Realm, RealmEmoji, Stream, UserProfile, UserActivity, \ Subscription, Recipient, Message, UserMessage, valid_stream_name, \ @@ -138,6 +139,29 @@ def delete_all_user_sessions(): for session in Session.objects.all(): delete_session(session) +def active_humans_in_realm(realm): + return UserProfile.objects.filter(realm=realm, is_active=True, is_bot=False) + +def do_deactivate_realm(realm): + """ + Deactivate this realm. Do NOT deactivate the users -- we need to be able to + tell the difference between users that were intentionally deactivated, + e.g. by a realm admin, and users who can't currently use Zulip because their + realm has been deactivated. + """ + if realm.deactivated: + return + + realm.deactivated = True + realm.save(update_fields=["deactivated"]) + + for user in active_humans_in_realm(realm): + # Don't deactivate the users, but do delete their sessions so they get + # bumped to the login screen, where they'll get a realm deactivation + # notice when they try to log in. + delete_user_sessions(user) + update_user_profile_cache(None, instance=user, update_fields=None) + def do_deactivate_user(user_profile, log=True, _cascade=True): if not user_profile.is_active: return diff --git a/zilencer/management/commands/deactivate_old_realms.py b/zilencer/management/commands/deactivate_old_realms.py new file mode 100644 index 0000000000..805a5f20e1 --- /dev/null +++ b/zilencer/management/commands/deactivate_old_realms.py @@ -0,0 +1,17 @@ +from __future__ import absolute_import + +from django.core.management.base import BaseCommand + +from zerver.lib.actions import do_deactivate_realm +from zerver.models import get_realm + +domains_to_deactivate = """""" + +class Command(BaseCommand): + help = """One-off script to deactivate our old realms.""" + + def handle(self, *args, **options): + for domain in [elt.strip() for elt in domains_to_deactivate.split("\n")]: + print "Deactivating", domain + do_deactivate_realm(get_realm(domain)) + print "Done!"