2017-07-21 10:57:55 +02:00
|
|
|
import sys
|
2017-11-16 00:43:27 +01:00
|
|
|
from argparse import ArgumentParser
|
2019-03-14 05:34:20 +01:00
|
|
|
from typing import Any, Dict, List
|
2017-11-16 00:43:27 +01:00
|
|
|
|
|
|
|
from django.conf import settings
|
2019-03-14 05:34:20 +01:00
|
|
|
from django.core.management.base import CommandError
|
2022-06-23 19:59:20 +02:00
|
|
|
from django.db.models.query import QuerySet
|
2017-07-21 10:57:55 +02:00
|
|
|
|
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.soft_deactivation import (
|
|
|
|
do_auto_soft_deactivate_users,
|
|
|
|
do_soft_activate_users,
|
|
|
|
do_soft_deactivate_users,
|
|
|
|
logger,
|
|
|
|
)
|
2017-11-16 00:43:27 +01:00
|
|
|
from zerver.models import Realm, UserProfile
|
2017-07-21 10:57:55 +02:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2022-06-23 19:59:20 +02:00
|
|
|
def get_users_from_emails(
|
|
|
|
emails: List[str], filter_kwargs: Dict[str, Realm]
|
|
|
|
) -> QuerySet[UserProfile]:
|
2019-11-16 01:42:36 +01:00
|
|
|
# Bug: Ideally, this would be case-insensitive like our other email queries.
|
2021-02-12 08:19:30 +01:00
|
|
|
users = UserProfile.objects.filter(delivery_email__in=emails, **filter_kwargs)
|
2019-03-14 05:34:20 +01:00
|
|
|
|
|
|
|
if len(users) != len(emails):
|
2019-11-16 01:42:36 +01:00
|
|
|
user_emails_found = {user.delivery_email for user in users}
|
2021-02-12 08:20:45 +01:00
|
|
|
user_emails_not_found = "\n".join(set(emails) - user_emails_found)
|
2020-06-14 02:57:50 +02:00
|
|
|
raise CommandError(
|
2021-02-12 08:20:45 +01:00
|
|
|
"Users with the following emails were not found:\n\n"
|
|
|
|
f"{user_emails_not_found}\n\n"
|
|
|
|
"Check if they are correct.",
|
2020-06-14 02:57:50 +02:00
|
|
|
)
|
2019-03-14 05:34:20 +01:00
|
|
|
return users
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-07-21 10:57:55 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2019-03-14 05:34:20 +01:00
|
|
|
help = """Soft activate/deactivate users. Users are recognised by their emails here."""
|
2017-07-21 10:57:55 +02:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2017-08-22 02:43:03 +02:00
|
|
|
self.add_realm_args(parser)
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"-d", "--deactivate", action="store_true", help="Used to deactivate user/users."
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"-a", "--activate", action="store_true", help="Used to activate user/users."
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"--inactive-for",
|
2021-02-12 08:19:30 +01:00
|
|
|
type=int,
|
|
|
|
default=28,
|
2021-02-12 08:20:45 +01:00
|
|
|
help="Number of days of inactivity before soft-deactivation",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"users",
|
|
|
|
metavar="<users>",
|
|
|
|
nargs="*",
|
2021-02-12 08:19:30 +01:00
|
|
|
help="A list of user emails to soft activate/deactivate.",
|
|
|
|
)
|
2017-07-21 10:57:55 +02:00
|
|
|
|
2019-08-10 00:30:34 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2017-08-22 02:46:19 +02:00
|
|
|
if settings.STAGING:
|
2021-02-12 08:20:45 +01:00
|
|
|
print("This is a Staging server. Suppressing management command.")
|
2017-08-22 02:46:19 +02:00
|
|
|
sys.exit(0)
|
|
|
|
|
2017-09-26 22:23:11 +02:00
|
|
|
realm = self.get_realm(options)
|
2021-02-12 08:20:45 +01:00
|
|
|
user_emails = options["users"]
|
|
|
|
activate = options["activate"]
|
|
|
|
deactivate = options["deactivate"]
|
2017-09-26 01:23:44 +02:00
|
|
|
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
filter_kwargs: Dict[str, Realm] = {}
|
2017-09-26 01:23:44 +02:00
|
|
|
if realm is not None:
|
2017-08-22 02:43:03 +02:00
|
|
|
filter_kwargs = dict(realm=realm)
|
|
|
|
|
2017-07-21 10:57:55 +02:00
|
|
|
if activate:
|
|
|
|
if not user_emails:
|
2021-02-12 08:20:45 +01:00
|
|
|
print("You need to specify at least one user to use the activate option.")
|
2017-10-06 17:48:43 +02:00
|
|
|
self.print_help("./manage.py", "soft_deactivate_users")
|
2019-05-03 23:20:39 +02:00
|
|
|
raise CommandError
|
2017-08-22 02:43:03 +02:00
|
|
|
|
2019-03-14 05:34:20 +01:00
|
|
|
users_to_activate = get_users_from_emails(user_emails, filter_kwargs)
|
2017-08-16 06:15:56 +02:00
|
|
|
users_activated = do_soft_activate_users(users_to_activate)
|
2021-05-10 07:02:14 +02:00
|
|
|
logger.info("Soft reactivated %d user(s)", len(users_activated))
|
2019-03-14 05:34:20 +01:00
|
|
|
|
2017-07-21 10:57:55 +02:00
|
|
|
elif deactivate:
|
|
|
|
if user_emails:
|
2019-03-14 05:34:20 +01:00
|
|
|
users_to_deactivate = get_users_from_emails(user_emails, filter_kwargs)
|
2021-02-12 08:20:45 +01:00
|
|
|
print("Soft deactivating forcefully...")
|
2019-03-12 04:28:40 +01:00
|
|
|
users_deactivated = do_soft_deactivate_users(users_to_deactivate)
|
2017-07-21 10:57:55 +02:00
|
|
|
else:
|
2021-02-12 08:19:30 +01:00
|
|
|
users_deactivated = do_auto_soft_deactivate_users(
|
2021-02-12 08:20:45 +01:00
|
|
|
int(options["inactive_for"]), realm
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
logger.info("Soft deactivated %d user(s)", len(users_deactivated))
|
2017-07-21 10:57:55 +02:00
|
|
|
|
|
|
|
else:
|
2017-10-06 17:48:43 +02:00
|
|
|
self.print_help("./manage.py", "soft_deactivate_users")
|
2019-05-03 23:20:39 +02:00
|
|
|
raise CommandError
|