2013-11-07 20:17:54 +01:00
|
|
|
|
2017-11-16 00:43:27 +01:00
|
|
|
import sys
|
|
|
|
from argparse import ArgumentParser
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2017-01-21 08:27:36 +01:00
|
|
|
from django.core.exceptions import ValidationError
|
2017-01-21 08:19:03 +01:00
|
|
|
from django.db.utils import IntegrityError
|
2017-11-16 00:43:27 +01:00
|
|
|
|
2017-01-21 08:27:36 +01:00
|
|
|
from zerver.lib.domains import validate_domain
|
2019-05-03 23:20:39 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand, CommandError
|
2017-11-16 00:43:27 +01:00
|
|
|
from zerver.models import RealmDomain, get_realm_domains
|
2013-11-07 20:17:54 +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
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2015-08-21 02:10:41 +02:00
|
|
|
parser.add_argument('--op',
|
|
|
|
dest='op',
|
|
|
|
type=str,
|
|
|
|
default="show",
|
|
|
|
help='What operation to do (add, show, remove).')
|
2017-01-21 08:19:03 +01:00
|
|
|
parser.add_argument('--allow-subdomains',
|
|
|
|
dest='allow_subdomains',
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help='Whether subdomains are allowed or not.')
|
2017-03-31 20:48:54 +02:00
|
|
|
parser.add_argument('domain', metavar='<domain>', type=str, nargs='?',
|
|
|
|
help="domain to add or remove")
|
2017-08-07 22:02:42 +02:00
|
|
|
self.add_realm_args(parser, True)
|
2013-11-07 20:17:54 +01:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: str) -> 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":
|
2017-03-31 20:48:54 +02:00
|
|
|
print("Domains for %s:" % (realm.string_id,))
|
|
|
|
for realm_domain in get_realm_domains(realm):
|
|
|
|
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)
|
|
|
|
|
2017-03-31 20:48:54 +02: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":
|
2017-01-21 08:19:03 +01:00
|
|
|
try:
|
2017-03-31 16:20:07 +02: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:
|
2019-05-03 23:20:39 +02:00
|
|
|
raise CommandError("The domain %(domain)s is already a part "
|
|
|
|
"of your organization." % {'domain': domain})
|
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
|