Files
vscode-workbench/PLANNING/tasks/D-01-租户成员关系.md
T

58 lines
2.8 KiB
Markdown
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.
# D-01 · 用户-租户 membership + 跨租户切换
| 字段 | 值 |
|---|---|
| 项目 | dealerhub · `Desktop/gj/dealerhub`(backend + frontend) |
| 优先级 | P0 · M3 |
| 建议模型 | **gpt-5.6-luna(先设计)** → deepseek-v4.1-flash(实现)/ glm-5.3-flash(回归) |
| 依赖 | 无 |
| 预估 | 3.5 天 |
## 一、背景(为什么做)
迭代 5 自列的"下一轮最高价值项"第一名(`backend/PROGRESS_AGI_ITERATION_5.md`):当前缺少用户—租户的服务端 membership/授权关系。这是系统从"能跑"到"能卖"的必经门槛——多租户 SaaS 的授权必须服务端强制,不能只靠前端过滤。
## 二、目标(交付物)
**阶段一:设计文档(gpt-5.6-luna 产出,先评审再动手)**
- membership 数据模型:`Membership(user, tenant, role[owner|admin|member], status[active|invited|disabled], joined_at)`
- 授权矩阵:每个角色对每个资源类别的 读/写/管理 权限表
- 跨租户切换流程:切换端点、会话/令牌携带方式、审计日志
- 兼容策略:存量单租户数据如何映射出 owner 关系(迁移路径)
**阶段二:实现**
1. `core` app 增 membership 模型 + 迁移(存量数据自动生成 owner)
2. **服务端强制校验**:TenantMiddleware / 权限层收口——所有租户资源接口验证当前用户对当前租户的 membership(禁止只依赖 query 参数)
3. 切换端点:`POST /api/tenants/{id}/switch`(校验 membership + 审计记录)
4. 成员管理接口:邀请 / 角色变更 / 禁用(owner/admin 分级权限)
5. 前端:租户切换器、成员管理页、无权限页
6. 回归 + 新增用例:跨租户越权矩阵(A 租户用户访问 B 租户资源 → 403/404)
## 三、验收标准
- [ ] 存量数据迁移后,每个租户有且仅有一个 owner(数据核对)
- [ ] **越权矩阵用例**:member 访问管理接口 403;非成员访问租户资源 403/404;跨租户读/写全被拒
- [ ] 跨租户切换端到端可用(前端切换器 + 会话隔离无脏数据)
- [ ] 成员管理分级正确(admin 不能踢 owner 等边界)
- [ ] 既有 460 用例零回归;`makemigrations --check --dry-run` 干净
- [ ] 设计文档与实现一致(评审记录进 PROGRESS)
## 四、验收命令(参考)
```bash
cd backend && pytest -q # 460+ 全绿
pytest -q -k membership # 新增用例
python manage.py makemigrations --check --dry-run
cd frontend && npm test && npm run build
```
## 五、边界(不许做)
- 不做计费/套餐(另一张卡的空间)
- 不改现有单据/库存业务逻辑(只加授权层)
- 不把权限逻辑塞进前端当唯一防线
## 六、交接
写 `PROGRESS_D-01.md`(含设计要点、越权矩阵用例输出、迁移核对数据)。