2020-06-11 00:54:34 +02:00
|
|
|
from typing import Iterable, Optional, Tuple
|
|
|
|
|
2020-01-27 16:12:43 +01:00
|
|
|
from django.conf import settings
|
2023-06-07 14:11:31 +02:00
|
|
|
from django.db import transaction
|
2020-01-27 16:12:43 +01:00
|
|
|
|
|
|
|
from zerver.lib.bulk_create import bulk_create_users
|
2022-06-28 19:04:17 +02:00
|
|
|
from zerver.lib.user_groups import create_system_user_groups_for_realm
|
2021-10-21 13:16:26 +02:00
|
|
|
from zerver.models import (
|
|
|
|
Realm,
|
|
|
|
RealmAuditLog,
|
2023-04-16 21:53:22 +02:00
|
|
|
RealmAuthenticationMethod,
|
2021-10-21 13:16:26 +02:00
|
|
|
RealmUserDefault,
|
|
|
|
UserProfile,
|
|
|
|
)
|
2023-12-15 04:33:19 +01:00
|
|
|
from zerver.models.clients import get_client
|
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
|
2020-01-27 16:12:43 +01:00
|
|
|
|
|
|
|
|
2020-01-30 14:43:46 +01:00
|
|
|
def server_initialized() -> bool:
|
2020-02-12 16:39:12 +01:00
|
|
|
return Realm.objects.exists()
|
2020-01-30 14:43:46 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2023-06-07 14:11:31 +02:00
|
|
|
@transaction.atomic(durable=True)
|
2020-01-27 16:12:43 +01:00
|
|
|
def create_internal_realm() -> None:
|
2023-08-09 15:06:56 +02:00
|
|
|
from zerver.actions.create_realm import set_default_for_realm_permission_group_settings
|
2022-04-14 23:48:28 +02:00
|
|
|
from zerver.actions.users import do_change_can_forge_sender
|
2020-02-12 16:39:12 +01:00
|
|
|
|
2023-08-09 15:06:56 +02:00
|
|
|
realm = Realm(string_id=settings.SYSTEM_BOT_REALM, name="System bot realm")
|
|
|
|
|
|
|
|
# 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
|
|
|
realm.save()
|
|
|
|
|
2021-04-20 12:29:19 +02:00
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=realm, event_type=RealmAuditLog.REALM_CREATED, event_time=realm.date_created
|
|
|
|
)
|
2021-10-21 13:16:26 +02:00
|
|
|
RealmUserDefault.objects.create(realm=realm)
|
2022-06-28 19:04:17 +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)
|
2020-01-27 16:12:43 +01: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
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-02-25 02:38:11 +01:00
|
|
|
# Create some client objects for common requests. Not required;
|
|
|
|
# just ensures these get low IDs in production, and in development
|
2022-02-08 00:13:33 +01:00
|
|
|
# avoids an extra database write for the first HTTP request in
|
2020-02-25 02:38:11 +01:00
|
|
|
# most tests.
|
2023-09-27 02:55:39 +02:00
|
|
|
get_client("Internal")
|
2020-01-27 16:21:32 +01:00
|
|
|
get_client("website")
|
2020-02-25 02:38:11 +01:00
|
|
|
get_client("ZulipMobile")
|
|
|
|
get_client("ZulipElectron")
|
2020-01-27 16:21:32 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
internal_bots = [
|
2021-02-12 08:20:45 +01:00
|
|
|
(bot["name"], bot["email_template"] % (settings.INTERNAL_BOT_DOMAIN,))
|
2021-02-12 08:19:30 +01:00
|
|
|
for bot in settings.INTERNAL_BOTS
|
|
|
|
]
|
2020-01-28 14:50:18 +01:00
|
|
|
create_users(realm, internal_bots, bot_type=UserProfile.DEFAULT_BOT)
|
2020-01-28 14:41:08 +01:00
|
|
|
# Set the owners for these bots to the bots themselves
|
2020-01-28 14:50:18 +01:00
|
|
|
bots = UserProfile.objects.filter(email__in=[bot_info[1] for bot_info in internal_bots])
|
2020-01-28 14:41:08 +01:00
|
|
|
for bot in bots:
|
|
|
|
bot.bot_owner = bot
|
|
|
|
bot.save()
|
2020-01-27 16:12:43 +01:00
|
|
|
|
2020-12-20 14:21:42 +01:00
|
|
|
# Initialize the email gateway bot as able to forge senders.
|
2021-07-26 17:05:18 +02:00
|
|
|
email_gateway_bot = get_system_bot(settings.EMAIL_GATEWAY_BOT, realm.id)
|
2020-12-20 14:21:42 +01:00
|
|
|
do_change_can_forge_sender(email_gateway_bot, True)
|
2020-01-27 16:12:43 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def create_users(
|
|
|
|
realm: Realm,
|
|
|
|
name_list: Iterable[Tuple[str, str]],
|
|
|
|
tos_version: Optional[str] = None,
|
|
|
|
bot_type: Optional[int] = None,
|
|
|
|
bot_owner: Optional[UserProfile] = None,
|
|
|
|
) -> None:
|
2020-01-28 14:39:19 +01:00
|
|
|
user_set = set()
|
2020-01-27 16:12:43 +01:00
|
|
|
for full_name, email in name_list:
|
2020-07-16 14:10:43 +02:00
|
|
|
user_set.add((email, full_name, True))
|
2021-02-12 08:19:30 +01:00
|
|
|
bulk_create_users(
|
|
|
|
realm, user_set, bot_type=bot_type, bot_owner=bot_owner, tos_version=tos_version
|
|
|
|
)
|