2020-10-22 20:30:35 +02:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from django.core.management.base import CommandError
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
2020-10-22 20:30:35 +02:00
|
|
|
|
2022-04-14 23:48:28 +02:00
|
|
|
from zerver.actions.users import (
|
2020-12-14 22:02:22 +01:00
|
|
|
do_change_can_create_users,
|
|
|
|
do_change_can_forge_sender,
|
|
|
|
do_change_user_role,
|
|
|
|
)
|
2020-10-22 20:30:35 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
|
|
|
|
|
|
|
class Command(ZulipBaseCommand):
|
|
|
|
help = """Change role of an existing user in their (own) Realm.
|
|
|
|
|
|
|
|
ONLY perform this on customer request from an authorized person.
|
|
|
|
"""
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2020-10-22 20:30:35 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
parser.add_argument("email", metavar="<email>", help="email of user to change role")
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"new_role",
|
|
|
|
metavar="<new_role>",
|
2021-04-22 22:03:03 +02:00
|
|
|
choices=[
|
|
|
|
"owner",
|
|
|
|
"admin",
|
|
|
|
"moderator",
|
|
|
|
"member",
|
|
|
|
"guest",
|
|
|
|
"can_forge_sender",
|
|
|
|
"can_create_users",
|
|
|
|
],
|
2021-02-12 08:19:30 +01:00
|
|
|
help="new role of the user",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"--revoke",
|
|
|
|
dest="grant",
|
2021-02-12 08:19:30 +01:00
|
|
|
action="store_false",
|
2021-02-12 08:20:45 +01:00
|
|
|
help="Remove can_forge_sender or can_create_users permission.",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-05-10 21:29:25 +02:00
|
|
|
self.add_realm_args(parser, required=True)
|
2020-10-22 20:30:35 +02:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2020-10-22 20:30:35 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
email = options["email"]
|
2020-10-22 20:30:35 +02:00
|
|
|
realm = self.get_realm(options)
|
|
|
|
|
|
|
|
user = self.get_user(email, realm)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
user_role_map = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"owner": UserProfile.ROLE_REALM_OWNER,
|
|
|
|
"admin": UserProfile.ROLE_REALM_ADMINISTRATOR,
|
2021-04-22 22:03:03 +02:00
|
|
|
"moderator": UserProfile.ROLE_MODERATOR,
|
2021-02-12 08:20:45 +01:00
|
|
|
"member": UserProfile.ROLE_MEMBER,
|
|
|
|
"guest": UserProfile.ROLE_GUEST,
|
2021-02-12 08:19:30 +01:00
|
|
|
}
|
2020-10-22 20:30:35 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if options["new_role"] not in ["can_forge_sender", "can_create_users"]:
|
|
|
|
new_role = user_role_map[options["new_role"]]
|
|
|
|
if not options["grant"]:
|
2021-02-12 08:19:30 +01:00
|
|
|
raise CommandError(
|
|
|
|
"Revoke not supported with this permission; please specify new role."
|
|
|
|
)
|
2020-10-22 20:30:35 +02:00
|
|
|
if new_role == user.role:
|
|
|
|
raise CommandError("User already has this role.")
|
|
|
|
old_role_name = UserProfile.ROLE_ID_TO_NAME_MAP[user.role]
|
|
|
|
do_change_user_role(user, new_role, acting_user=None)
|
|
|
|
new_role_name = UserProfile.ROLE_ID_TO_NAME_MAP[user.role]
|
2021-02-12 08:19:30 +01:00
|
|
|
print(
|
|
|
|
f"Role for {user.delivery_email} changed from {old_role_name} to {new_role_name}."
|
|
|
|
)
|
2020-12-14 22:02:22 +01:00
|
|
|
return
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if options["new_role"] == "can_forge_sender":
|
|
|
|
if user.can_forge_sender and options["grant"]:
|
2020-12-20 14:21:42 +01:00
|
|
|
raise CommandError("User can already forge messages for this realm.")
|
2021-02-12 08:20:45 +01:00
|
|
|
elif not user.can_forge_sender and not options["grant"]:
|
2020-12-20 14:21:42 +01:00
|
|
|
raise CommandError("User can't forge messages for this realm.")
|
2021-02-12 08:20:45 +01:00
|
|
|
do_change_can_forge_sender(user, options["grant"])
|
2020-12-14 22:02:22 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
granted_text = "have" if options["grant"] else "not have"
|
2021-02-12 08:19:30 +01:00
|
|
|
print(
|
|
|
|
f"{user.delivery_email} changed to {granted_text} {options['new_role']} permission."
|
|
|
|
)
|
2020-12-14 22:02:22 +01:00
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
if user.can_create_users and options["grant"]:
|
2020-12-14 22:02:22 +01:00
|
|
|
raise CommandError("User can already create users for this realm.")
|
2021-02-12 08:20:45 +01:00
|
|
|
elif not user.can_create_users and not options["grant"]:
|
2020-12-14 22:02:22 +01:00
|
|
|
raise CommandError("User can't create users for this realm.")
|
2021-02-12 08:20:45 +01:00
|
|
|
do_change_can_create_users(user, options["grant"])
|