实现角色权限体系重构

本次提交完成了角色权限体系的重构,主要包括:

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>
This commit is contained in:
2026-01-10 09:51:52 +08:00
parent a36e4a79c0
commit 1b9080e3ab
31 changed files with 1767 additions and 607 deletions

View File

@@ -167,7 +167,7 @@ func TestRoleAPI_Create(t *testing.T) {
reqBody := model.CreateRoleRequest{
RoleName: "测试角色",
RoleDesc: "这是一个测试角色",
RoleType: constants.RoleTypeSuper,
RoleType: constants.RoleTypePlatform,
}
jsonBody, _ := json.Marshal(reqBody)
@@ -224,7 +224,7 @@ func TestRoleAPI_Get(t *testing.T) {
// 创建测试角色
testRole := &model.Role{
RoleName: "获取测试角色",
RoleType: constants.RoleTypeSuper,
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
env.db.Create(testRole)
@@ -269,7 +269,7 @@ func TestRoleAPI_Update(t *testing.T) {
// 创建测试角色
testRole := &model.Role{
RoleName: "更新测试角色",
RoleType: constants.RoleTypeSuper,
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
env.db.Create(testRole)
@@ -312,7 +312,7 @@ func TestRoleAPI_Delete(t *testing.T) {
// 创建测试角色
testRole := &model.Role{
RoleName: "删除测试角色",
RoleType: constants.RoleTypeSuper,
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
env.db.Create(testRole)
@@ -347,7 +347,7 @@ func TestRoleAPI_List(t *testing.T) {
for i := 1; i <= 5; i++ {
role := &model.Role{
RoleName: fmt.Sprintf("列表测试角色_%d", i),
RoleType: constants.RoleTypeSuper,
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
env.db.Create(role)
@@ -382,7 +382,7 @@ func TestRoleAPI_AssignPermissions(t *testing.T) {
// 创建测试角色
testRole := &model.Role{
RoleName: "权限分配测试角色",
RoleType: constants.RoleTypeSuper,
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
env.db.Create(testRole)
@@ -432,7 +432,7 @@ func TestRoleAPI_GetPermissions(t *testing.T) {
// 创建测试角色
testRole := &model.Role{
RoleName: "获取权限测试角色",
RoleType: constants.RoleTypeSuper,
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
env.db.Create(testRole)
@@ -482,7 +482,7 @@ func TestRoleAPI_RemovePermission(t *testing.T) {
// 创建测试角色
testRole := &model.Role{
RoleName: "移除权限测试角色",
RoleType: constants.RoleTypeSuper,
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
env.db.Create(testRole)