2016-12-26 19:19:02 +01:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2016-12-26 19:19:02 +01:00
|
|
|
|
2022-04-14 23:57:26 +02:00
|
|
|
from zerver.actions.realm_domains import (
|
|
|
|
do_add_realm_domain,
|
|
|
|
do_change_realm_domain,
|
|
|
|
do_remove_realm_domain,
|
|
|
|
)
|
2021-07-29 09:10:33 +02:00
|
|
|
from zerver.decorator import require_realm_owner
|
2017-01-21 08:27:36 +01:00
|
|
|
from zerver.lib.domains import validate_domain
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.response import json_success
|
2021-05-04 16:31:26 +02:00
|
|
|
from zerver.lib.validator import check_bool
|
2023-12-15 02:14:24 +01:00
|
|
|
from zerver.models import RealmDomain, UserProfile
|
|
|
|
from zerver.models.realms import 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)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data={"domains": domains})
|
2016-12-26 19:19:02 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-07-29 09:10:33 +02:00
|
|
|
@require_realm_owner
|
2016-12-26 19:19:02 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def create_realm_domain(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2021-05-04 16:31:26 +02:00
|
|
|
domain: str = REQ(),
|
2021-04-07 22:00:44 +02:00
|
|
|
allow_subdomains: bool = REQ(json_validator=check_bool),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2017-01-21 08:27:36 +01:00
|
|
|
domain = domain.strip().lower()
|
|
|
|
try:
|
|
|
|
validate_domain(domain)
|
|
|
|
except ValidationError as e:
|
2023-07-17 22:40:33 +02:00
|
|
|
raise JsonableError(_("Invalid domain: {error}").format(error=e.messages[0]))
|
2017-03-31 16:20:07 +02:00
|
|
|
if RealmDomain.objects.filter(realm=user_profile.realm, domain=domain).exists():
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(
|
2021-02-12 08:19:30 +01:00
|
|
|
_("The domain {domain} is already a part of your organization.").format(domain=domain)
|
|
|
|
)
|
2022-03-07 14:49:16 +01:00
|
|
|
realm_domain = do_add_realm_domain(
|
|
|
|
user_profile.realm, domain, allow_subdomains, acting_user=user_profile
|
|
|
|
)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data={"new_domain": [realm_domain.id, realm_domain.domain]})
|
2016-12-26 19:19:02 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-07-29 09:10:33 +02:00
|
|
|
@require_realm_owner
|
2017-01-21 08:19:03 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def patch_realm_domain(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
domain: str,
|
2021-04-07 22:00:44 +02:00
|
|
|
allow_subdomains: bool = REQ(json_validator=check_bool),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> 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)
|
2022-03-07 15:19:13 +01:00
|
|
|
do_change_realm_domain(realm_domain, allow_subdomains, acting_user=user_profile)
|
2017-03-31 16:20:07 +02:00
|
|
|
except RealmDomain.DoesNotExist:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("No entry found for domain {domain}.").format(domain=domain))
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2017-01-21 08:19:03 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-07-29 09:10:33 +02:00
|
|
|
@require_realm_owner
|
2016-12-26 19:19:02 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def delete_realm_domain(
|
|
|
|
request: HttpRequest, user_profile: UserProfile, 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)
|
2020-06-29 15:34:19 +02:00
|
|
|
do_remove_realm_domain(realm_domain, acting_user=user_profile)
|
2017-03-31 16:20:07 +02:00
|
|
|
except RealmDomain.DoesNotExist:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("No entry found for domain {domain}.").format(domain=domain))
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|