mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 21:43:21 +00:00
So far, we've used the BitField .authentication_methods on Realm for tracking which backends are enabled for an organization. This however made it a pain to add new backends (requiring altering the column and a migration - particularly troublesome if someone wanted to create their own custom auth backend for their server). Instead this will be tracked through the existence of the appropriate rows in the RealmAuthenticationMethods table.
78 lines
2.5 KiB
Python
78 lines
2.5 KiB
Python
from typing import Iterable, Optional, Tuple
|
|
|
|
from django.conf import settings
|
|
|
|
from zerver.lib.bulk_create import bulk_create_users
|
|
from zerver.lib.user_groups import create_system_user_groups_for_realm
|
|
from zerver.models import (
|
|
Realm,
|
|
RealmAuditLog,
|
|
RealmAuthenticationMethod,
|
|
RealmUserDefault,
|
|
UserProfile,
|
|
get_client,
|
|
get_system_bot,
|
|
)
|
|
from zproject.backends import all_implemented_backend_names
|
|
|
|
|
|
def server_initialized() -> bool:
|
|
return Realm.objects.exists()
|
|
|
|
|
|
def create_internal_realm() -> None:
|
|
from zerver.actions.users import do_change_can_forge_sender
|
|
|
|
realm = Realm.objects.create(string_id=settings.SYSTEM_BOT_REALM, name="System bot realm")
|
|
RealmAuditLog.objects.create(
|
|
realm=realm, event_type=RealmAuditLog.REALM_CREATED, event_time=realm.date_created
|
|
)
|
|
RealmUserDefault.objects.create(realm=realm)
|
|
create_system_user_groups_for_realm(realm)
|
|
|
|
# We create realms with all authentications methods enabled by default.
|
|
RealmAuthenticationMethod.objects.bulk_create(
|
|
[
|
|
RealmAuthenticationMethod(name=backend_name, realm=realm)
|
|
for backend_name in all_implemented_backend_names()
|
|
]
|
|
)
|
|
|
|
# Create some client objects for common requests. Not required;
|
|
# just ensures these get low IDs in production, and in development
|
|
# avoids an extra database write for the first HTTP request in
|
|
# most tests.
|
|
get_client("website")
|
|
get_client("ZulipMobile")
|
|
get_client("ZulipElectron")
|
|
|
|
internal_bots = [
|
|
(bot["name"], bot["email_template"] % (settings.INTERNAL_BOT_DOMAIN,))
|
|
for bot in settings.INTERNAL_BOTS
|
|
]
|
|
create_users(realm, internal_bots, bot_type=UserProfile.DEFAULT_BOT)
|
|
# Set the owners for these bots to the bots themselves
|
|
bots = UserProfile.objects.filter(email__in=[bot_info[1] for bot_info in internal_bots])
|
|
for bot in bots:
|
|
bot.bot_owner = bot
|
|
bot.save()
|
|
|
|
# Initialize the email gateway bot as able to forge senders.
|
|
email_gateway_bot = get_system_bot(settings.EMAIL_GATEWAY_BOT, realm.id)
|
|
do_change_can_forge_sender(email_gateway_bot, True)
|
|
|
|
|
|
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:
|
|
user_set = set()
|
|
for full_name, email in name_list:
|
|
user_set.add((email, full_name, True))
|
|
bulk_create_users(
|
|
realm, user_set, bot_type=bot_type, bot_owner=bot_owner, tos_version=tos_version
|
|
)
|