snapshot of https://github.com/zulip/zulip.git @ 48c69911c7dd4e03b20552eb3bd77ad8151c1357 (2026-09-15)
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
This commit is contained in:
98
zerver/management/commands/soft_deactivate_users.py
Normal file
98
zerver/management/commands/soft_deactivate_users.py
Normal file
@@ -0,0 +1,98 @@
|
||||
import sys
|
||||
from argparse import ArgumentParser
|
||||
from typing import Any
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.management.base import CommandError
|
||||
from typing_extensions import override
|
||||
|
||||
from zerver.lib.management import ZulipBaseCommand, abort_unless_locked
|
||||
from zerver.lib.soft_deactivation import (
|
||||
do_auto_soft_deactivate_users,
|
||||
do_soft_activate_users,
|
||||
do_soft_deactivate_users,
|
||||
logger,
|
||||
)
|
||||
from zerver.models import Realm, UserProfile
|
||||
|
||||
|
||||
def get_users_from_emails(emails: list[str], filter_kwargs: dict[str, Realm]) -> list[UserProfile]:
|
||||
# Bug: Ideally, this would be case-insensitive like our other email queries.
|
||||
users = list(UserProfile.objects.filter(delivery_email__in=emails, **filter_kwargs))
|
||||
|
||||
if len(users) != len(emails):
|
||||
user_emails_found = {user.delivery_email for user in users}
|
||||
user_emails_not_found = "\n".join(set(emails) - user_emails_found)
|
||||
raise CommandError(
|
||||
"Users with the following emails were not found:\n\n"
|
||||
f"{user_emails_not_found}\n\n"
|
||||
"Check if they are correct.",
|
||||
)
|
||||
return users
|
||||
|
||||
|
||||
class Command(ZulipBaseCommand):
|
||||
help = """Soft activate/deactivate users. Users are recognised by their emails here."""
|
||||
|
||||
@override
|
||||
def add_arguments(self, parser: ArgumentParser) -> None:
|
||||
self.add_realm_args(parser)
|
||||
parser.add_argument(
|
||||
"-d", "--deactivate", action="store_true", help="Used to deactivate user/users."
|
||||
)
|
||||
parser.add_argument(
|
||||
"-a", "--activate", action="store_true", help="Used to activate user/users."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--inactive-for",
|
||||
type=int,
|
||||
default=28,
|
||||
help="Number of days of inactivity before soft-deactivation",
|
||||
)
|
||||
parser.add_argument(
|
||||
"users",
|
||||
metavar="<users>",
|
||||
nargs="*",
|
||||
help="A list of user emails to soft activate/deactivate.",
|
||||
)
|
||||
|
||||
@override
|
||||
@abort_unless_locked
|
||||
def handle(self, *args: Any, **options: Any) -> None:
|
||||
if settings.STAGING:
|
||||
print("This is a Staging server. Suppressing management command.")
|
||||
sys.exit(0)
|
||||
|
||||
realm = self.get_realm(options)
|
||||
user_emails = options["users"]
|
||||
activate = options["activate"]
|
||||
deactivate = options["deactivate"]
|
||||
|
||||
filter_kwargs: dict[str, Realm] = {}
|
||||
if realm is not None:
|
||||
filter_kwargs = dict(realm=realm)
|
||||
|
||||
if activate:
|
||||
if not user_emails:
|
||||
print("You need to specify at least one user to use the activate option.")
|
||||
self.print_help("./manage.py", "soft_deactivate_users")
|
||||
raise CommandError
|
||||
|
||||
users_to_activate = get_users_from_emails(user_emails, filter_kwargs)
|
||||
users_activated = do_soft_activate_users(users_to_activate)
|
||||
logger.info("Soft reactivated %d user(s)", len(users_activated))
|
||||
|
||||
elif deactivate:
|
||||
if user_emails:
|
||||
users_to_deactivate = get_users_from_emails(user_emails, filter_kwargs)
|
||||
print("Soft deactivating forcefully...")
|
||||
users_deactivated = do_soft_deactivate_users(users_to_deactivate)
|
||||
else:
|
||||
users_deactivated = do_auto_soft_deactivate_users(
|
||||
int(options["inactive_for"]), realm
|
||||
)
|
||||
logger.info("Soft deactivated %d user(s)", len(users_deactivated))
|
||||
|
||||
else:
|
||||
self.print_help("./manage.py", "soft_deactivate_users")
|
||||
raise CommandError
|
||||
Reference in New Issue
Block a user