2024-07-12 02:30:25 +02:00
|
|
|
from collections.abc import Mapping
|
2024-07-12 17:49:28 +02:00
|
|
|
from typing import Annotated, Any, Literal
|
2020-06-11 00:54:34 +02:00
|
|
|
|
|
|
|
from django.core.exceptions import ValidationError
|
2024-05-23 16:21:25 +02:00
|
|
|
from django.db import transaction
|
2016-07-26 23:16:20 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2018-11-12 14:15:49 +01:00
|
|
|
from django.shortcuts import render
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2019-08-12 05:44:35 +02:00
|
|
|
from django.views.decorators.http import require_safe
|
2024-05-17 19:44:28 +02:00
|
|
|
from pydantic import Json, NonNegativeInt, StringConstraints
|
|
|
|
from pydantic.functional_validators import AfterValidator
|
2018-01-25 19:08:40 +01:00
|
|
|
|
2022-11-17 09:30:48 +01:00
|
|
|
from confirmation.models import Confirmation, ConfirmationKeyError, get_object_from_key
|
2022-04-14 23:58:15 +02:00
|
|
|
from zerver.actions.create_realm import do_change_realm_subdomain
|
2022-04-14 23:57:15 +02:00
|
|
|
from zerver.actions.realm_settings import (
|
2022-04-11 19:26:16 +02:00
|
|
|
do_change_realm_org_type,
|
2023-08-09 15:06:56 +02:00
|
|
|
do_change_realm_permission_group_setting,
|
2020-06-11 00:54:34 +02:00
|
|
|
do_deactivate_realm,
|
|
|
|
do_reactivate_realm,
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_authentication_methods,
|
2024-02-07 12:13:02 +01:00
|
|
|
do_set_realm_new_stream_announcements_stream,
|
2017-03-21 18:08:40 +01:00
|
|
|
do_set_realm_property,
|
2024-02-07 17:11:43 +01:00
|
|
|
do_set_realm_signup_announcements_stream,
|
2021-07-21 13:23:23 +02:00
|
|
|
do_set_realm_user_default_setting,
|
2024-01-26 14:45:37 +01:00
|
|
|
do_set_realm_zulip_update_announcements_stream,
|
2022-10-12 09:14:27 +02:00
|
|
|
parse_and_set_setting_value_if_required,
|
2024-02-02 23:32:27 +01:00
|
|
|
validate_authentication_methods_dict_from_api,
|
2016-07-26 23:16:20 +02:00
|
|
|
)
|
2022-04-14 23:57:15 +02:00
|
|
|
from zerver.decorator import require_realm_admin, require_realm_owner
|
|
|
|
from zerver.forms import check_subdomain_available as check_subdomain
|
2022-11-17 09:30:48 +01:00
|
|
|
from zerver.lib.exceptions import JsonableError, OrganizationOwnerRequiredError
|
2016-08-04 17:32:41 +02:00
|
|
|
from zerver.lib.i18n import get_available_language_codes
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.response import json_success
|
2020-06-21 11:14:35 +02:00
|
|
|
from zerver.lib.retention import parse_message_retention_days
|
2017-06-09 20:50:38 +02:00
|
|
|
from zerver.lib.streams import access_stream_by_id
|
2024-07-12 17:49:28 +02:00
|
|
|
from zerver.lib.typed_endpoint import (
|
|
|
|
ApiParamConfig,
|
|
|
|
typed_endpoint,
|
|
|
|
typed_endpoint_without_parameters,
|
|
|
|
)
|
|
|
|
from zerver.lib.typed_endpoint_validators import check_int_in_validator, check_string_in_validator
|
2024-05-23 16:21:25 +02:00
|
|
|
from zerver.lib.user_groups import (
|
|
|
|
GroupSettingChangeRequest,
|
|
|
|
access_user_group_for_setting,
|
|
|
|
get_group_setting_value_for_api,
|
|
|
|
parse_group_setting_value,
|
|
|
|
validate_group_setting_value_change,
|
|
|
|
)
|
2024-07-12 17:49:28 +02:00
|
|
|
from zerver.lib.validator import check_capped_url, check_string
|
2022-07-26 15:48:26 +02:00
|
|
|
from zerver.models import Realm, RealmReactivationStatus, RealmUserDefault, UserProfile
|
2024-05-17 19:44:28 +02:00
|
|
|
from zerver.models.realms import (
|
|
|
|
BotCreationPolicyEnum,
|
|
|
|
CommonPolicyEnum,
|
|
|
|
DigestWeekdayEnum,
|
|
|
|
EditTopicPolicyEnum,
|
|
|
|
InviteToRealmPolicyEnum,
|
|
|
|
OrgTypeEnum,
|
|
|
|
WildcardMentionPolicyEnum,
|
|
|
|
)
|
2024-07-18 11:05:37 +02:00
|
|
|
from zerver.views.user_settings import (
|
|
|
|
check_information_density_setting_values,
|
|
|
|
check_settings_values,
|
|
|
|
)
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2017-03-21 18:08:40 +01:00
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
def parse_jitsi_server_url(value: str, special_values_map: Mapping[str, str | None]) -> str | None:
|
2023-09-19 19:03:08 +02:00
|
|
|
if value in special_values_map:
|
|
|
|
return special_values_map[value]
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
2023-12-14 16:44:18 +01:00
|
|
|
JITSI_SERVER_URL_MAX_LENGTH = 200
|
|
|
|
|
|
|
|
|
2024-05-17 19:44:28 +02:00
|
|
|
def check_jitsi_url(value: str) -> str:
|
|
|
|
var_name = "jitsi_server_url"
|
|
|
|
value = check_string(var_name, value)
|
|
|
|
|
|
|
|
if value in list(Realm.JITSI_SERVER_SPECIAL_VALUES_MAP.keys()):
|
|
|
|
return value
|
|
|
|
|
|
|
|
validator = check_capped_url(JITSI_SERVER_URL_MAX_LENGTH)
|
|
|
|
try:
|
|
|
|
return validator(var_name, value)
|
|
|
|
except ValidationError:
|
|
|
|
raise JsonableError(_("{var_name} is not an allowed_type").format(var_name=var_name))
|
|
|
|
|
|
|
|
|
2016-07-26 23:16:20 +02:00
|
|
|
@require_realm_admin
|
2024-05-17 19:44:28 +02:00
|
|
|
@typed_endpoint
|
2017-12-06 12:25:18 +01:00
|
|
|
def update_realm(
|
2021-02-12 08:19:30 +01:00
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2024-05-17 19:44:28 +02:00
|
|
|
*,
|
2024-07-12 02:30:23 +02:00
|
|
|
name: Annotated[str | None, StringConstraints(max_length=Realm.MAX_REALM_NAME_LENGTH)] = None,
|
2024-05-17 19:44:28 +02:00
|
|
|
description: Annotated[
|
2024-07-12 02:30:23 +02:00
|
|
|
str | None, StringConstraints(max_length=Realm.MAX_REALM_DESCRIPTION_LENGTH)
|
2024-05-17 19:44:28 +02:00
|
|
|
] = None,
|
2024-07-12 02:30:23 +02:00
|
|
|
emails_restricted_to_domains: Json[bool] | None = None,
|
|
|
|
disallow_disposable_email_addresses: Json[bool] | None = None,
|
|
|
|
invite_required: Json[bool] | None = None,
|
|
|
|
invite_to_realm_policy: Json[InviteToRealmPolicyEnum] | None = None,
|
2024-10-22 11:48:29 +02:00
|
|
|
create_multiuse_invite_group: Json[GroupSettingChangeRequest] | None = None,
|
2024-07-12 02:30:23 +02:00
|
|
|
require_unique_names: Json[bool] | None = None,
|
|
|
|
name_changes_disabled: Json[bool] | None = None,
|
|
|
|
email_changes_disabled: Json[bool] | None = None,
|
|
|
|
avatar_changes_disabled: Json[bool] | None = None,
|
|
|
|
inline_image_preview: Json[bool] | None = None,
|
|
|
|
inline_url_embed_preview: Json[bool] | None = None,
|
2024-10-13 09:09:11 +02:00
|
|
|
can_add_custom_emoji_group: Json[GroupSettingChangeRequest] | None = None,
|
2024-08-15 07:29:15 +02:00
|
|
|
can_delete_any_message_group: Json[GroupSettingChangeRequest] | None = None,
|
2024-09-11 19:13:37 +02:00
|
|
|
can_delete_own_message_group: Json[GroupSettingChangeRequest] | None = None,
|
2024-05-17 19:44:28 +02:00
|
|
|
message_content_delete_limit_seconds_raw: Annotated[
|
2024-07-12 02:30:23 +02:00
|
|
|
Json[int | str] | None,
|
2024-05-17 19:44:28 +02:00
|
|
|
ApiParamConfig("message_content_delete_limit_seconds"),
|
|
|
|
] = None,
|
2024-07-12 02:30:23 +02:00
|
|
|
allow_message_editing: Json[bool] | None = None,
|
|
|
|
edit_topic_policy: Json[EditTopicPolicyEnum] | None = None,
|
|
|
|
mandatory_topics: Json[bool] | None = None,
|
2024-05-17 19:44:28 +02:00
|
|
|
message_content_edit_limit_seconds_raw: Annotated[
|
2024-07-12 02:30:23 +02:00
|
|
|
Json[int | str] | None, ApiParamConfig("message_content_edit_limit_seconds")
|
2024-05-17 19:44:28 +02:00
|
|
|
] = None,
|
2024-07-12 02:30:23 +02:00
|
|
|
allow_edit_history: Json[bool] | None = None,
|
|
|
|
default_language: str | None = None,
|
|
|
|
waiting_period_threshold: Json[NonNegativeInt] | None = None,
|
|
|
|
authentication_methods: Json[dict[str, Any]] | None = None,
|
2023-11-23 22:07:41 +01:00
|
|
|
# Note: push_notifications_enabled and push_notifications_enabled_end_timestamp
|
|
|
|
# are not offered here as it is maintained by the server, not via the API.
|
2024-07-12 02:30:23 +02:00
|
|
|
new_stream_announcements_stream_id: Json[int] | None = None,
|
|
|
|
signup_announcements_stream_id: Json[int] | None = None,
|
|
|
|
zulip_update_announcements_stream_id: Json[int] | None = None,
|
2024-05-17 19:44:28 +02:00
|
|
|
message_retention_days_raw: Annotated[
|
2024-07-12 02:30:23 +02:00
|
|
|
Json[int | str] | None, ApiParamConfig("message_retention_days")
|
2024-05-17 19:44:28 +02:00
|
|
|
] = None,
|
2024-07-12 02:30:23 +02:00
|
|
|
send_welcome_emails: Json[bool] | None = None,
|
|
|
|
digest_emails_enabled: Json[bool] | None = None,
|
|
|
|
message_content_allowed_in_email_notifications: Json[bool] | None = None,
|
|
|
|
bot_creation_policy: Json[BotCreationPolicyEnum] | None = None,
|
2024-10-03 08:18:50 +02:00
|
|
|
can_create_groups: Json[GroupSettingChangeRequest] | None = None,
|
2024-07-12 02:30:23 +02:00
|
|
|
can_create_public_channel_group: Json[GroupSettingChangeRequest] | None = None,
|
|
|
|
can_create_private_channel_group: Json[GroupSettingChangeRequest] | None = None,
|
2024-07-01 16:48:16 +02:00
|
|
|
can_create_web_public_channel_group: Json[GroupSettingChangeRequest] | None = None,
|
2024-10-03 08:18:50 +02:00
|
|
|
can_manage_all_groups: Json[GroupSettingChangeRequest] | None = None,
|
2024-10-16 18:09:38 +02:00
|
|
|
can_move_messages_between_channels_group: Json[GroupSettingChangeRequest] | None = None,
|
2024-07-12 02:30:23 +02:00
|
|
|
direct_message_initiator_group: Json[GroupSettingChangeRequest] | None = None,
|
|
|
|
direct_message_permission_group: Json[GroupSettingChangeRequest] | None = None,
|
|
|
|
invite_to_stream_policy: Json[CommonPolicyEnum] | None = None,
|
|
|
|
wildcard_mention_policy: Json[WildcardMentionPolicyEnum] | None = None,
|
|
|
|
video_chat_provider: Json[int] | None = None,
|
2024-05-17 19:44:28 +02:00
|
|
|
jitsi_server_url_raw: Annotated[
|
2024-07-12 02:30:23 +02:00
|
|
|
Json[str] | None,
|
2024-05-17 19:44:28 +02:00
|
|
|
AfterValidator(lambda val: check_jitsi_url(val)),
|
|
|
|
ApiParamConfig("jitsi_server_url"),
|
|
|
|
] = None,
|
2024-07-12 02:30:23 +02:00
|
|
|
giphy_rating: Json[int] | None = None,
|
|
|
|
default_code_block_language: str | None = None,
|
|
|
|
digest_weekday: Json[DigestWeekdayEnum] | None = None,
|
2024-05-17 19:44:28 +02:00
|
|
|
string_id: Annotated[
|
2024-07-12 02:30:23 +02:00
|
|
|
str | None, StringConstraints(max_length=Realm.MAX_REALM_SUBDOMAIN_LENGTH)
|
2024-05-17 19:44:28 +02:00
|
|
|
] = None,
|
2024-07-12 02:30:23 +02:00
|
|
|
org_type: Json[OrgTypeEnum] | None = None,
|
|
|
|
enable_spectator_access: Json[bool] | None = None,
|
|
|
|
want_advertise_in_communities_directory: Json[bool] | None = None,
|
|
|
|
enable_read_receipts: Json[bool] | None = None,
|
2024-05-17 19:44:28 +02:00
|
|
|
move_messages_within_stream_limit_seconds_raw: Annotated[
|
2024-07-12 02:30:23 +02:00
|
|
|
Json[int | str] | None,
|
2024-05-17 19:44:28 +02:00
|
|
|
ApiParamConfig("move_messages_within_stream_limit_seconds"),
|
|
|
|
] = None,
|
|
|
|
move_messages_between_streams_limit_seconds_raw: Annotated[
|
2024-07-12 02:30:23 +02:00
|
|
|
Json[int | str] | None,
|
2024-05-17 19:44:28 +02:00
|
|
|
ApiParamConfig("move_messages_between_streams_limit_seconds"),
|
|
|
|
] = None,
|
2024-07-12 02:30:23 +02:00
|
|
|
enable_guest_user_indicator: Json[bool] | None = None,
|
2024-10-22 12:14:59 +02:00
|
|
|
can_access_all_users_group: Json[GroupSettingChangeRequest] | None = None,
|
2017-12-06 12:25:18 +01:00
|
|
|
) -> HttpResponse:
|
2024-07-16 21:00:59 +02:00
|
|
|
# Realm object is being refetched here to make sure that we
|
|
|
|
# do not use stale object from cache which can happen when a
|
|
|
|
# previous request tried updating multiple settings in a single
|
|
|
|
# request.
|
|
|
|
#
|
|
|
|
# TODO: Change the cache flushing strategy to make sure cache
|
|
|
|
# does not contain stale objects.
|
|
|
|
realm = Realm.objects.get(id=user_profile.realm_id)
|
2017-04-16 17:30:49 +02:00
|
|
|
|
|
|
|
# Additional validation/error checking beyond types go here, so
|
|
|
|
# the entire request can succeed or fail atomically.
|
2016-08-04 17:32:41 +02:00
|
|
|
if default_language is not None and default_language not in get_available_language_codes():
|
2023-07-17 22:40:33 +02:00
|
|
|
raise JsonableError(_("Invalid language '{language}'").format(language=default_language))
|
2020-06-11 16:24:15 +02:00
|
|
|
if authentication_methods is not None:
|
|
|
|
if not user_profile.is_realm_owner:
|
2023-02-04 02:07:20 +01:00
|
|
|
raise OrganizationOwnerRequiredError
|
2024-02-02 23:32:27 +01:00
|
|
|
|
|
|
|
validate_authentication_methods_dict_from_api(realm, authentication_methods)
|
2023-09-12 23:19:57 +02:00
|
|
|
if True not in authentication_methods.values():
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("At least one authentication method must be enabled."))
|
2024-02-02 23:32:27 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
if video_chat_provider is not None and video_chat_provider not in {
|
2021-02-12 08:20:45 +01:00
|
|
|
p["id"] for p in Realm.VIDEO_CHAT_PROVIDERS.values()
|
2021-02-12 08:19:30 +01:00
|
|
|
}:
|
2023-07-17 22:40:33 +02:00
|
|
|
raise JsonableError(
|
|
|
|
_("Invalid video_chat_provider {video_chat_provider}").format(
|
|
|
|
video_chat_provider=video_chat_provider
|
|
|
|
)
|
|
|
|
)
|
2021-03-31 13:10:46 +02:00
|
|
|
if giphy_rating is not None and giphy_rating not in {
|
|
|
|
p["id"] for p in Realm.GIPHY_RATING_OPTIONS.values()
|
|
|
|
}:
|
2023-07-17 22:40:33 +02:00
|
|
|
raise JsonableError(
|
|
|
|
_("Invalid giphy_rating {giphy_rating}").format(giphy_rating=giphy_rating)
|
|
|
|
)
|
2017-04-16 17:30:49 +02:00
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
message_retention_days: int | None = None
|
2020-06-24 19:59:36 +02:00
|
|
|
if message_retention_days_raw is not None:
|
2020-06-11 21:16:53 +02:00
|
|
|
if not user_profile.is_realm_owner:
|
2023-02-04 02:07:20 +01:00
|
|
|
raise OrganizationOwnerRequiredError
|
2020-05-19 14:38:43 +02:00
|
|
|
realm.ensure_not_on_limited_plan()
|
2023-05-27 02:25:36 +02:00
|
|
|
message_retention_days = parse_message_retention_days( # used by locals() below
|
|
|
|
message_retention_days_raw, Realm.MESSAGE_RETENTION_SPECIAL_VALUES_MAP
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2020-05-09 19:46:56 +02:00
|
|
|
|
2021-07-28 18:23:39 +02:00
|
|
|
if (
|
2023-08-09 15:06:56 +02:00
|
|
|
invite_to_realm_policy is not None
|
|
|
|
or invite_required is not None
|
2024-10-22 11:48:29 +02:00
|
|
|
or create_multiuse_invite_group is not None
|
2024-09-13 15:45:49 +02:00
|
|
|
or can_create_groups is not None
|
2024-09-13 20:37:43 +02:00
|
|
|
or can_manage_all_groups is not None
|
2021-07-28 18:23:39 +02:00
|
|
|
) and not user_profile.is_realm_owner:
|
2023-02-04 02:07:20 +01:00
|
|
|
raise OrganizationOwnerRequiredError
|
2021-07-19 11:47:42 +02:00
|
|
|
|
2021-07-28 18:43:09 +02:00
|
|
|
if (
|
|
|
|
emails_restricted_to_domains is not None or disallow_disposable_email_addresses is not None
|
|
|
|
) and not user_profile.is_realm_owner:
|
2023-02-04 02:07:20 +01:00
|
|
|
raise OrganizationOwnerRequiredError
|
2021-07-28 18:43:09 +02:00
|
|
|
|
2021-07-29 09:57:58 +02:00
|
|
|
if waiting_period_threshold is not None and not user_profile.is_realm_owner:
|
2023-02-04 02:07:20 +01:00
|
|
|
raise OrganizationOwnerRequiredError
|
2021-07-29 09:57:58 +02:00
|
|
|
|
2022-08-03 12:24:39 +02:00
|
|
|
if enable_spectator_access:
|
|
|
|
realm.ensure_not_on_limited_plan()
|
|
|
|
|
2024-10-22 12:14:59 +02:00
|
|
|
if can_access_all_users_group is not None:
|
2023-11-22 12:33:48 +01:00
|
|
|
realm.can_enable_restricted_user_access_for_guests()
|
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
data: dict[str, Any] = {}
|
2021-06-14 18:49:28 +02:00
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
message_content_delete_limit_seconds: int | None = None
|
2021-06-14 18:49:28 +02:00
|
|
|
if message_content_delete_limit_seconds_raw is not None:
|
2022-10-12 09:14:27 +02:00
|
|
|
(
|
|
|
|
message_content_delete_limit_seconds,
|
|
|
|
setting_value_changed,
|
|
|
|
) = parse_and_set_setting_value_if_required(
|
|
|
|
realm,
|
|
|
|
"message_content_delete_limit_seconds",
|
2021-06-14 18:49:28 +02:00
|
|
|
message_content_delete_limit_seconds_raw,
|
2022-10-12 09:14:27 +02:00
|
|
|
acting_user=user_profile,
|
2021-06-14 18:49:28 +02:00
|
|
|
)
|
2022-10-12 09:14:27 +02:00
|
|
|
|
|
|
|
if setting_value_changed:
|
2022-04-13 19:05:15 +02:00
|
|
|
data["message_content_delete_limit_seconds"] = message_content_delete_limit_seconds
|
2021-06-14 18:49:28 +02:00
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
message_content_edit_limit_seconds: int | None = None
|
2022-09-22 10:53:37 +02:00
|
|
|
if message_content_edit_limit_seconds_raw is not None:
|
2022-10-12 09:14:27 +02:00
|
|
|
(
|
|
|
|
message_content_edit_limit_seconds,
|
|
|
|
setting_value_changed,
|
|
|
|
) = parse_and_set_setting_value_if_required(
|
|
|
|
realm,
|
|
|
|
"message_content_edit_limit_seconds",
|
2022-09-22 10:53:37 +02:00
|
|
|
message_content_edit_limit_seconds_raw,
|
2022-10-12 09:14:27 +02:00
|
|
|
acting_user=user_profile,
|
2022-09-22 10:53:37 +02:00
|
|
|
)
|
|
|
|
|
2022-10-12 09:14:27 +02:00
|
|
|
if setting_value_changed:
|
2022-09-22 10:53:37 +02:00
|
|
|
data["message_content_edit_limit_seconds"] = message_content_edit_limit_seconds
|
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
move_messages_within_stream_limit_seconds: int | None = None
|
2023-01-26 12:53:27 +01:00
|
|
|
if move_messages_within_stream_limit_seconds_raw is not None:
|
|
|
|
(
|
|
|
|
move_messages_within_stream_limit_seconds,
|
|
|
|
setting_value_changed,
|
|
|
|
) = parse_and_set_setting_value_if_required(
|
|
|
|
realm,
|
|
|
|
"move_messages_within_stream_limit_seconds",
|
|
|
|
move_messages_within_stream_limit_seconds_raw,
|
|
|
|
acting_user=user_profile,
|
|
|
|
)
|
|
|
|
|
|
|
|
if setting_value_changed:
|
2024-01-29 00:32:21 +01:00
|
|
|
data["move_messages_within_stream_limit_seconds"] = (
|
|
|
|
move_messages_within_stream_limit_seconds
|
|
|
|
)
|
2023-01-26 12:53:27 +01:00
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
move_messages_between_streams_limit_seconds: int | None = None
|
2022-10-11 13:19:49 +02:00
|
|
|
if move_messages_between_streams_limit_seconds_raw is not None:
|
|
|
|
(
|
|
|
|
move_messages_between_streams_limit_seconds,
|
|
|
|
setting_value_changed,
|
|
|
|
) = parse_and_set_setting_value_if_required(
|
|
|
|
realm,
|
|
|
|
"move_messages_between_streams_limit_seconds",
|
|
|
|
move_messages_between_streams_limit_seconds_raw,
|
|
|
|
acting_user=user_profile,
|
|
|
|
)
|
|
|
|
|
|
|
|
if setting_value_changed:
|
2024-01-29 00:32:21 +01:00
|
|
|
data["move_messages_between_streams_limit_seconds"] = (
|
|
|
|
move_messages_between_streams_limit_seconds
|
|
|
|
)
|
2022-10-11 13:19:49 +02:00
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
jitsi_server_url: str | None = None
|
2023-09-19 19:03:08 +02:00
|
|
|
if jitsi_server_url_raw is not None:
|
|
|
|
jitsi_server_url = parse_jitsi_server_url(
|
|
|
|
jitsi_server_url_raw,
|
|
|
|
Realm.JITSI_SERVER_SPECIAL_VALUES_MAP,
|
|
|
|
)
|
|
|
|
|
|
|
|
# We handle the "None" case separately here because
|
|
|
|
# in the loop below, do_set_realm_property is called only when
|
|
|
|
# the setting value is not "None". For values other than "None",
|
|
|
|
# the loop itself sets the value of 'jitsi_server_url' by
|
|
|
|
# calling do_set_realm_property.
|
|
|
|
if jitsi_server_url is None and realm.jitsi_server_url is not None:
|
|
|
|
do_set_realm_property(
|
|
|
|
realm,
|
|
|
|
"jitsi_server_url",
|
|
|
|
jitsi_server_url,
|
|
|
|
acting_user=user_profile,
|
|
|
|
)
|
|
|
|
|
|
|
|
data["jitsi_server_url"] = jitsi_server_url
|
|
|
|
|
2017-04-16 17:30:49 +02:00
|
|
|
# The user of `locals()` here is a bit of a code smell, but it's
|
|
|
|
# restricted to the elements present in realm.property_types.
|
|
|
|
#
|
|
|
|
# TODO: It should be possible to deduplicate this function up
|
|
|
|
# further by some more advanced usage of the
|
2024-09-03 18:08:36 +02:00
|
|
|
# `typed_endpoint` extraction.
|
2023-08-09 15:06:56 +02:00
|
|
|
req_vars = {}
|
|
|
|
req_group_setting_vars = {}
|
|
|
|
|
2023-09-12 23:19:57 +02:00
|
|
|
for k, v in locals().items():
|
2023-08-09 15:06:56 +02:00
|
|
|
if k in realm.property_types:
|
|
|
|
req_vars[k] = v
|
|
|
|
|
2024-05-23 16:21:25 +02:00
|
|
|
for setting_name, permission_configuration in Realm.REALM_PERMISSION_GROUP_SETTINGS.items():
|
|
|
|
if k in [permission_configuration.id_field_name, setting_name]:
|
2023-08-09 15:06:56 +02:00
|
|
|
req_group_setting_vars[k] = v
|
2017-04-16 17:30:49 +02:00
|
|
|
|
2023-09-12 23:19:57 +02:00
|
|
|
for k, v in req_vars.items():
|
2017-04-19 05:49:53 +02:00
|
|
|
if v is not None and getattr(realm, k) != v:
|
2020-06-29 15:34:19 +02:00
|
|
|
do_set_realm_property(realm, k, v, acting_user=user_profile)
|
2018-04-24 03:47:28 +02:00
|
|
|
if isinstance(v, str):
|
2021-02-12 08:20:45 +01:00
|
|
|
data[k] = "updated"
|
2017-04-16 17:30:49 +02:00
|
|
|
else:
|
|
|
|
data[k] = v
|
|
|
|
|
2023-10-31 12:28:49 +01:00
|
|
|
for setting_name, permission_configuration in Realm.REALM_PERMISSION_GROUP_SETTINGS.items():
|
2024-05-23 16:21:25 +02:00
|
|
|
expected_current_setting_value = None
|
2024-10-22 12:42:14 +02:00
|
|
|
assert setting_name in req_group_setting_vars
|
|
|
|
if req_group_setting_vars[setting_name] is None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
setting_value = req_group_setting_vars[setting_name]
|
|
|
|
new_setting_value = parse_group_setting_value(setting_value.new, setting_name)
|
|
|
|
|
|
|
|
if setting_value.old is not None:
|
|
|
|
expected_current_setting_value = parse_group_setting_value(
|
|
|
|
setting_value.old, setting_name
|
|
|
|
)
|
2024-05-23 16:21:25 +02:00
|
|
|
|
|
|
|
current_value = getattr(realm, setting_name)
|
|
|
|
current_setting_api_value = get_group_setting_value_for_api(current_value)
|
|
|
|
|
|
|
|
if validate_group_setting_value_change(
|
|
|
|
current_setting_api_value, new_setting_value, expected_current_setting_value
|
|
|
|
):
|
|
|
|
with transaction.atomic(durable=True):
|
|
|
|
user_group = access_user_group_for_setting(
|
|
|
|
new_setting_value,
|
|
|
|
user_profile,
|
|
|
|
setting_name=setting_name,
|
|
|
|
permission_configuration=permission_configuration,
|
|
|
|
current_setting_value=current_value,
|
|
|
|
)
|
|
|
|
do_change_realm_permission_group_setting(
|
|
|
|
realm,
|
|
|
|
setting_name,
|
|
|
|
user_group,
|
|
|
|
old_setting_api_value=current_setting_api_value,
|
|
|
|
acting_user=user_profile,
|
|
|
|
)
|
|
|
|
data[setting_name] = new_setting_value
|
2023-08-09 15:06:56 +02:00
|
|
|
|
2017-04-16 17:30:49 +02:00
|
|
|
# The following realm properties do not fit the pattern above
|
2017-04-19 05:49:53 +02:00
|
|
|
# authentication_methods is not supported by the do_set_realm_property
|
2023-04-16 21:53:22 +02:00
|
|
|
# framework because it's tracked through the RealmAuthenticationMethod table.
|
2021-02-12 08:19:30 +01:00
|
|
|
if authentication_methods is not None and (
|
|
|
|
realm.authentication_methods_dict() != authentication_methods
|
|
|
|
):
|
2020-07-02 14:03:43 +02:00
|
|
|
do_set_realm_authentication_methods(realm, authentication_methods, acting_user=user_profile)
|
2021-02-12 08:20:45 +01:00
|
|
|
data["authentication_methods"] = authentication_methods
|
2022-04-12 13:13:02 +02:00
|
|
|
|
2024-01-26 14:45:37 +01:00
|
|
|
# Realm.new_stream_announcements_stream, Realm.signup_announcements_stream,
|
|
|
|
# and Realm.zulip_update_announcements_stream are not boolean, str or integer field,
|
|
|
|
# and thus doesn't fit into the do_set_realm_property framework.
|
2024-02-07 12:13:02 +01:00
|
|
|
if new_stream_announcements_stream_id is not None and (
|
|
|
|
realm.new_stream_announcements_stream is None
|
|
|
|
or (realm.new_stream_announcements_stream.id != new_stream_announcements_stream_id)
|
2023-01-18 02:59:37 +01:00
|
|
|
):
|
2024-02-07 12:13:02 +01:00
|
|
|
new_stream_announcements_stream_new = None
|
|
|
|
if new_stream_announcements_stream_id >= 0:
|
|
|
|
(new_stream_announcements_stream_new, sub) = access_stream_by_id(
|
|
|
|
user_profile, new_stream_announcements_stream_id, allow_realm_admin=True
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2024-02-07 12:13:02 +01:00
|
|
|
do_set_realm_new_stream_announcements_stream(
|
|
|
|
realm,
|
|
|
|
new_stream_announcements_stream_new,
|
|
|
|
new_stream_announcements_stream_id,
|
|
|
|
acting_user=user_profile,
|
2023-01-18 02:59:37 +01:00
|
|
|
)
|
2024-02-07 12:13:02 +01:00
|
|
|
data["new_stream_announcements_stream_id"] = new_stream_announcements_stream_id
|
2017-06-09 20:50:38 +02:00
|
|
|
|
2024-02-07 17:11:43 +01:00
|
|
|
if signup_announcements_stream_id is not None and (
|
|
|
|
realm.signup_announcements_stream is None
|
|
|
|
or realm.signup_announcements_stream.id != signup_announcements_stream_id
|
2023-01-18 02:59:37 +01:00
|
|
|
):
|
2024-02-07 17:11:43 +01:00
|
|
|
new_signup_announcements_stream = None
|
|
|
|
if signup_announcements_stream_id >= 0:
|
|
|
|
(new_signup_announcements_stream, sub) = access_stream_by_id(
|
|
|
|
user_profile, signup_announcements_stream_id, allow_realm_admin=True
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2024-02-07 17:11:43 +01:00
|
|
|
do_set_realm_signup_announcements_stream(
|
2023-01-18 02:59:37 +01:00
|
|
|
realm,
|
2024-02-07 17:11:43 +01:00
|
|
|
new_signup_announcements_stream,
|
|
|
|
signup_announcements_stream_id,
|
2023-01-18 02:59:37 +01:00
|
|
|
acting_user=user_profile,
|
|
|
|
)
|
2024-02-07 17:11:43 +01:00
|
|
|
data["signup_announcements_stream_id"] = signup_announcements_stream_id
|
2017-10-20 16:55:04 +02:00
|
|
|
|
2024-01-26 14:45:37 +01:00
|
|
|
if zulip_update_announcements_stream_id is not None and (
|
|
|
|
realm.zulip_update_announcements_stream is None
|
|
|
|
or realm.zulip_update_announcements_stream.id != zulip_update_announcements_stream_id
|
|
|
|
):
|
|
|
|
new_zulip_update_announcements_stream = None
|
|
|
|
if zulip_update_announcements_stream_id >= 0:
|
|
|
|
(new_zulip_update_announcements_stream, sub) = access_stream_by_id(
|
|
|
|
user_profile, zulip_update_announcements_stream_id, allow_realm_admin=True
|
|
|
|
)
|
|
|
|
do_set_realm_zulip_update_announcements_stream(
|
|
|
|
realm,
|
|
|
|
new_zulip_update_announcements_stream,
|
|
|
|
zulip_update_announcements_stream_id,
|
|
|
|
acting_user=user_profile,
|
|
|
|
)
|
|
|
|
data["zulip_update_announcements_stream_id"] = zulip_update_announcements_stream_id
|
|
|
|
|
2021-09-13 20:01:35 +02:00
|
|
|
if string_id is not None:
|
|
|
|
if not user_profile.is_realm_owner:
|
2023-02-04 02:07:20 +01:00
|
|
|
raise OrganizationOwnerRequiredError
|
2021-09-13 20:01:35 +02:00
|
|
|
|
|
|
|
if realm.demo_organization_scheduled_deletion_date is None:
|
|
|
|
raise JsonableError(_("Must be a demo organization."))
|
|
|
|
|
|
|
|
try:
|
|
|
|
check_subdomain(string_id)
|
|
|
|
except ValidationError as err:
|
|
|
|
raise JsonableError(str(err.message))
|
|
|
|
|
|
|
|
do_change_realm_subdomain(realm, string_id, acting_user=user_profile)
|
2024-05-06 15:27:22 +02:00
|
|
|
data["realm_uri"] = realm.url
|
2024-05-07 06:08:52 +02:00
|
|
|
data["realm_url"] = realm.url
|
2021-09-13 20:01:35 +02:00
|
|
|
|
2022-04-11 19:26:16 +02:00
|
|
|
if org_type is not None:
|
|
|
|
do_change_realm_org_type(realm, org_type, acting_user=user_profile)
|
|
|
|
data["org_type"] = org_type
|
|
|
|
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data)
|
2018-01-30 14:58:50 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-06-11 00:26:49 +02:00
|
|
|
@require_realm_owner
|
2024-07-12 17:49:28 +02:00
|
|
|
@typed_endpoint_without_parameters
|
2019-05-08 05:59:04 +02:00
|
|
|
def deactivate_realm(request: HttpRequest, user: UserProfile) -> HttpResponse:
|
|
|
|
realm = user.realm
|
2023-09-25 22:59:44 +02:00
|
|
|
do_deactivate_realm(
|
|
|
|
realm, acting_user=user, deactivation_reason="owner_request", email_owners=True
|
|
|
|
)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2018-01-25 19:08:40 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-08-12 05:44:35 +02:00
|
|
|
@require_safe
|
2018-04-24 03:47:28 +02:00
|
|
|
def check_subdomain_available(request: HttpRequest, subdomain: str) -> HttpResponse:
|
2018-01-25 19:08:40 +01:00
|
|
|
try:
|
|
|
|
check_subdomain(subdomain)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data={"msg": "available"})
|
2018-01-25 19:08:40 +01:00
|
|
|
except ValidationError as e:
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data={"msg": e.message})
|
2018-11-12 14:15:49 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-11-12 14:15:49 +01:00
|
|
|
def realm_reactivation(request: HttpRequest, confirmation_key: str) -> HttpResponse:
|
|
|
|
try:
|
2022-07-26 15:48:26 +02:00
|
|
|
obj = get_object_from_key(
|
|
|
|
confirmation_key, [Confirmation.REALM_REACTIVATION], mark_object_used=True
|
2022-07-19 21:13:32 +02:00
|
|
|
)
|
2022-11-17 09:30:48 +01:00
|
|
|
except ConfirmationKeyError:
|
2022-07-26 15:37:43 +02:00
|
|
|
return render(request, "zerver/realm_reactivation_link_error.html", status=404)
|
2022-07-26 15:48:26 +02:00
|
|
|
|
|
|
|
assert isinstance(obj, RealmReactivationStatus)
|
|
|
|
realm = obj.realm
|
|
|
|
|
2018-11-12 14:15:49 +01:00
|
|
|
do_reactivate_realm(realm)
|
2022-07-19 21:13:32 +02:00
|
|
|
|
2018-11-12 14:15:49 +01:00
|
|
|
context = {"realm": realm}
|
2021-02-12 08:20:45 +01:00
|
|
|
return render(request, "zerver/realm_reactivation.html", context)
|
2021-07-21 13:23:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
emojiset_choices = {emojiset["key"] for emojiset in RealmUserDefault.emojiset_choices()}
|
|
|
|
|
|
|
|
|
|
|
|
@require_realm_admin
|
2024-07-12 17:49:28 +02:00
|
|
|
@typed_endpoint
|
2021-07-21 13:23:23 +02:00
|
|
|
def update_realm_user_settings_defaults(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2024-07-12 17:49:28 +02:00
|
|
|
*,
|
|
|
|
dense_mode: Json[bool] | None = None,
|
|
|
|
web_mark_read_on_scroll_policy: Json[
|
|
|
|
Annotated[
|
|
|
|
int,
|
|
|
|
check_int_in_validator(UserProfile.WEB_MARK_READ_ON_SCROLL_POLICY_CHOICES),
|
|
|
|
]
|
|
|
|
]
|
|
|
|
| None = None,
|
|
|
|
web_channel_default_view: Json[
|
|
|
|
Annotated[int, check_int_in_validator(UserProfile.WEB_CHANNEL_DEFAULT_VIEW_CHOICES)]
|
|
|
|
]
|
|
|
|
| None = None,
|
|
|
|
starred_message_counts: Json[bool] | None = None,
|
|
|
|
receives_typing_notifications: Json[bool] | None = None,
|
|
|
|
web_stream_unreads_count_display_policy: Json[
|
|
|
|
Annotated[
|
|
|
|
int,
|
|
|
|
check_int_in_validator(UserProfile.WEB_STREAM_UNREADS_COUNT_DISPLAY_POLICY_CHOICES),
|
|
|
|
]
|
|
|
|
]
|
|
|
|
| None = None,
|
|
|
|
fluid_layout_width: Json[bool] | None = None,
|
|
|
|
high_contrast_mode: Json[bool] | None = None,
|
|
|
|
color_scheme: Json[Annotated[int, check_int_in_validator(UserProfile.COLOR_SCHEME_CHOICES)]]
|
|
|
|
| None = None,
|
|
|
|
web_font_size_px: Json[int] | None = None,
|
|
|
|
web_line_height_percent: Json[int] | None = None,
|
|
|
|
translate_emoticons: Json[bool] | None = None,
|
|
|
|
display_emoji_reaction_users: Json[bool] | None = None,
|
|
|
|
web_home_view: Literal["recent_topics", "inbox", "all_messages"] | None = None,
|
|
|
|
web_escape_navigates_to_home_view: Json[bool] | None = None,
|
|
|
|
left_side_userlist: Json[bool] | None = None,
|
|
|
|
emojiset: Annotated[str, check_string_in_validator(emojiset_choices)] | None = None,
|
|
|
|
demote_inactive_streams: Json[
|
|
|
|
Annotated[int, check_int_in_validator(UserProfile.DEMOTE_STREAMS_CHOICES)]
|
|
|
|
]
|
|
|
|
| None = None,
|
|
|
|
enable_stream_desktop_notifications: Json[bool] | None = None,
|
|
|
|
enable_stream_email_notifications: Json[bool] | None = None,
|
|
|
|
enable_stream_push_notifications: Json[bool] | None = None,
|
|
|
|
enable_stream_audible_notifications: Json[bool] | None = None,
|
|
|
|
wildcard_mentions_notify: Json[bool] | None = None,
|
|
|
|
enable_followed_topic_desktop_notifications: Json[bool] | None = None,
|
|
|
|
enable_followed_topic_email_notifications: Json[bool] | None = None,
|
|
|
|
enable_followed_topic_push_notifications: Json[bool] | None = None,
|
|
|
|
enable_followed_topic_audible_notifications: Json[bool] | None = None,
|
|
|
|
enable_followed_topic_wildcard_mentions_notify: Json[bool] | None = None,
|
|
|
|
notification_sound: str | None = None,
|
|
|
|
enable_desktop_notifications: Json[bool] | None = None,
|
|
|
|
enable_sounds: Json[bool] | None = None,
|
|
|
|
enable_offline_email_notifications: Json[bool] | None = None,
|
|
|
|
enable_offline_push_notifications: Json[bool] | None = None,
|
|
|
|
enable_online_push_notifications: Json[bool] | None = None,
|
|
|
|
enable_digest_emails: Json[bool] | None = None,
|
2021-09-28 10:03:43 +02:00
|
|
|
# enable_login_emails is not included here, because we don't want
|
|
|
|
# security-related settings to be controlled by organization administrators.
|
2021-07-21 13:23:23 +02:00
|
|
|
# enable_marketing_emails is not included here, since we don't at
|
|
|
|
# present allow organizations to customize this. (The user's selection
|
|
|
|
# in the signup form takes precedence over RealmUserDefault).
|
|
|
|
#
|
|
|
|
# We may want to change this model in the future, since some SSO signups
|
|
|
|
# do not offer an opportunity to prompt the user at all during signup.
|
2024-07-12 17:49:28 +02:00
|
|
|
message_content_in_email_notifications: Json[bool] | None = None,
|
|
|
|
pm_content_in_desktop_notifications: Json[bool] | None = None,
|
|
|
|
desktop_icon_count_display: Json[
|
|
|
|
Annotated[int, check_int_in_validator(UserProfile.DESKTOP_ICON_COUNT_DISPLAY_CHOICES)]
|
|
|
|
]
|
|
|
|
| None = None,
|
|
|
|
realm_name_in_email_notifications_policy: Json[
|
|
|
|
Annotated[
|
|
|
|
int,
|
|
|
|
check_int_in_validator(UserProfile.REALM_NAME_IN_EMAIL_NOTIFICATIONS_POLICY_CHOICES),
|
|
|
|
]
|
|
|
|
]
|
|
|
|
| None = None,
|
|
|
|
automatically_follow_topics_policy: Json[
|
|
|
|
Annotated[
|
|
|
|
int,
|
|
|
|
check_int_in_validator(UserProfile.AUTOMATICALLY_CHANGE_VISIBILITY_POLICY_CHOICES),
|
|
|
|
]
|
|
|
|
]
|
|
|
|
| None = None,
|
|
|
|
automatically_unmute_topics_in_muted_streams_policy: Json[
|
|
|
|
Annotated[
|
|
|
|
int,
|
|
|
|
check_int_in_validator(UserProfile.AUTOMATICALLY_CHANGE_VISIBILITY_POLICY_CHOICES),
|
|
|
|
]
|
|
|
|
]
|
|
|
|
| None = None,
|
|
|
|
automatically_follow_topics_where_mentioned: Json[bool] | None = None,
|
|
|
|
presence_enabled: Json[bool] | None = None,
|
|
|
|
enter_sends: Json[bool] | None = None,
|
|
|
|
enable_drafts_synchronization: Json[bool] | None = None,
|
|
|
|
email_notifications_batching_period_seconds: Json[int] | None = None,
|
|
|
|
twenty_four_hour_time: Json[bool] | None = None,
|
|
|
|
send_stream_typing_notifications: Json[bool] | None = None,
|
|
|
|
send_private_typing_notifications: Json[bool] | None = None,
|
|
|
|
send_read_receipts: Json[bool] | None = None,
|
|
|
|
user_list_style: Json[
|
|
|
|
Annotated[int, check_int_in_validator(UserProfile.USER_LIST_STYLE_CHOICES)]
|
|
|
|
]
|
|
|
|
| None = None,
|
2024-07-21 15:23:56 +02:00
|
|
|
web_animate_image_previews: Literal["always", "on_hover", "never"] | None = None,
|
2024-07-12 17:49:28 +02:00
|
|
|
email_address_visibility: Json[
|
|
|
|
Annotated[int, check_int_in_validator(UserProfile.EMAIL_ADDRESS_VISIBILITY_TYPES)]
|
|
|
|
]
|
|
|
|
| None = None,
|
|
|
|
web_navigate_to_sent_message: Json[bool] | None = None,
|
2021-07-21 13:23:23 +02:00
|
|
|
) -> HttpResponse:
|
|
|
|
if notification_sound is not None or email_notifications_batching_period_seconds is not None:
|
|
|
|
check_settings_values(notification_sound, email_notifications_batching_period_seconds)
|
|
|
|
|
2024-07-18 11:05:37 +02:00
|
|
|
realm_user_default = RealmUserDefault.objects.get(realm=user_profile.realm)
|
2024-07-15 12:12:17 +02:00
|
|
|
|
|
|
|
if (
|
2024-07-18 11:05:37 +02:00
|
|
|
dense_mode is not None
|
|
|
|
or web_font_size_px is not None
|
|
|
|
or web_line_height_percent is not None
|
2024-07-15 12:12:17 +02:00
|
|
|
):
|
2024-07-18 11:05:37 +02:00
|
|
|
check_information_density_setting_values(
|
|
|
|
realm_user_default, dense_mode, web_font_size_px, web_line_height_percent
|
2024-07-15 12:12:17 +02:00
|
|
|
)
|
|
|
|
|
2023-09-12 21:10:57 +02:00
|
|
|
request_settings = {k: v for k, v in locals().items() if k in RealmUserDefault.property_types}
|
2023-09-12 23:19:57 +02:00
|
|
|
for k, v in request_settings.items():
|
2021-07-21 13:23:23 +02:00
|
|
|
if v is not None and getattr(realm_user_default, k) != v:
|
|
|
|
do_set_realm_user_default_setting(realm_user_default, k, v, acting_user=user_profile)
|
|
|
|
|
2022-08-25 18:41:46 +02:00
|
|
|
return json_success(request)
|