Jun 28 - Code optimize - Fix Medium/Low issues
This commit is contained in:
+34
-27
@@ -68,33 +68,40 @@ def init_tenancy(app):
|
||||
# tenant's DB. The session is server-signed so this is safe.
|
||||
imp_id = session.get('impersonating_tenant_id')
|
||||
if imp_id is not None:
|
||||
from control.base import control_session
|
||||
from control.models import Tenant
|
||||
from app.tenancy.context import TenantContext
|
||||
with control_session() as s:
|
||||
t = s.get(Tenant, imp_id)
|
||||
if t and t.status == 'active':
|
||||
plan = t.plan
|
||||
ctx = TenantContext(
|
||||
id=t.id,
|
||||
slug=t.slug,
|
||||
name=t.name,
|
||||
plan_id=t.plan_id,
|
||||
db_uri=t.db_uri,
|
||||
plan_code=plan.code if plan else None,
|
||||
max_users=plan.max_users if plan else None,
|
||||
max_facilities=plan.max_facilities if plan else None,
|
||||
max_inspections_month=plan.max_inspections_month if plan else None,
|
||||
max_issues_month=plan.max_issues_month if plan else None,
|
||||
allow_mobile_api=plan.allow_mobile_api if plan else True,
|
||||
allow_scheduled_reports=plan.allow_scheduled_reports if plan else True,
|
||||
allow_branding=plan.allow_branding if plan else True,
|
||||
allow_custom_domain=plan.allow_custom_domain if plan else True,
|
||||
)
|
||||
g.tenant = ctx
|
||||
g.tenant_engine = get_tenant_engine(ctx)
|
||||
return # skip normal Host resolution
|
||||
# Impersonation target invalid or suspended — clear and fall through
|
||||
try:
|
||||
from control.base import control_session
|
||||
from control.models import Tenant
|
||||
from app.tenancy.context import TenantContext
|
||||
with control_session() as s:
|
||||
t = s.get(Tenant, imp_id)
|
||||
if t and t.status == 'active':
|
||||
plan = t.plan
|
||||
ctx = TenantContext(
|
||||
id=t.id,
|
||||
slug=t.slug,
|
||||
name=t.name,
|
||||
plan_id=t.plan_id,
|
||||
db_uri=t.db_uri,
|
||||
plan_code=plan.code if plan else None,
|
||||
max_users=plan.max_users if plan else None,
|
||||
max_facilities=plan.max_facilities if plan else None,
|
||||
max_inspections_month=plan.max_inspections_month if plan else None,
|
||||
max_issues_month=plan.max_issues_month if plan else None,
|
||||
allow_mobile_api=plan.allow_mobile_api if plan else True,
|
||||
allow_scheduled_reports=plan.allow_scheduled_reports if plan else True,
|
||||
allow_branding=plan.allow_branding if plan else True,
|
||||
allow_custom_domain=plan.allow_custom_domain if plan else True,
|
||||
)
|
||||
g.tenant = ctx
|
||||
g.tenant_engine = get_tenant_engine(ctx)
|
||||
return # skip normal Host resolution
|
||||
except Exception:
|
||||
import logging as _logging
|
||||
_logging.getLogger(__name__).warning(
|
||||
'TENANCY | impersonation_failed | tenant_id=%s', imp_id
|
||||
)
|
||||
# Tenant not found, suspended, or engine error — clear stale session
|
||||
# keys so the next request doesn't retry a permanently failing lookup.
|
||||
session.pop('impersonating_tenant_id', None)
|
||||
session.pop('impersonating_superadmin_id', None)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user