2022-04-14 23:58:15 +02:00
|
|
|
import logging
|
2023-11-19 19:45:19 +01:00
|
|
|
from datetime import datetime, timedelta
|
2022-04-14 23:58:15 +02:00
|
|
|
from typing import Any, Dict, Optional
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.db import transaction
|
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
|
2023-03-10 11:42:18 +01:00
|
|
|
from confirmation import settings as confirmation_settings
|
2022-04-14 23:58:15 +02:00
|
|
|
from zerver.actions.message_send import internal_send_stream_message
|
|
|
|
from zerver.actions.realm_settings import (
|
|
|
|
do_add_deactivated_redirect,
|
|
|
|
do_change_realm_plan_type,
|
|
|
|
do_deactivate_realm,
|
|
|
|
)
|
|
|
|
from zerver.lib.bulk_create import create_users
|
2023-11-23 22:07:41 +01:00
|
|
|
from zerver.lib.push_notifications import sends_notifications_directly
|
2023-12-11 04:59:00 +01:00
|
|
|
from zerver.lib.remote_server import maybe_enqueue_audit_log_upload
|
2022-04-14 23:58:15 +02:00
|
|
|
from zerver.lib.server_initialization import create_internal_realm, server_initialized
|
|
|
|
from zerver.lib.streams import ensure_stream, get_signups_stream
|
2023-08-09 15:06:56 +02:00
|
|
|
from zerver.lib.user_groups import (
|
|
|
|
create_system_user_groups_for_realm,
|
|
|
|
get_role_based_system_groups_dict,
|
|
|
|
)
|
2024-02-05 17:12:55 +01:00
|
|
|
from zerver.lib.zulip_update_announcements import get_latest_zulip_update_announcements_level
|
2022-04-14 23:58:15 +02:00
|
|
|
from zerver.models import (
|
|
|
|
DefaultStream,
|
2023-03-10 11:42:18 +01:00
|
|
|
PreregistrationRealm,
|
2022-04-14 23:58:15 +02:00
|
|
|
Realm,
|
|
|
|
RealmAuditLog,
|
2023-04-16 21:53:22 +02:00
|
|
|
RealmAuthenticationMethod,
|
2022-04-14 23:58:15 +02:00
|
|
|
RealmUserDefault,
|
|
|
|
Stream,
|
|
|
|
UserProfile,
|
|
|
|
)
|
2023-12-15 02:14:24 +01:00
|
|
|
from zerver.models.realms import get_org_type_display_name, get_realm
|
2023-12-15 01:16:00 +01:00
|
|
|
from zerver.models.users import get_system_bot
|
2024-02-05 23:52:25 +01:00
|
|
|
from zproject.backends import all_default_backend_names
|
2022-04-14 23:58:15 +02:00
|
|
|
|
2023-03-10 16:03:18 +01:00
|
|
|
if settings.CORPORATE_ENABLED:
|
2023-12-06 15:00:23 +01:00
|
|
|
from corporate.lib.support import get_realm_support_url
|
2023-03-10 16:03:18 +01:00
|
|
|
|
2022-04-14 23:58:15 +02:00
|
|
|
|
|
|
|
def do_change_realm_subdomain(
|
2022-06-23 23:57:39 +02:00
|
|
|
realm: Realm,
|
|
|
|
new_subdomain: str,
|
|
|
|
*,
|
|
|
|
acting_user: Optional[UserProfile],
|
|
|
|
add_deactivated_redirect: bool = True,
|
2022-04-14 23:58:15 +02:00
|
|
|
) -> None:
|
|
|
|
"""Changing a realm's subdomain is a highly disruptive operation,
|
|
|
|
because all existing clients will need to be updated to point to
|
|
|
|
the new URL. Further, requests to fetch data from existing event
|
|
|
|
queues will fail with an authentication error when this change
|
|
|
|
happens (because the old subdomain is no longer associated with
|
|
|
|
the realm), making it hard for us to provide a graceful update
|
|
|
|
experience for clients.
|
|
|
|
"""
|
|
|
|
old_subdomain = realm.subdomain
|
|
|
|
old_uri = realm.uri
|
|
|
|
# If the realm had been a demo organization scheduled for
|
|
|
|
# deleting, clear that state.
|
|
|
|
realm.demo_organization_scheduled_deletion_date = None
|
|
|
|
realm.string_id = new_subdomain
|
|
|
|
with transaction.atomic():
|
|
|
|
realm.save(update_fields=["string_id", "demo_organization_scheduled_deletion_date"])
|
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=realm,
|
|
|
|
event_type=RealmAuditLog.REALM_SUBDOMAIN_CHANGED,
|
|
|
|
event_time=timezone_now(),
|
|
|
|
acting_user=acting_user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={"old_subdomain": old_subdomain, "new_subdomain": new_subdomain},
|
2022-04-14 23:58:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# If a realm if being renamed multiple times, we should find all the placeholder
|
|
|
|
# realms and reset their deactivated_redirect field to point to the new realm uri
|
|
|
|
placeholder_realms = Realm.objects.filter(deactivated_redirect=old_uri, deactivated=True)
|
|
|
|
for placeholder_realm in placeholder_realms:
|
|
|
|
do_add_deactivated_redirect(placeholder_realm, realm.uri)
|
|
|
|
|
|
|
|
# The below block isn't executed in a transaction with the earlier code due to
|
|
|
|
# the functions called below being complex and potentially sending events,
|
|
|
|
# which we don't want to do in atomic blocks.
|
|
|
|
# When we change a realm's subdomain the realm with old subdomain is basically
|
|
|
|
# deactivated. We are creating a deactivated realm using old subdomain and setting
|
|
|
|
# it's deactivated redirect to new_subdomain so that we can tell the users that
|
|
|
|
# the realm has been moved to a new subdomain.
|
2022-06-23 23:57:39 +02:00
|
|
|
if add_deactivated_redirect:
|
|
|
|
placeholder_realm = do_create_realm(old_subdomain, realm.name)
|
|
|
|
do_deactivate_realm(placeholder_realm, acting_user=None)
|
|
|
|
do_add_deactivated_redirect(placeholder_realm, realm.uri)
|
2022-04-14 23:58:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
def set_realm_permissions_based_on_org_type(realm: Realm) -> None:
|
|
|
|
"""This function implements overrides for the default configuration
|
|
|
|
for new organizations when the administrator selected specific
|
|
|
|
organization types.
|
|
|
|
|
|
|
|
This substantially simplifies our /help/ advice for folks setting
|
|
|
|
up new organizations of these types.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Custom configuration for educational organizations. The present
|
|
|
|
# defaults are designed for a single class, not a department or
|
|
|
|
# larger institution, since those are more common.
|
2023-07-22 01:15:10 +02:00
|
|
|
if realm.org_type in (
|
|
|
|
Realm.ORG_TYPES["education_nonprofit"]["id"],
|
|
|
|
Realm.ORG_TYPES["education"]["id"],
|
2022-04-14 23:58:15 +02:00
|
|
|
):
|
2021-10-26 09:15:16 +02:00
|
|
|
# Limit user creation to administrators.
|
2022-04-14 23:58:15 +02:00
|
|
|
realm.invite_to_realm_policy = Realm.POLICY_ADMINS_ONLY
|
|
|
|
# Restrict public stream creation to staff, but allow private
|
|
|
|
# streams (useful for study groups, etc.).
|
|
|
|
realm.create_public_stream_policy = Realm.POLICY_ADMINS_ONLY
|
|
|
|
# Don't allow members (students) to manage user groups or
|
|
|
|
# stream subscriptions.
|
|
|
|
realm.user_group_edit_policy = Realm.POLICY_MODERATORS_ONLY
|
|
|
|
realm.invite_to_stream_policy = Realm.POLICY_MODERATORS_ONLY
|
|
|
|
# Allow moderators (TAs?) to move topics between streams.
|
|
|
|
realm.move_messages_between_streams_policy = Realm.POLICY_MODERATORS_ONLY
|
|
|
|
|
|
|
|
|
2023-08-09 15:06:56 +02:00
|
|
|
@transaction.atomic(savepoint=False)
|
|
|
|
def set_default_for_realm_permission_group_settings(realm: Realm) -> None:
|
|
|
|
system_groups_dict = get_role_based_system_groups_dict(realm)
|
|
|
|
|
2023-10-31 12:28:49 +01:00
|
|
|
for setting_name, permission_configuration in Realm.REALM_PERMISSION_GROUP_SETTINGS.items():
|
|
|
|
group_name = permission_configuration.default_group_name
|
2023-08-09 15:06:56 +02:00
|
|
|
setattr(realm, setting_name, system_groups_dict[group_name])
|
|
|
|
|
|
|
|
realm.save(update_fields=list(Realm.REALM_PERMISSION_GROUP_SETTINGS.keys()))
|
|
|
|
|
|
|
|
|
2022-04-14 23:58:15 +02:00
|
|
|
def setup_realm_internal_bots(realm: Realm) -> None:
|
|
|
|
"""Create this realm's internal bots.
|
|
|
|
|
|
|
|
This function is idempotent; it does nothing for a bot that
|
|
|
|
already exists.
|
|
|
|
"""
|
|
|
|
internal_bots = [
|
|
|
|
(bot["name"], bot["email_template"] % (settings.INTERNAL_BOT_DOMAIN,))
|
|
|
|
for bot in settings.REALM_INTERNAL_BOTS
|
|
|
|
]
|
|
|
|
create_users(realm, internal_bots, bot_type=UserProfile.DEFAULT_BOT)
|
|
|
|
bots = UserProfile.objects.filter(
|
|
|
|
realm=realm,
|
|
|
|
email__in=[bot_info[1] for bot_info in internal_bots],
|
|
|
|
bot_owner__isnull=True,
|
|
|
|
)
|
|
|
|
for bot in bots:
|
|
|
|
bot.bot_owner = bot
|
|
|
|
bot.save()
|
|
|
|
|
|
|
|
|
|
|
|
def do_create_realm(
|
|
|
|
string_id: str,
|
|
|
|
name: str,
|
|
|
|
*,
|
|
|
|
emails_restricted_to_domains: Optional[bool] = None,
|
|
|
|
description: Optional[str] = None,
|
|
|
|
invite_required: Optional[bool] = None,
|
|
|
|
plan_type: Optional[int] = None,
|
|
|
|
org_type: Optional[int] = None,
|
2023-09-12 21:58:58 +02:00
|
|
|
default_language: Optional[str] = None,
|
2023-11-19 19:45:19 +01:00
|
|
|
date_created: Optional[datetime] = None,
|
2022-04-27 04:23:43 +02:00
|
|
|
is_demo_organization: bool = False,
|
2021-06-16 21:15:47 +02:00
|
|
|
enable_read_receipts: Optional[bool] = None,
|
2022-04-27 04:23:43 +02:00
|
|
|
enable_spectator_access: Optional[bool] = None,
|
2023-03-10 11:42:18 +01:00
|
|
|
prereg_realm: Optional[PreregistrationRealm] = None,
|
2024-03-26 06:14:16 +01:00
|
|
|
how_realm_creator_found_zulip: Optional[str] = None,
|
|
|
|
how_realm_creator_found_zulip_extra_context: Optional[str] = None,
|
2022-04-14 23:58:15 +02:00
|
|
|
) -> Realm:
|
2023-11-20 20:16:03 +01:00
|
|
|
if string_id in [settings.SOCIAL_AUTH_SUBDOMAIN, settings.SELF_HOSTING_MANAGEMENT_SUBDOMAIN]:
|
|
|
|
raise AssertionError(
|
2023-11-29 02:51:10 +01:00
|
|
|
"Creating a realm on SOCIAL_AUTH_SUBDOMAIN or SELF_HOSTING_MANAGEMENT_SUBDOMAIN is not allowed!"
|
2023-11-20 20:16:03 +01:00
|
|
|
)
|
2022-04-14 23:58:15 +02:00
|
|
|
if Realm.objects.filter(string_id=string_id).exists():
|
|
|
|
raise AssertionError(f"Realm {string_id} already exists!")
|
|
|
|
if not server_initialized():
|
|
|
|
logging.info("Server not yet initialized. Creating the internal realm first.")
|
|
|
|
create_internal_realm()
|
|
|
|
|
|
|
|
kwargs: Dict[str, Any] = {}
|
|
|
|
if emails_restricted_to_domains is not None:
|
|
|
|
kwargs["emails_restricted_to_domains"] = emails_restricted_to_domains
|
|
|
|
if description is not None:
|
|
|
|
kwargs["description"] = description
|
|
|
|
if invite_required is not None:
|
|
|
|
kwargs["invite_required"] = invite_required
|
|
|
|
if plan_type is not None:
|
|
|
|
kwargs["plan_type"] = plan_type
|
|
|
|
if org_type is not None:
|
|
|
|
kwargs["org_type"] = org_type
|
2023-09-12 21:58:58 +02:00
|
|
|
if default_language is not None:
|
|
|
|
kwargs["default_language"] = default_language
|
2022-04-14 23:58:15 +02:00
|
|
|
if enable_spectator_access is not None:
|
2022-06-02 15:56:30 +02:00
|
|
|
if enable_spectator_access:
|
|
|
|
# Realms with LIMITED plan cannot have spectators enabled.
|
|
|
|
assert plan_type != Realm.PLAN_TYPE_LIMITED
|
|
|
|
assert plan_type is not None or not settings.BILLING_ENABLED
|
2022-04-14 23:58:15 +02:00
|
|
|
kwargs["enable_spectator_access"] = enable_spectator_access
|
|
|
|
|
|
|
|
if date_created is not None:
|
|
|
|
# The date_created parameter is intended only for use by test
|
|
|
|
# suites that want to backdate the date of a realm's creation.
|
|
|
|
assert not settings.PRODUCTION
|
|
|
|
kwargs["date_created"] = date_created
|
|
|
|
|
2021-06-16 21:15:47 +02:00
|
|
|
# Generally, closed organizations like companies want read
|
|
|
|
# receipts, whereas it's unclear what an open organization's
|
|
|
|
# preferences will be. We enable the setting by default only for
|
|
|
|
# closed organizations.
|
|
|
|
if enable_read_receipts is not None:
|
|
|
|
kwargs["enable_read_receipts"] = enable_read_receipts
|
|
|
|
else:
|
|
|
|
# Hacky: The default of invited_required is True, so we need
|
|
|
|
# to check for None too.
|
|
|
|
kwargs["enable_read_receipts"] = (
|
|
|
|
invite_required is None or invite_required is True or emails_restricted_to_domains
|
|
|
|
)
|
2023-11-23 22:07:41 +01:00
|
|
|
# Initialize this property correctly in the case that no network activity
|
|
|
|
# is required to do so correctly.
|
|
|
|
kwargs["push_notifications_enabled"] = sends_notifications_directly()
|
2021-06-16 21:15:47 +02:00
|
|
|
|
2022-04-14 23:58:15 +02:00
|
|
|
with transaction.atomic():
|
|
|
|
realm = Realm(string_id=string_id, name=name, **kwargs)
|
|
|
|
if is_demo_organization:
|
2023-11-19 19:45:19 +01:00
|
|
|
realm.demo_organization_scheduled_deletion_date = realm.date_created + timedelta(
|
|
|
|
days=settings.DEMO_ORG_DEADLINE_DAYS
|
2022-04-14 23:58:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
set_realm_permissions_based_on_org_type(realm)
|
2023-08-09 15:06:56 +02:00
|
|
|
|
|
|
|
# For now a dummy value of -1 is given to groups fields which
|
|
|
|
# is changed later before the transaction is committed.
|
2023-10-31 12:28:49 +01:00
|
|
|
for permission_configuration in Realm.REALM_PERMISSION_GROUP_SETTINGS.values():
|
|
|
|
setattr(realm, permission_configuration.id_field_name, -1)
|
2023-08-09 15:06:56 +02:00
|
|
|
|
2022-04-14 23:58:15 +02:00
|
|
|
realm.save()
|
|
|
|
|
|
|
|
RealmAuditLog.objects.create(
|
2022-04-16 00:24:37 +02:00
|
|
|
# acting_user will be set as the initial realm owner inside
|
|
|
|
# do_create_user(..., realm_creation=True).
|
|
|
|
acting_user=None,
|
|
|
|
realm=realm,
|
|
|
|
event_type=RealmAuditLog.REALM_CREATED,
|
|
|
|
event_time=realm.date_created,
|
2024-03-26 06:14:16 +01:00
|
|
|
extra_data={
|
|
|
|
"how_realm_creator_found_zulip": how_realm_creator_found_zulip,
|
|
|
|
"how_realm_creator_found_zulip_extra_context": how_realm_creator_found_zulip_extra_context,
|
|
|
|
},
|
2022-04-14 23:58:15 +02:00
|
|
|
)
|
|
|
|
|
2021-10-26 09:15:16 +02:00
|
|
|
realm_default_email_address_visibility = RealmUserDefault.EMAIL_ADDRESS_VISIBILITY_EVERYONE
|
2023-07-22 01:15:10 +02:00
|
|
|
if realm.org_type in (
|
|
|
|
Realm.ORG_TYPES["education_nonprofit"]["id"],
|
|
|
|
Realm.ORG_TYPES["education"]["id"],
|
2021-10-26 09:15:16 +02:00
|
|
|
):
|
|
|
|
# Email address of users should be initially visible to admins only.
|
|
|
|
realm_default_email_address_visibility = (
|
|
|
|
RealmUserDefault.EMAIL_ADDRESS_VISIBILITY_ADMINS
|
|
|
|
)
|
|
|
|
|
|
|
|
RealmUserDefault.objects.create(
|
|
|
|
realm=realm, email_address_visibility=realm_default_email_address_visibility
|
|
|
|
)
|
2022-04-14 23:58:15 +02:00
|
|
|
|
|
|
|
create_system_user_groups_for_realm(realm)
|
2023-08-09 15:06:56 +02:00
|
|
|
set_default_for_realm_permission_group_settings(realm)
|
2022-04-14 23:58:15 +02:00
|
|
|
|
2023-04-16 21:53:22 +02:00
|
|
|
RealmAuthenticationMethod.objects.bulk_create(
|
|
|
|
[
|
|
|
|
RealmAuthenticationMethod(name=backend_name, realm=realm)
|
2024-02-05 23:52:25 +01:00
|
|
|
for backend_name in all_default_backend_names()
|
2023-04-16 21:53:22 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2023-12-11 04:59:00 +01:00
|
|
|
maybe_enqueue_audit_log_upload(realm)
|
2023-12-01 14:52:44 +01:00
|
|
|
|
2022-04-14 23:58:15 +02:00
|
|
|
# Create stream once Realm object has been saved
|
2024-02-07 12:13:02 +01:00
|
|
|
new_stream_announcements_stream = ensure_stream(
|
2022-04-14 23:58:15 +02:00
|
|
|
realm,
|
|
|
|
Realm.DEFAULT_NOTIFICATION_STREAM_NAME,
|
|
|
|
stream_description="Everyone is added to this stream by default. Welcome! :octopus:",
|
|
|
|
acting_user=None,
|
|
|
|
)
|
2024-01-26 14:45:37 +01:00
|
|
|
# By default, 'New stream' & 'Zulip update' announcements are sent to the same stream.
|
2024-02-07 12:13:02 +01:00
|
|
|
realm.new_stream_announcements_stream = new_stream_announcements_stream
|
2024-01-26 14:45:37 +01:00
|
|
|
realm.zulip_update_announcements_stream = new_stream_announcements_stream
|
2022-04-14 23:58:15 +02:00
|
|
|
|
|
|
|
# With the current initial streams situation, the only public
|
2024-02-07 12:13:02 +01:00
|
|
|
# stream is the new_stream_announcements_stream.
|
|
|
|
DefaultStream.objects.create(stream=new_stream_announcements_stream, realm=realm)
|
2022-04-14 23:58:15 +02:00
|
|
|
|
2024-02-07 17:11:43 +01:00
|
|
|
signup_announcements_stream = ensure_stream(
|
2022-04-14 23:58:15 +02:00
|
|
|
realm,
|
|
|
|
Realm.INITIAL_PRIVATE_STREAM_NAME,
|
|
|
|
invite_only=True,
|
|
|
|
stream_description="A private stream for core team members.",
|
|
|
|
acting_user=None,
|
|
|
|
)
|
2024-02-07 17:11:43 +01:00
|
|
|
realm.signup_announcements_stream = signup_announcements_stream
|
2022-04-14 23:58:15 +02:00
|
|
|
|
2024-02-05 17:12:55 +01:00
|
|
|
# New realm is initialized with the latest zulip update announcements
|
|
|
|
# level as it shouldn't receive a bunch of old updates.
|
|
|
|
realm.zulip_update_announcements_level = get_latest_zulip_update_announcements_level()
|
|
|
|
|
2024-01-26 14:45:37 +01:00
|
|
|
realm.save(
|
|
|
|
update_fields=[
|
|
|
|
"new_stream_announcements_stream",
|
|
|
|
"signup_announcements_stream",
|
|
|
|
"zulip_update_announcements_stream",
|
2024-02-05 17:12:55 +01:00
|
|
|
"zulip_update_announcements_level",
|
2024-01-26 14:45:37 +01:00
|
|
|
]
|
|
|
|
)
|
2022-04-14 23:58:15 +02:00
|
|
|
|
|
|
|
if plan_type is None and settings.BILLING_ENABLED:
|
2022-04-16 00:24:37 +02:00
|
|
|
# We use acting_user=None for setting the initial plan type.
|
2022-04-14 23:58:15 +02:00
|
|
|
do_change_realm_plan_type(realm, Realm.PLAN_TYPE_LIMITED, acting_user=None)
|
|
|
|
|
2023-03-10 11:42:18 +01:00
|
|
|
if prereg_realm is not None:
|
|
|
|
prereg_realm.status = confirmation_settings.STATUS_USED
|
|
|
|
prereg_realm.created_realm = realm
|
|
|
|
prereg_realm.save(update_fields=["status", "created_realm"])
|
|
|
|
|
2023-03-10 16:03:18 +01:00
|
|
|
# Send a notification to the admin realm when a new organization registers.
|
|
|
|
if settings.CORPORATE_ENABLED:
|
|
|
|
admin_realm = get_realm(settings.SYSTEM_BOT_REALM)
|
|
|
|
sender = get_system_bot(settings.NOTIFICATION_BOT, admin_realm.id)
|
2022-04-14 23:58:15 +02:00
|
|
|
|
2023-12-06 15:00:23 +01:00
|
|
|
support_url = get_realm_support_url(realm)
|
2023-03-10 16:03:18 +01:00
|
|
|
organization_type = get_org_type_display_name(realm.org_type)
|
2022-04-14 23:58:15 +02:00
|
|
|
|
2024-03-21 03:43:05 +01:00
|
|
|
message = f"[{realm.name}]({support_url}) ([{realm.display_subdomain}]({realm.uri})). Organization type: {organization_type}"
|
2024-01-13 09:55:16 +01:00
|
|
|
topic_name = "new organizations"
|
2023-03-10 16:03:18 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
signups_stream = get_signups_stream(admin_realm)
|
|
|
|
|
|
|
|
internal_send_stream_message(
|
|
|
|
sender,
|
|
|
|
signups_stream,
|
2024-01-13 09:55:16 +01:00
|
|
|
topic_name,
|
2023-03-10 16:03:18 +01:00
|
|
|
message,
|
|
|
|
)
|
|
|
|
except Stream.DoesNotExist: # nocoverage
|
|
|
|
# If the signups stream hasn't been created in the admin
|
|
|
|
# realm, don't auto-create it to send to it; just do nothing.
|
|
|
|
pass
|
2022-04-14 23:58:15 +02:00
|
|
|
|
|
|
|
setup_realm_internal_bots(realm)
|
|
|
|
return realm
|