authentik.tenants.utils

Tenant utils

 1"""Tenant utils"""
 2
 3from django.db import connection
 4from django_tenants.utils import get_public_schema_name
 5
 6from authentik.lib.config import CONFIG
 7from authentik.root.install_id import get_install_id
 8from authentik.tenants.models import Tenant
 9
10
11def get_current_tenant(only: list[str] | None = None) -> Tenant:
12    """Get tenant for current request"""
13    if only is None:
14        only = []
15    return Tenant.objects.only(*only).get(schema_name=connection.schema_name)
16
17
18def get_unique_identifier() -> str:
19    """Get a globally unique identifier that does not change"""
20    install_id = get_install_id()
21    if CONFIG.get_bool("tenants.enabled"):
22        tenant = get_current_tenant()
23        # Only use tenant's uuid if this request is not from the "public"
24        # (i.e. default) tenant
25        if tenant.schema_name == get_public_schema_name():
26            return install_id
27        return str(get_current_tenant().tenant_uuid)
28    return install_id
def get_current_tenant(only: list[str] | None = None) -> authentik.tenants.models.Tenant:
12def get_current_tenant(only: list[str] | None = None) -> Tenant:
13    """Get tenant for current request"""
14    if only is None:
15        only = []
16    return Tenant.objects.only(*only).get(schema_name=connection.schema_name)

Get tenant for current request

def get_unique_identifier() -> str:
19def get_unique_identifier() -> str:
20    """Get a globally unique identifier that does not change"""
21    install_id = get_install_id()
22    if CONFIG.get_bool("tenants.enabled"):
23        tenant = get_current_tenant()
24        # Only use tenant's uuid if this request is not from the "public"
25        # (i.e. default) tenant
26        if tenant.schema_name == get_public_schema_name():
27            return install_id
28        return str(get_current_tenant().tenant_uuid)
29    return install_id

Get a globally unique identifier that does not change