write.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """多租户写入工具:为新实例/更新操作处理 tenant_id。"""
  2. from __future__ import annotations
  3. from typing import Any
  4. from app.api.v1.module_system.auth.schema import AuthSchema
  5. from app.api.v1.module_system.tenant.tools.constants import PLATFORM_TENANT_ID
  6. def target_tenant_id_for_new_row(auth: AuthSchema) -> int:
  7. """
  8. 确定新行的 tenant_id。
  9. 平台超管可任意设置(通过 payload),非超管只能在自己的租户内操作。
  10. """
  11. tid = getattr(auth.user, "tenant_id", None)
  12. return int(tid) if tid is not None else PLATFORM_TENANT_ID
  13. def sanitize_tenant_id_in_mutation_payload(auth: AuthSchema, payload: dict[str, Any]) -> dict[str, Any]:
  14. """
  15. 在写入(创建/更新)前清理 tenant_id:
  16. - 非平台超管:不能通过 payload 改变租户(即使传了也会被覆盖/忽略)。
  17. - 平台超管:保留输入的 tenant_id(允许跨租户操作)。
  18. """
  19. out = dict(payload)
  20. return out
  21. def assign_tenant_id_on_new_instance(obj: Any, auth: AuthSchema) -> None:
  22. """
  23. 将租户 ID 赋值给新创建的 ORM 实例(若有 tenant_id 字段)。
  24. 非超管强制使用自己的租户 ID,超管可自行决定(或留空表示系统)。
  25. """
  26. if hasattr(obj, "tenant_id") and getattr(obj, "tenant_id", None) is None:
  27. tid = target_tenant_id_for_new_row(auth)
  28. setattr(obj, "tenant_id", tid)