Files
zulip/zerver/lib/subdomains.py
Greg Price c9457d4af0 subdomains: Refactor check_subdomain to a clearer interface.
Now that every call site of check_subdomain produces its second
argument in exactly the same way, push that shared bit of logic
into a new wrapper for check_subdomain.

Also give that new function a name that says more specifically what
it's checking -- which I think is easier to articulate for this
interface than for that of check_subdomain.
2017-10-26 10:29:17 -07:00

45 lines
1.4 KiB
Python

# -*- coding: utf-8 -*-
from django.conf import settings
from django.http import HttpRequest
from typing import Optional, Text
from zerver.models import get_realm, Realm, UserProfile
def _extract_subdomain(request):
# type: (HttpRequest) -> Text
domain = request.get_host().lower()
index = domain.find("." + settings.EXTERNAL_HOST)
if index == -1:
return Realm.SUBDOMAIN_FOR_ROOT_DOMAIN
return domain[0:index]
def get_subdomain(request):
# type: (HttpRequest) -> Text
subdomain = _extract_subdomain(request)
if subdomain in settings.ROOT_SUBDOMAIN_ALIASES:
return Realm.SUBDOMAIN_FOR_ROOT_DOMAIN
return subdomain
def is_subdomain_root_or_alias(request):
# type: (HttpRequest) -> bool
subdomain = _extract_subdomain(request)
return not subdomain or subdomain in settings.ROOT_SUBDOMAIN_ALIASES
def check_subdomain(realm_subdomain, user_subdomain):
# type: (Optional[Text], Text) -> bool
if realm_subdomain is not None:
if realm_subdomain != user_subdomain:
return False
return True
def user_matches_subdomain(realm_subdomain, user_profile):
# type: (Optional[Text], UserProfile) -> bool
return check_subdomain(realm_subdomain, user_profile.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