Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

management: Validate string_id when creating or renaming a realm.

Anders Kaseorgd26a94a0db4b
diff --git a/zerver/management/commands/change_realm_subdomain.py b/zerver/management/commands/change_realm_subdomain.py
index fc3dd9a0596..c9b98a39af2 100644
--- a/zerver/management/commands/change_realm_subdomain.py
+++ b/zerver/management/commands/change_realm_subdomain.py
@@ -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,
diff --git a/zerver/management/commands/create_realm.py b/zerver/management/commands/create_realm.py
index 62f94483659..28b3f927e3e 100644
--- a/zerver/management/commands/create_realm.py
+++ b/zerver/management/commands/create_realm.py
@@ -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)