PMFlow/backend/app/dependencies/tenant.py

31 lines
727 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
租户过滤依赖
从请求中提取 tenant_id,用于多租户数据隔离。
"""
from typing import Annotated
from fastapi import Header
from ..utils.audited_write import update_audit_context
async def get_tenant_id(
x_tenant_id: Annotated[str | None, Header(alias="X-Tenant-ID")] = None,
) -> int:
"""
从 Header X-Tenant-ID 或 JWT 解析 tenant_id。
未提供时返回默认租户 1。
"""
if x_tenant_id:
try:
resolved = int(x_tenant_id)
update_audit_context(tenant_id=resolved)
return resolved
except ValueError:
update_audit_context(tenant_id=1)
return 1
update_audit_context(tenant_id=1)
return 1