2016-12-26 19:19:02 +01:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
2017-10-28 00:07:31 +02:00
|
|
|
from zerver.decorator import require_realm_admin
|
2017-03-31 19:57:46 +02:00
|
|
|
from zerver.lib.actions import do_add_realm_domain, do_change_realm_domain, \
|
2017-04-29 06:06:57 +02:00
|
|
|
do_remove_realm_domain
|
2017-01-21 08:27:36 +01:00
|
|
|
from zerver.lib.domains import validate_domain
|
2017-10-28 00:07:31 +02:00
|
|
|
from zerver.lib.request import has_request_variables, REQ
|
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-10-02 08:04:12 +02:00
|
|
|
from zerver.models import RealmDomain, UserProfile, get_realm_domains
|
2016-12-26 19:19:02 +01:00
|
|
|
|
|
|
|
|
2017-10-27 02:18:49 +02:00
|
|
|
def list_realm_domains(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
|
2017-03-31 21:14:11 +02:00
|
|
|
domains = get_realm_domains(user_profile.realm)
|
|
|
|
return json_success({'domains': domains})
|
2016-12-26 19:19:02 +01:00
|
|
|
|
|
|
|
@require_realm_admin
|
|
|
|
@has_request_variables
|
2017-10-27 02:18:49 +02:00
|
|
|
def create_realm_domain(request: HttpRequest, user_profile: UserProfile,
|
2018-04-24 03:47:28 +02:00
|
|
|
domain: str=REQ(validator=check_string),
|
2017-10-27 02:18:49 +02:00
|
|
|
allow_subdomains: bool=REQ(validator=check_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-11-05 03:14:28 +01:00
|
|
|
return json_error(_("The domain %(domain)s is already"
|
|
|
|
" a part of your organization.") % {'domain': domain})
|
2017-03-31 21:14:11 +02:00
|
|
|
realm_domain = do_add_realm_domain(user_profile.realm, domain, allow_subdomains)
|
|
|
|
return json_success({'new_domain': [realm_domain.id, realm_domain.domain]})
|
2016-12-26 19:19:02 +01:00
|
|
|
|
2017-01-21 08:19:03 +01:00
|
|
|
@require_realm_admin
|
|
|
|
@has_request_variables
|
2018-04-24 03:47:28 +02:00
|
|
|
def patch_realm_domain(request: HttpRequest, user_profile: UserProfile, domain: str,
|
2017-10-27 02:18:49 +02:00
|
|
|
allow_subdomains: bool=REQ(validator=check_bool)) -> HttpResponse:
|
2017-01-21 08:19:03 +01:00
|
|
|
try:
|
2017-03-31 21:14:11 +02:00
|
|
|
realm_domain = RealmDomain.objects.get(realm=user_profile.realm, domain=domain)
|
|
|
|
do_change_realm_domain(realm_domain, 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-10-27 02:18:49 +02:00
|
|
|
def delete_realm_domain(request: HttpRequest, user_profile: UserProfile,
|
2018-04-24 03:47:28 +02:00
|
|
|
domain: str) -> HttpResponse:
|
2016-12-26 19:19:02 +01:00
|
|
|
try:
|
2017-03-31 21:14:11 +02:00
|
|
|
realm_domain = RealmDomain.objects.get(realm=user_profile.realm, domain=domain)
|
|
|
|
do_remove_realm_domain(realm_domain)
|
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()
|