Files
wms-saas/.cursor/rules/tenant-isolation.mdc
2026-09-30 11:11:54 +08:00

28 lines
1.3 KiB
Plaintext
Raw Permalink 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.
---
description: 多租户 tenant_code 隔离规则(后端模型、查询、任务、缓存)
globs: backend/app/**/*.php,backend/database/**/*.php,backend/routes/**/*.php
alwaysApply: false
---
# 租户隔离
详见 `docs/03-multi-tenancy.md`。**严禁租户 A 查询或修改租户 B 的数据。**
- 方案:单数据库 + `tenant_code`;所有核心业务表必须有 `tenant_code`。
- 通过 TenantContext + 全局 Scope / Trait 自动过滤查询、自动填充 `tenant_code`,不要到处手写 where。
```php
// ❌ 手工拼接,容易遗漏
Sku::where('tenant_code', $request->user()->tenant_code)->get();
// ✅ 模型使用租户 Trait,查询自动限定当前租户
Sku::query()->get();
```
- 绕过租户 Scope(`withoutGlobalScope`)只允许出现在平台级管理功能中,并需注释说明原因。
- 根据业务编码查找数据时,也必须受租户限制(编码只在租户内唯一)。
- Queue Job 必须携带 `tenant_code`,执行时恢复 TenantContext。
- Scheduler / Console Command 按租户遍历执行,不能一次性处理全部租户数据而不区分。
- Webhook 根据店铺 / 应用凭证解析租户后再处理。
- 缓存 Key、锁 Key 必须带 `tenant_code` 前缀。
- 新增接口需有租户隔离测试。