mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-23 13:13:09 +00:00
- 修复多处代码缩进、换行不规范问题 - 调整部分配置注释与字符串格式对齐 - 优化导入语句与多行表达式的排版 - 统一枚举、模型字段的注释风格 - 调整部分校验逻辑与提示文案 - 重构部分长函数参数与查询语句格式
55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
"""多租户上下文管理
|
|
|
|
使用 Python contextvars 实现异步安全的租户上下文传递。
|
|
在整个请求生命周期中,通过 ContextVar 存储当前请求的租户 ID
|
|
和超级管理员标识,供 ORM 事件处理器、依赖注入等模块使用。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from contextvars import ContextVar
|
|
|
|
# 当前请求的租户 ID(None 表示未设置或公开接口)
|
|
current_tenant_id: ContextVar[int | None] = ContextVar("current_tenant_id", default=None)
|
|
|
|
# 当前用户是否为超级管理员
|
|
current_is_super_admin: ContextVar[bool] = ContextVar("current_is_super_admin", default=False)
|
|
|
|
|
|
def set_current_tenant(tenant_id: int | None, is_super_admin: bool = False) -> None:
|
|
"""设置当前请求的租户上下文。
|
|
|
|
参数:
|
|
tenant_id: 租户 ID,None 表示无租户上下文(公开接口或未认证)。
|
|
is_super_admin: 是否为平台超级管理员,默认 False。
|
|
"""
|
|
current_tenant_id.set(tenant_id)
|
|
current_is_super_admin.set(is_super_admin)
|
|
|
|
|
|
def get_current_tenant_id() -> int | None:
|
|
"""获取当前请求的租户 ID。
|
|
|
|
返回:
|
|
int | None: 租户 ID,未设置时返回 None。
|
|
"""
|
|
return current_tenant_id.get()
|
|
|
|
|
|
def get_is_super_admin() -> bool:
|
|
"""获取当前用户是否为超级管理员。
|
|
|
|
返回:
|
|
bool: 是否为超级管理员,默认 False。
|
|
"""
|
|
return current_is_super_admin.get()
|
|
|
|
|
|
def clear_current_tenant() -> None:
|
|
"""清除当前请求的租户上下文(重置为默认值)。
|
|
|
|
应在请求结束时调用,防止 ContextVar 泄漏到其他请求。
|
|
"""
|
|
current_tenant_id.set(None)
|
|
current_is_super_admin.set(False)
|