management: Validate string_id when creating or renaming a realm.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2023-08-23 15:00:21 -07:00 committed by Tim Abbott
parent 124c5d02e5
commit d26a94a0db
2 changed files with 28 additions and 1 deletions

View File

@ -1,7 +1,11 @@
from argparse import ArgumentParser
from typing import Any
from django.core.exceptions import ValidationError
from django.core.management.base import CommandError
from zerver.actions.create_realm import do_change_realm_subdomain
from zerver.forms import check_subdomain_available
from zerver.lib.management import ZulipBaseCommand
@ -16,12 +20,22 @@ class Command(ZulipBaseCommand):
action="store_true",
help="Do not redirect the old name to the new one",
)
parser.add_argument(
"--allow-reserved-subdomain",
action="store_true",
help="Allow use of reserved subdomains",
)
def handle(self, *args: Any, **options: Any) -> None:
realm = self.get_realm(options)
assert realm is not None # Should be ensured by parser
new_subdomain = options["new_subdomain"]
allow_reserved_subdomain = options["allow_reserved_subdomain"]
try:
check_subdomain_available(new_subdomain, allow_reserved_subdomain)
except ValidationError as error:
raise CommandError(error.message)
do_change_realm_subdomain(
realm,
new_subdomain,

View File

@ -1,10 +1,12 @@
import argparse
from typing import Any
from django.core.exceptions import ValidationError
from django.core.management.base import CommandError
from zerver.actions.create_realm import do_create_realm
from zerver.actions.create_user import do_create_user
from zerver.forms import check_subdomain_available
from zerver.lib.management import ZulipBaseCommand
from zerver.models import UserProfile
@ -35,11 +37,22 @@ workflow as `./manage.py create_user`.
help="Subdomain for the new organization. Empty if root domain.",
default="",
)
parser.add_argument(
"--allow-reserved-subdomain",
action="store_true",
help="Allow use of reserved subdomains",
)
self.add_create_user_args(parser)
def handle(self, *args: Any, **options: str) -> None:
def handle(self, *args: Any, **options: Any) -> None:
realm_name = options["realm_name"]
string_id = options["string_id"]
allow_reserved_subdomain = options["allow_reserved_subdomain"]
try:
check_subdomain_available(string_id, allow_reserved_subdomain)
except ValidationError as error:
raise CommandError(error.message)
create_user_params = self.get_create_user_params(options)