AidopTenantScope.cs 1.1 KB

12345678910111213141516171819202122232425262728
  1. namespace Admin.NET.Plugin.AiDOP.Infrastructure;
  2. /// <summary>
  3. /// 严格租户解析:只信认证后的 JWT。无有效租户或超管未选目标租户则抛异常。
  4. /// 业务数据的查询与写入一律用 <see cref="ResolveOrThrow"/>。
  5. /// </summary>
  6. public static class AidopTenantScope
  7. {
  8. /// <summary>
  9. /// 超管未选目标租户时的拒绝哨兵(框架默认租户)。绝不作为业务数据归属。
  10. /// </summary>
  11. public static long MainTenantId => SqlSugarConst.DefaultTenantId;
  12. /// <summary>
  13. /// 严格可信租户解析:只来自认证后 JWT;无 Token(&lt;=0) 或超管未选主租户 → 拒绝;
  14. /// 不读前端 tenantId、无默认回退。
  15. /// </summary>
  16. public static long ResolveOrThrow(UserManager userManager)
  17. {
  18. ArgumentNullException.ThrowIfNull(userManager);
  19. var tid = userManager.TenantId;
  20. if (tid <= 0)
  21. throw Oops.Oh("无法确定当前租户,请重新登录或选择目标租户");
  22. if (userManager.SuperAdmin && tid == MainTenantId)
  23. throw Oops.Oh("超级管理员操作前必须选择目标租户");
  24. return tid;
  25. }
  26. }