Files
huang 1b9080e3ab 实现角色权限体系重构
本次提交完成了角色权限体系的重构,主要包括:

1. 数据库迁移
   - 添加 tb_permission.platform 字段(all/web/h5)
   - 更新 tb_role.role_type 注释(1=平台角色,2=客户角色)

2. GORM 模型更新
   - Permission 模型添加 Platform 字段
   - Role 模型更新 RoleType 注释

3. 常量定义
   - 新增角色类型常量(RoleTypePlatform, RoleTypeCustomer)
   - 新增权限端口常量(PlatformAll, PlatformWeb, PlatformH5)
   - 添加角色类型与用户类型匹配规则函数

4. Store 层实现
   - Permission Store 支持按 platform 过滤
   - Account Role Store 添加 CountByAccountID 方法

5. Service 层实现
   - 角色分配支持类型匹配校验
   - 角色分配支持数量限制(超级管理员0个,平台用户无限制,代理/企业1个)
   - Permission Service 支持 platform 过滤

6. 权限校验中间件
   - 实现 RequirePermission、RequireAnyPermission、RequireAllPermissions
   - 支持 platform 字段过滤
   - 支持跳过超级管理员检查

7. 测试用例
   - 角色类型匹配规则单元测试
   - 角色分配数量限制单元测试
   - 权限 platform 过滤单元测试
   - 权限校验中间件集成测试(占位)

8. 代码清理
   - 删除过时的 subordinate 测试文件
   - 移除 Account.ParentID 相关引用
   - 更新 DTO 验证规则

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-10 09:51:52 +08:00

52 lines
1.9 KiB
Markdown
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.
# Change: 重构角色权限体系
## Why
当前系统的角色权限模型Role、Permission、AccountRole、RolePermission需要适配新的用户组织体系。主要问题
1. 角色类型role_type需要与新的用户类型对应
2. 权限缺少端口区分(某些权限只在 Web 后台有效,某些只在 H5 有效)
3. 账号-角色关联规则需要调整(平台用户可多角色,代理/企业只能单角色)
## What Changes
### 修改现有模型
- **Role**: 重新定义角色类型枚举(平台角色、客户角色)
- **Permission**: 添加 `platform` 字段支持按端口区分权限all/web/h5
- **AccountRole**: 添加角色数量限制逻辑
### 业务规则
1. **平台角色**: 用于区分平台用户的不同职责(运营、客服、管理等)
2. **客户角色**: 用于决定代理/企业客户的能力边界
3. **权限端口**:
- `all` - 通用权限Web 和 H5 均可用)
- `web` - 仅 Web 后台使用
- `h5` - 仅 H5 端使用
### 角色分配规则
| 用户类型 | 可分配角色类型 | 角色数量限制 |
|---------|--------------|-------------|
| 超级管理员 | 无需角色 | 0 |
| 平台用户 | 平台角色 | 多个 |
| 代理账号 | 客户角色 | 1个 |
| 企业账号 | 客户角色 | 1个 |
| 个人客户 | 无角色 | 0 |
## Impact
- **Affected specs**: role-permission (新建), auth
- **Affected code**:
- `internal/model/role.go` - 修改角色类型定义
- `internal/model/permission.go` - 添加 platform 字段
- `internal/store/postgres/account_role_store.go` - 添加角色数量校验
- `internal/service/` - 添加角色分配逻辑
- `migrations/` - 修改表结构迁移脚本
- `pkg/constants/` - 添加角色类型、权限端口常量
## 依赖关系
本提案依赖 **add-user-organization-model** 提案完成后执行,因为角色分配规则需要基于新的用户类型定义。