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 _
|
|
|
|
|
|
|
|
from zerver.decorator import has_request_variables, REQ, require_realm_admin
|
2017-01-17 02:31:47 +01:00
|
|
|
from zerver.lib.actions import get_realm_aliases, do_add_realm_alias, \
|
2016-12-26 19:19:02 +01:00
|
|
|
do_remove_realm_alias
|
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:27:36 +01:00
|
|
|
from zerver.lib.validator import check_string
|
2016-12-26 19:19:02 +01:00
|
|
|
from zerver.models import can_add_alias, RealmAlias, UserProfile
|
|
|
|
|
|
|
|
from typing import Text
|
|
|
|
|
|
|
|
def list_aliases(request, user_profile):
|
|
|
|
# type: (HttpRequest, UserProfile) -> (HttpResponse)
|
2017-01-17 02:31:47 +01:00
|
|
|
aliases = get_realm_aliases(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:27:36 +01:00
|
|
|
def create_alias(request, user_profile, domain=REQ(validator=check_string)):
|
2016-12-26 19:19:02 +01:00
|
|
|
# type: (HttpRequest, UserProfile, Text) -> (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]))
|
|
|
|
if RealmAlias.objects.filter(realm=user_profile.realm, domain=domain).exists():
|
|
|
|
return json_error(_("The domain %(domain)s is already a part of your organization.") % {'domain': domain})
|
|
|
|
if not can_add_alias(domain):
|
|
|
|
return json_error(_("The domain %(domain)s belongs to another organization.") % {'domain': domain})
|
|
|
|
alias = do_add_realm_alias(user_profile.realm, domain)
|
2016-12-26 19:19:02 +01:00
|
|
|
return json_success({'new_domain': [alias.id, alias.domain]})
|
|
|
|
|
|
|
|
@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-01-21 09:09:27 +01:00
|
|
|
RealmAlias.objects.get(realm=user_profile.realm, domain=domain)
|
|
|
|
do_remove_realm_alias(user_profile.realm, domain)
|
2016-12-26 19:19:02 +01:00
|
|
|
except RealmAlias.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()
|