コード例 #1
0
ファイル: realm_domains.py プロジェクト: brockwhittaker/zulip
def create_realm_domain(request: HttpRequest, user_profile: UserProfile,
                        domain: Text=REQ(validator=check_string),
                        allow_subdomains: bool=REQ(validator=check_bool)) -> HttpResponse:
    domain = domain.strip().lower()
    try:
        validate_domain(domain)
    except ValidationError as e:
        return json_error(_('Invalid domain: {}').format(e.messages[0]))
    if RealmDomain.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})
    realm_domain = do_add_realm_domain(user_profile.realm, domain, allow_subdomains)
    return json_success({'new_domain': [realm_domain.id, realm_domain.domain]})
コード例 #2
0
ファイル: realm_domains.py プロジェクト: vfseanm/zulip
def create_realm_domain(request, user_profile, domain=REQ(validator=check_string), allow_subdomains=REQ(validator=check_bool)):
    # type: (HttpRequest, UserProfile, Text, bool) -> (HttpResponse)
    domain = domain.strip().lower()
    try:
        validate_domain(domain)
    except ValidationError as e:
        return json_error(_('Invalid domain: {}').format(e.messages[0]))
    if RealmDomain.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_realm_domain(domain):
        return json_error(_("The domain %(domain)s belongs to another organization.") % {'domain': domain})
    realm_domain = do_add_realm_domain(user_profile.realm, domain, allow_subdomains)
    return json_success({'new_domain': [realm_domain.id, realm_domain.domain]})
コード例 #3
0
def create_realm_domain(request: HttpRequest, user_profile: UserProfile,
                        domain: str=REQ(validator=check_string),
                        allow_subdomains: bool=REQ(validator=check_bool)) -> HttpResponse:
    domain = domain.strip().lower()
    try:
        validate_domain(domain)
    except ValidationError as e:
        return json_error(_('Invalid domain: {}').format(e.messages[0]))
    if RealmDomain.objects.filter(realm=user_profile.realm, domain=domain).exists():
        return json_error(_("The domain {domain} is already"
                            " a part of your organization.").format(domain=domain))
    realm_domain = do_add_realm_domain(user_profile.realm, domain, allow_subdomains)
    return json_success({'new_domain': [realm_domain.id, realm_domain.domain]})
コード例 #4
0
def create_realm_domain(
        request: HttpRequest,
        user_profile: UserProfile,
        domain: str = REQ(),
        allow_subdomains: bool = REQ(json_validator=check_bool),
) -> HttpResponse:
    domain = domain.strip().lower()
    try:
        validate_domain(domain)
    except ValidationError as e:
        raise JsonableError(_("Invalid domain: {}").format(e.messages[0]))
    if RealmDomain.objects.filter(realm=user_profile.realm,
                                  domain=domain).exists():
        raise JsonableError(
            _("The domain {domain} is already a part of your organization.").
            format(domain=domain))
    realm_domain = do_add_realm_domain(user_profile.realm,
                                       domain,
                                       allow_subdomains,
                                       acting_user=user_profile)
    return json_success(
        request, data={"new_domain": [realm_domain.id, realm_domain.domain]})
コード例 #5
0
    def test_realm_domain_entries(self) -> None:
        user = self.example_user("iago")
        initial_domains = get_realm_domains(user.realm)

        now = timezone_now()
        realm_domain = do_add_realm_domain(user.realm,
                                           "zulip.org",
                                           False,
                                           acting_user=user)
        added_domain: Dict[str, Union[str, bool]] = {
            "domain": "zulip.org",
            "allow_subdomains": False,
        }
        expected_extra_data = {
            "realm_domains": initial_domains + [added_domain],
            "added_domain": added_domain,
        }
        self.assertEqual(
            RealmAuditLog.objects.filter(
                realm=user.realm,
                event_type=RealmAuditLog.REALM_DOMAIN_ADDED,
                event_time__gte=now,
                acting_user=user,
                extra_data=orjson.dumps(expected_extra_data).decode(),
            ).count(),
            1,
        )

        now = timezone_now()
        do_change_realm_domain(realm_domain, True, acting_user=user)
        changed_domain: Dict[str, Union[str, bool]] = {
            "domain": "zulip.org",
            "allow_subdomains": True,
        }
        expected_extra_data = {
            "realm_domains": initial_domains + [changed_domain],
            "changed_domain": changed_domain,
        }
        self.assertEqual(
            RealmAuditLog.objects.filter(
                realm=user.realm,
                event_type=RealmAuditLog.REALM_DOMAIN_CHANGED,
                event_time__gte=now,
                acting_user=user,
                extra_data=orjson.dumps(expected_extra_data).decode(),
            ).count(),
            1,
        )

        now = timezone_now()
        do_remove_realm_domain(realm_domain, acting_user=user)
        removed_domain = {
            "domain": "zulip.org",
            "allow_subdomains": True,
        }
        expected_extra_data = {
            "realm_domains": initial_domains,
            "removed_domain": removed_domain,
        }
        self.assertEqual(
            RealmAuditLog.objects.filter(
                realm=user.realm,
                event_type=RealmAuditLog.REALM_DOMAIN_REMOVED,
                event_time__gte=now,
                acting_user=user,
                extra_data=orjson.dumps(expected_extra_data).decode(),
            ).count(),
            1,
        )