2016-10-16 00:29:57 +02:00
|
|
|
from argparse import ArgumentParser
|
2017-11-16 00:43:27 +01:00
|
|
|
from typing import Any
|
2013-04-02 18:00:12 +02:00
|
|
|
|
2022-03-27 22:40:21 +02:00
|
|
|
from django.db.models import Q
|
|
|
|
|
|
|
|
from zerver.actions.user_settings import bulk_regenerate_api_keys
|
2017-08-07 21:18:19 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.sessions import (
|
|
|
|
delete_all_deactivated_user_sessions,
|
|
|
|
delete_all_user_sessions,
|
|
|
|
delete_realm_user_sessions,
|
|
|
|
)
|
2022-03-27 22:40:21 +02:00
|
|
|
from zerver.models import UserProfile
|
2013-04-02 18:00:12 +02:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2017-08-07 21:18:19 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2022-03-24 22:54:42 +01:00
|
|
|
help = """\
|
|
|
|
Log out all users from active browser sessions.
|
|
|
|
|
|
|
|
Does not disable API keys, and thus will not log users out of the
|
|
|
|
mobile apps.
|
|
|
|
"""
|
2013-04-02 18:00:12 +02:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"--deactivated-only",
|
|
|
|
action="store_true",
|
docs: Add missing space to compound verbs “log in”, “set up”, etc.
Noun: backup, checkout, cleanup, login, logout, setup, shutdown, signup,
timeout.
Verb: back up, check out, clean up, log in, log out, set up, shut
down, sign up, time out.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-04-25 23:05:38 +02:00
|
|
|
help="Only log out all users who are deactivated",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2022-03-27 22:40:21 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"--rotate-api-keys",
|
|
|
|
action="store_true",
|
|
|
|
help="Also rotate API keys of the affected users",
|
|
|
|
)
|
docs: Add missing space to compound verbs “log in”, “set up”, etc.
Noun: backup, checkout, cleanup, login, logout, setup, shutdown, signup,
timeout.
Verb: back up, check out, clean up, log in, log out, set up, shut
down, sign up, time out.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-04-25 23:05:38 +02:00
|
|
|
self.add_realm_args(parser, help="Only log out all users in a particular realm")
|
2016-10-16 00:29:57 +02:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2017-08-07 21:18:19 +02:00
|
|
|
realm = self.get_realm(options)
|
2022-03-27 22:40:21 +02:00
|
|
|
rotate_api_keys = options["rotate_api_keys"]
|
2017-08-07 21:18:19 +02:00
|
|
|
if realm:
|
2013-04-02 18:00:12 +02:00
|
|
|
delete_realm_user_sessions(realm)
|
2022-03-27 22:40:21 +02:00
|
|
|
regenerate_api_key_queryset = UserProfile.objects.filter(realm=realm).values_list(
|
|
|
|
"id", flat=True
|
|
|
|
)
|
2016-05-09 23:01:49 +02:00
|
|
|
elif options["deactivated_only"]:
|
|
|
|
delete_all_deactivated_user_sessions()
|
2022-03-27 22:40:21 +02:00
|
|
|
regenerate_api_key_queryset = UserProfile.objects.filter(
|
|
|
|
Q(is_active=False) | Q(realm__deactivated=True)
|
|
|
|
).values_list("id", flat=True)
|
2013-04-02 18:00:12 +02:00
|
|
|
else:
|
|
|
|
delete_all_user_sessions()
|
2022-03-27 22:40:21 +02:00
|
|
|
regenerate_api_key_queryset = UserProfile.objects.values_list("id", flat=True)
|
|
|
|
|
|
|
|
if rotate_api_keys:
|
|
|
|
bulk_regenerate_api_keys(regenerate_api_key_queryset)
|