27 lines
899 B
Python
27 lines
899 B
Python
from __future__ import annotations
|
|
|
|
from govoplan_core.core.access import PrincipalRef, TenantRef, TenantResolver
|
|
from govoplan_core.db.session import get_database
|
|
from govoplan_tenancy.backend.db.models import Tenant
|
|
|
|
|
|
def _tenant_ref(tenant: Tenant) -> TenantRef:
|
|
return TenantRef(
|
|
id=tenant.id,
|
|
name=tenant.name,
|
|
slug=tenant.slug,
|
|
status="active" if tenant.is_active else "inactive",
|
|
)
|
|
|
|
|
|
class SqlTenantResolver(TenantResolver):
|
|
def get_tenant(self, tenant_id: str) -> TenantRef | None:
|
|
with get_database().session() as session:
|
|
tenant = session.get(Tenant, tenant_id)
|
|
return _tenant_ref(tenant) if tenant is not None else None
|
|
|
|
def current_tenant(self, principal: PrincipalRef) -> TenantRef | None:
|
|
if principal.tenant_id is None:
|
|
return None
|
|
return self.get_tenant(principal.tenant_id)
|