2017-11-16 00:43:27 +01:00
|
|
|
import sys
|
|
|
|
from argparse import ArgumentParser
|
2024-07-12 02:30:23 +02:00
|
|
|
from typing import Any
|
2016-06-04 16:52:18 +02:00
|
|
|
|
2017-01-21 08:27:36 +01:00
|
|
|
from django.core.exceptions import ValidationError
|
2021-07-16 22:11:10 +02:00
|
|
|
from django.core.management.base import CommandError
|
2017-01-21 08:19:03 +01:00
|
|
|
from django.db.utils import IntegrityError
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
2017-11-16 00:43:27 +01:00
|
|
|
|
2017-01-21 08:27:36 +01:00
|
|
|
from zerver.lib.domains import validate_domain
|
2021-07-16 22:11:10 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2023-12-15 02:14:24 +01:00
|
|
|
from zerver.models import RealmDomain
|
|
|
|
from zerver.models.realms import get_realm_domains
|
2013-11-07 20:17:54 +01:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2017-08-07 22:02:42 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2017-03-31 20:48:54 +02:00
|
|
|
help = """Manage domains for the specified realm"""
|
2015-08-21 02:10:41 +02:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"--op", default="show", help="What operation to do (add, show, remove)."
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"--allow-subdomains", action="store_true", help="Whether subdomains are allowed or not."
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
parser.add_argument("domain", metavar="<domain>", nargs="?", help="domain to add or remove")
|
2021-05-10 21:29:25 +02:00
|
|
|
self.add_realm_args(parser, required=True)
|
2013-11-07 20:17:54 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2024-07-12 02:30:23 +02:00
|
|
|
def handle(self, *args: Any, **options: str | bool) -> None:
|
2017-08-07 22:02:42 +02:00
|
|
|
realm = self.get_realm(options)
|
2017-09-26 01:25:39 +02:00
|
|
|
assert realm is not None # Should be ensured by parser
|
2013-11-07 20:17:54 +01:00
|
|
|
if options["op"] == "show":
|
2020-06-10 06:41:04 +02:00
|
|
|
print(f"Domains for {realm.string_id}:")
|
2017-03-31 20:48:54 +02:00
|
|
|
for realm_domain in get_realm_domains(realm):
|
2022-03-10 18:06:05 +01:00
|
|
|
assert isinstance(realm_domain["domain"], str)
|
2017-03-31 20:48:54 +02:00
|
|
|
if realm_domain["allow_subdomains"]:
|
|
|
|
print(realm_domain["domain"] + " (subdomains allowed)")
|
2017-01-21 08:19:03 +01:00
|
|
|
else:
|
2017-03-31 20:48:54 +02:00
|
|
|
print(realm_domain["domain"] + " (subdomains not allowed)")
|
2013-11-07 20:17:54 +01:00
|
|
|
sys.exit(0)
|
|
|
|
|
2021-08-14 16:51:57 +02:00
|
|
|
assert isinstance(options["domain"], str)
|
2021-02-12 08:20:45 +01:00
|
|
|
domain = options["domain"].strip().lower()
|
2017-01-21 08:27:36 +01:00
|
|
|
try:
|
|
|
|
validate_domain(domain)
|
|
|
|
except ValidationError as e:
|
2019-05-03 23:20:39 +02:00
|
|
|
raise CommandError(e.messages[0])
|
2013-11-07 20:17:54 +01:00
|
|
|
if options["op"] == "add":
|
2021-08-14 16:51:57 +02:00
|
|
|
assert isinstance(options["allow_subdomains"], bool)
|
2017-01-21 08:19:03 +01:00
|
|
|
try:
|
2021-02-12 08:19:30 +01:00
|
|
|
RealmDomain.objects.create(
|
|
|
|
realm=realm, domain=domain, allow_subdomains=options["allow_subdomains"]
|
|
|
|
)
|
2017-01-21 08:19:03 +01:00
|
|
|
sys.exit(0)
|
|
|
|
except IntegrityError:
|
2021-02-12 03:52:14 +01:00
|
|
|
raise CommandError(f"The domain {domain} is already a part of your organization.")
|
2013-11-07 20:17:54 +01:00
|
|
|
elif options["op"] == "remove":
|
2017-01-21 08:19:03 +01:00
|
|
|
try:
|
2017-03-31 16:20:07 +02:00
|
|
|
RealmDomain.objects.get(realm=realm, domain=domain).delete()
|
2017-01-21 08:19:03 +01:00
|
|
|
sys.exit(0)
|
2017-03-31 16:20:07 +02:00
|
|
|
except RealmDomain.DoesNotExist:
|
2019-05-03 23:20:39 +02:00
|
|
|
raise CommandError("No such entry found!")
|
2013-11-07 20:17:54 +01:00
|
|
|
else:
|
2017-03-31 20:51:05 +02:00
|
|
|
self.print_help("./manage.py", "realm_domain")
|
2019-05-03 23:20:39 +02:00
|
|
|
raise CommandError
|