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 _
|
|
|
|
from zerver.models import get_realm, can_add_alias, \
|
2017-03-31 16:20:07 +02:00
|
|
|
Realm, RealmDomain
|
2017-01-17 02:31:47 +01:00
|
|
|
from zerver.lib.actions import get_realm_aliases
|
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):
|
2015-08-21 02:10:41 +02:00
|
|
|
help = """Manage aliases for the specified realm"""
|
|
|
|
|
|
|
|
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.')
|
2015-08-21 02:10:41 +02:00
|
|
|
parser.add_argument('alias', metavar='<alias>', type=str, nargs='?',
|
|
|
|
help="alias 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-13 17:44:32 +01:00
|
|
|
print("Aliases for %s:" % (realm.string_id,))
|
2017-01-17 02:31:47 +01:00
|
|
|
for alias in get_realm_aliases(realm):
|
2017-01-21 08:19:03 +01:00
|
|
|
if alias["allow_subdomains"]:
|
|
|
|
print(alias["domain"] + " (subdomains allowed)")
|
|
|
|
else:
|
|
|
|
print(alias["domain"] + " (subdomains not allowed)")
|
2013-11-07 20:17:54 +01:00
|
|
|
sys.exit(0)
|
|
|
|
|
2017-01-21 08:27:36 +01:00
|
|
|
domain = options['alias'].strip().lower()
|
|
|
|
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:
|
|
|
|
if not can_add_alias(domain):
|
|
|
|
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:
|
2016-11-22 01:44:16 +01:00
|
|
|
self.print_help("./manage.py", "realm_alias")
|
2013-11-07 20:17:54 +01:00
|
|
|
sys.exit(1)
|