change to the tenant anagment
This commit is contained in:
+27
-9
@@ -166,6 +166,11 @@ def _find_registered_tenant_id(candidate):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _resolve_registered_tenant_id(candidate):
|
||||||
|
"""Return the canonical registered tenant id for a candidate, or None."""
|
||||||
|
return _find_registered_tenant_id(candidate)
|
||||||
|
|
||||||
|
|
||||||
def _is_ip_host(hostname):
|
def _is_ip_host(hostname):
|
||||||
hostname = str(hostname or '').strip()
|
hostname = str(hostname or '').strip()
|
||||||
if not hostname:
|
if not hostname:
|
||||||
@@ -185,6 +190,8 @@ def get_tenant_config(tenant_id=None):
|
|||||||
ctx = get_tenant_context()
|
ctx = get_tenant_context()
|
||||||
tenant_id = ctx.tenant_id if ctx and ctx.tenant_id else 'default'
|
tenant_id = ctx.tenant_id if ctx and ctx.tenant_id else 'default'
|
||||||
|
|
||||||
|
tenant_id = _resolve_registered_tenant_id(tenant_id) or tenant_id
|
||||||
|
|
||||||
if tenant_id in TENANT_REGISTRY:
|
if tenant_id in TENANT_REGISTRY:
|
||||||
return TENANT_REGISTRY[tenant_id] or {}
|
return TENANT_REGISTRY[tenant_id] or {}
|
||||||
|
|
||||||
@@ -481,7 +488,8 @@ class TenantContext:
|
|||||||
or request.args.get('tenantId', '').strip()
|
or request.args.get('tenantId', '').strip()
|
||||||
)
|
)
|
||||||
if tenant_from_query:
|
if tenant_from_query:
|
||||||
matched_tenant = _find_registered_tenant_id(tenant_from_query) or tenant_from_query
|
matched_tenant = _resolve_registered_tenant_id(tenant_from_query)
|
||||||
|
if matched_tenant:
|
||||||
self.tenant_id = matched_tenant
|
self.tenant_id = matched_tenant
|
||||||
self.config = get_tenant_config(matched_tenant)
|
self.config = get_tenant_config(matched_tenant)
|
||||||
session['tenant_id'] = matched_tenant
|
session['tenant_id'] = matched_tenant
|
||||||
@@ -490,7 +498,8 @@ class TenantContext:
|
|||||||
# Priority 1: X-Tenant-ID header (for testing/internal APIs)
|
# Priority 1: X-Tenant-ID header (for testing/internal APIs)
|
||||||
tenant_from_header = request.headers.get('X-Tenant-ID', '').strip()
|
tenant_from_header = request.headers.get('X-Tenant-ID', '').strip()
|
||||||
if tenant_from_header:
|
if tenant_from_header:
|
||||||
matched_tenant = _find_registered_tenant_id(tenant_from_header) or tenant_from_header
|
matched_tenant = _resolve_registered_tenant_id(tenant_from_header)
|
||||||
|
if matched_tenant:
|
||||||
self.tenant_id = matched_tenant
|
self.tenant_id = matched_tenant
|
||||||
self.config = get_tenant_config(matched_tenant)
|
self.config = get_tenant_config(matched_tenant)
|
||||||
session['tenant_id'] = matched_tenant
|
session['tenant_id'] = matched_tenant
|
||||||
@@ -544,11 +553,11 @@ class TenantContext:
|
|||||||
if len(parts) >= 2:
|
if len(parts) >= 2:
|
||||||
potential_subdomain = parts[0]
|
potential_subdomain = parts[0]
|
||||||
if potential_subdomain not in ('www', 'api', 'admin', 'app', 'mail'):
|
if potential_subdomain not in ('www', 'api', 'admin', 'app', 'mail'):
|
||||||
matched_tenant = _find_registered_tenant_id(potential_subdomain)
|
matched_tenant = _resolve_registered_tenant_id(potential_subdomain)
|
||||||
if not matched_tenant and potential_subdomain.startswith('school'):
|
if not matched_tenant and potential_subdomain.startswith('school'):
|
||||||
matched_tenant = _find_registered_tenant_id('schule' + potential_subdomain[len('school'):])
|
matched_tenant = _resolve_registered_tenant_id('schule' + potential_subdomain[len('school'):])
|
||||||
elif not matched_tenant and potential_subdomain.startswith('schule'):
|
elif not matched_tenant and potential_subdomain.startswith('schule'):
|
||||||
matched_tenant = _find_registered_tenant_id('school' + potential_subdomain[len('schule'):])
|
matched_tenant = _resolve_registered_tenant_id('school' + potential_subdomain[len('schule'):])
|
||||||
if matched_tenant:
|
if matched_tenant:
|
||||||
self.subdomain = potential_subdomain
|
self.subdomain = potential_subdomain
|
||||||
self.tenant_id = matched_tenant
|
self.tenant_id = matched_tenant
|
||||||
@@ -565,12 +574,21 @@ class TenantContext:
|
|||||||
# Priority 4: sticky tenant from the authenticated session
|
# Priority 4: sticky tenant from the authenticated session
|
||||||
session_tenant = session.get('tenant_id', '').strip() if session.get('tenant_id') else ''
|
session_tenant = session.get('tenant_id', '').strip() if session.get('tenant_id') else ''
|
||||||
if session_tenant:
|
if session_tenant:
|
||||||
self.tenant_id = session_tenant
|
matched_tenant = _resolve_registered_tenant_id(session_tenant)
|
||||||
self.config = get_tenant_config(session_tenant)
|
if matched_tenant:
|
||||||
|
self.tenant_id = matched_tenant
|
||||||
|
self.config = get_tenant_config(matched_tenant)
|
||||||
|
session['tenant_id'] = matched_tenant
|
||||||
logger.info(
|
logger.info(
|
||||||
f"Tenant resolution by session: host={primary_host} tenant={session_tenant} config={self.config}"
|
f"Tenant resolution by session: host={primary_host} tenant={matched_tenant} config={self.config}"
|
||||||
|
)
|
||||||
|
return self._get_db_name(matched_tenant)
|
||||||
|
session.pop('tenant_id', None)
|
||||||
|
logger.info(
|
||||||
|
"Discarded stale tenant session reference: host=%s tenant=%s",
|
||||||
|
primary_host,
|
||||||
|
session_tenant,
|
||||||
)
|
)
|
||||||
return self._get_db_name(session_tenant)
|
|
||||||
|
|
||||||
# Fallback to default tenant if no tenant identifier found.
|
# Fallback to default tenant if no tenant identifier found.
|
||||||
# If no explicit 'default' tenant config exists, use configured MongoDB DB.
|
# If no explicit 'default' tenant config exists, use configured MongoDB DB.
|
||||||
|
|||||||
Reference in New Issue
Block a user