mirror of
https://github.com/zulip/zulip.git
synced 2025-10-28 02:23:57 +00:00
The main limitation of this version is that it's controlled entirely from settings, with nothing in the database and no web UI or even management command to control it. That makes it a bit more of a burden for the server admins than it'd ideally be, but that's fine for now. Relatedly, the web flow for realm creation still requires choosing a subdomain even if the realm is destined to live at an alias domain. Specific to the dev environment, there is an annoying quirk: the special dev login flow doesn't work on a REALM_HOSTS realm. Also, in this version the `add_new_realm` and `add_new_user` management commands, which are intended for use in development environments only, don't support this feature. In manual testing, I've confirmed that a REALM_HOSTS realm works for signup and login, with email/password, Google SSO, or GitHub SSO. Most of that was in dev; I used zulipstaging.com to also test * logging in with email and password; * logging in with Google SSO... far enough to correctly determine that my email address is associated with some other realm.
57 lines
2.0 KiB
Python
57 lines
2.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from django.conf import settings
|
|
from django.http import HttpRequest
|
|
import re
|
|
from typing import Optional, Text
|
|
|
|
from zerver.models import get_realm, Realm, UserProfile
|
|
|
|
def get_subdomain(request):
|
|
# type: (HttpRequest) -> Text
|
|
|
|
# The HTTP spec allows, but doesn't require, a client to omit the
|
|
# port in the `Host` header if it's "the default port for the
|
|
# service requested", i.e. typically either 443 or 80; and
|
|
# whatever Django gets there, or from proxies reporting that via
|
|
# X-Forwarded-Host, it passes right through the same way. So our
|
|
# logic is a bit complicated to allow for that variation.
|
|
#
|
|
# For both EXTERNAL_HOST and REALM_HOSTS, we take a missing port
|
|
# to mean that any port should be accepted in Host. It's not
|
|
# totally clear that's the right behavior, but it keeps
|
|
# compatibility with older versions of Zulip, so that's a start.
|
|
|
|
host = request.get_host().lower()
|
|
|
|
m = re.search('\.%s(:\d+)?$' % (settings.EXTERNAL_HOST,),
|
|
host)
|
|
if m:
|
|
subdomain = host[:m.start()]
|
|
if subdomain in settings.ROOT_SUBDOMAIN_ALIASES:
|
|
return Realm.SUBDOMAIN_FOR_ROOT_DOMAIN
|
|
return subdomain
|
|
|
|
for subdomain, realm_host in settings.REALM_HOSTS.items():
|
|
if re.search('^%s(:\d+)?$' % (realm_host,),
|
|
host):
|
|
return subdomain
|
|
|
|
return Realm.SUBDOMAIN_FOR_ROOT_DOMAIN
|
|
|
|
def is_subdomain_root_or_alias(request):
|
|
# type: (HttpRequest) -> bool
|
|
return get_subdomain(request) == Realm.SUBDOMAIN_FOR_ROOT_DOMAIN
|
|
|
|
def user_matches_subdomain(realm_subdomain, user_profile):
|
|
# type: (Optional[Text], UserProfile) -> bool
|
|
if realm_subdomain is None:
|
|
return True
|
|
return user_profile.realm.subdomain == realm_subdomain
|
|
|
|
def is_root_domain_available():
|
|
# type: () -> bool
|
|
if settings.ROOT_DOMAIN_LANDING_PAGE:
|
|
return False
|
|
return get_realm(Realm.SUBDOMAIN_FOR_ROOT_DOMAIN) is None
|