2013-11-07 20:17:54 +01:00
|
|
|
from __future__ import absolute_import
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
2013-11-07 20:17:54 +01:00
|
|
|
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from argparse import ArgumentParser
|
2017-01-21 08:27:36 +01:00
|
|
|
from django.core.exceptions import ValidationError
|
2013-11-07 20:17:54 +01:00
|
|
|
from django.core.management.base import BaseCommand
|
2017-01-21 08:19:03 +01:00
|
|
|
from django.db.utils import IntegrityError
|
|
|
|
from django.utils.translation import ugettext as _
|
2017-03-31 17:03:42 +02:00
|
|
|
from zerver.models import get_realm, can_add_realm_domain, \
|
2017-04-29 06:06:57 +02:00
|
|
|
Realm, RealmDomain, get_realm_domains
|
2017-01-21 08:27:36 +01:00
|
|
|
from zerver.lib.domains import validate_domain
|
2013-11-07 20:17:54 +01:00
|
|
|
import sys
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2017-03-31 20:48:54 +02:00
|
|
|
help = """Manage domains for the specified realm"""
|
2015-08-21 02:10:41 +02:00
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (ArgumentParser) -> None
|
2015-08-21 02:10:41 +02:00
|
|
|
parser.add_argument('-r', '--realm',
|
2016-11-16 21:16:02 +01:00
|
|
|
dest='string_id',
|
2015-08-21 02:10:41 +02:00
|
|
|
type=str,
|
|
|
|
required=True,
|
2016-11-16 21:16:02 +01:00
|
|
|
help='The subdomain or string_id of the realm.')
|
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")
|
2013-11-07 20:17:54 +01:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (*Any, **str) -> None
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm(options["string_id"])
|
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:
|
|
|
|
print(e.messages[0])
|
|
|
|
sys.exit(1)
|
2013-11-07 20:17:54 +01:00
|
|
|
if options["op"] == "add":
|
2017-01-21 08:19:03 +01:00
|
|
|
try:
|
2017-03-31 17:03:42 +02:00
|
|
|
if not can_add_realm_domain(domain):
|
2017-01-21 08:19:03 +01:00
|
|
|
print(_("The domain %(domain)s belongs to another organization.") % {'domain': domain})
|
|
|
|
sys.exit(1)
|
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:
|
|
|
|
print(_("The domain %(domain)s is already a part of your organization.") % {'domain': domain})
|
2013-11-07 20:17:54 +01:00
|
|
|
sys.exit(1)
|
|
|
|
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:
|
2017-01-21 08:19:03 +01:00
|
|
|
print("No such entry found!")
|
|
|
|
sys.exit(1)
|
2013-11-07 20:17:54 +01:00
|
|
|
else:
|
2017-03-31 20:51:05 +02:00
|
|
|
self.print_help("./manage.py", "realm_domain")
|
2013-11-07 20:17:54 +01:00
|
|
|
sys.exit(1)
|