2016-12-26 19:19:02 +01:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
2017-01-21 08:19:03 +01:00
|
|
|
from zerver.decorator import has_request_variables, require_realm_admin, REQ
|
2017-03-31 19:53:34 +02:00
|
|
|
from zerver.lib.actions import do_add_realm_domain, do_change_realm_alias, \
|
2017-03-31 19:46:43 +02:00
|
|
|
do_remove_realm_alias, get_realm_domains
|
2017-01-21 08:27:36 +01:00
|
|
|
from zerver.lib.domains import validate_domain
|
2016-12-26 19:19:02 +01:00
|
|
|
from zerver.lib.response import json_error, json_success
|
2017-01-21 08:19:03 +01:00
|
|
|
from zerver.lib.validator import check_bool, check_string
|
2017-03-31 17:03:42 +02:00
|
|
|
from zerver.models import can_add_realm_domain, RealmDomain, UserProfile
|
2016-12-26 19:19:02 +01:00
|
|
|
|
|
|
|
from typing import Text
|
|
|
|
|
|
|
|
def list_aliases(request, user_profile):
|
|
|
|
# type: (HttpRequest, UserProfile) -> (HttpResponse)
|
2017-03-31 19:46:43 +02:00
|
|
|
aliases = get_realm_domains(user_profile.realm)
|
2016-12-26 19:19:02 +01:00
|
|
|
return json_success({'domains': aliases})
|
|
|
|
|
|
|
|
@require_realm_admin
|
|
|
|
@has_request_variables
|
2017-01-21 08:19:03 +01:00
|
|
|
def create_alias(request, user_profile, domain=REQ(validator=check_string), allow_subdomains=REQ(validator=check_bool)):
|
|
|
|
# type: (HttpRequest, UserProfile, Text, bool) -> (HttpResponse)
|
2017-01-21 08:27:36 +01:00
|
|
|
domain = domain.strip().lower()
|
|
|
|
try:
|
|
|
|
validate_domain(domain)
|
|
|
|
except ValidationError as e:
|
|
|
|
return json_error(_('Invalid domain: {}').format(e.messages[0]))
|
2017-03-31 16:20:07 +02:00
|
|
|
if RealmDomain.objects.filter(realm=user_profile.realm, domain=domain).exists():
|
2017-01-21 08:27:36 +01:00
|
|
|
return json_error(_("The domain %(domain)s is already a part of your organization.") % {'domain': domain})
|
2017-03-31 17:03:42 +02:00
|
|
|
if not can_add_realm_domain(domain):
|
2017-01-21 08:27:36 +01:00
|
|
|
return json_error(_("The domain %(domain)s belongs to another organization.") % {'domain': domain})
|
2017-03-31 19:53:34 +02:00
|
|
|
alias = do_add_realm_domain(user_profile.realm, domain, allow_subdomains)
|
2016-12-26 19:19:02 +01:00
|
|
|
return json_success({'new_domain': [alias.id, alias.domain]})
|
|
|
|
|
2017-01-21 08:19:03 +01:00
|
|
|
@require_realm_admin
|
|
|
|
@has_request_variables
|
|
|
|
def patch_alias(request, user_profile, domain, allow_subdomains=REQ(validator=check_bool)):
|
|
|
|
# type: (HttpRequest, UserProfile, Text, bool) -> (HttpResponse)
|
|
|
|
try:
|
2017-03-31 16:20:07 +02:00
|
|
|
alias = RealmDomain.objects.get(realm=user_profile.realm, domain=domain)
|
2017-01-21 08:19:03 +01:00
|
|
|
do_change_realm_alias(alias, allow_subdomains)
|
2017-03-31 16:20:07 +02:00
|
|
|
except RealmDomain.DoesNotExist:
|
2017-01-21 08:19:03 +01:00
|
|
|
return json_error(_('No entry found for domain %(domain)s.' % {'domain': domain}))
|
|
|
|
return json_success()
|
|
|
|
|
2016-12-26 19:19:02 +01:00
|
|
|
@require_realm_admin
|
|
|
|
@has_request_variables
|
2017-01-21 09:09:27 +01:00
|
|
|
def delete_alias(request, user_profile, domain):
|
|
|
|
# type: (HttpRequest, UserProfile, Text) -> (HttpResponse)
|
2016-12-26 19:19:02 +01:00
|
|
|
try:
|
2017-03-31 16:20:07 +02:00
|
|
|
alias = RealmDomain.objects.get(realm=user_profile.realm, domain=domain)
|
2017-01-23 04:18:05 +01:00
|
|
|
do_remove_realm_alias(alias)
|
2017-03-31 16:20:07 +02:00
|
|
|
except RealmDomain.DoesNotExist:
|
2017-01-21 09:09:27 +01:00
|
|
|
return json_error(_('No entry found for domain %(domain)s.' % {'domain': domain}))
|
2016-12-26 19:19:02 +01:00
|
|
|
return json_success()
|