实现角色权限体系重构
本次提交完成了角色权限体系的重构,主要包括: 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:
@@ -97,7 +97,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
|
||||
// 创建测试角色
|
||||
role := &model.Role{
|
||||
RoleName: "单角色测试",
|
||||
RoleType: constants.RoleTypeSuper,
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(role)
|
||||
@@ -127,7 +127,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
|
||||
for i := 0; i < 3; i++ {
|
||||
roles[i] = &model.Role{
|
||||
RoleName: "多角色测试_" + string(rune('A'+i)),
|
||||
RoleType: constants.RoleTypeSuper,
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(roles[i])
|
||||
@@ -154,7 +154,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
|
||||
// 创建并分配角色
|
||||
role := &model.Role{
|
||||
RoleName: "获取角色列表测试",
|
||||
RoleType: constants.RoleTypeSuper,
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(role)
|
||||
@@ -183,7 +183,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
|
||||
// 创建并分配角色
|
||||
role := &model.Role{
|
||||
RoleName: "移除角色测试",
|
||||
RoleType: constants.RoleTypeSuper,
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(role)
|
||||
@@ -216,7 +216,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
|
||||
// 创建测试角色
|
||||
role := &model.Role{
|
||||
RoleName: "重复分配测试",
|
||||
RoleType: constants.RoleTypeSuper,
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(role)
|
||||
@@ -238,7 +238,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
|
||||
t.Run("账号不存在时分配角色失败", func(t *testing.T) {
|
||||
role := &model.Role{
|
||||
RoleName: "账号不存在测试",
|
||||
RoleType: constants.RoleTypeSuper,
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(role)
|
||||
@@ -322,7 +322,7 @@ func TestAccountRoleAssociation_SoftDelete(t *testing.T) {
|
||||
|
||||
role := &model.Role{
|
||||
RoleName: "恢复角色测试",
|
||||
RoleType: constants.RoleTypeSuper,
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(role)
|
||||
|
||||
@@ -187,7 +187,6 @@ func TestAccountAPI_Create(t *testing.T) {
|
||||
Phone: "13800000001",
|
||||
Password: "Password123",
|
||||
UserType: constants.UserTypePlatform,
|
||||
ParentID: &rootAccount.ID,
|
||||
}
|
||||
|
||||
jsonBody, _ := json.Marshal(reqBody)
|
||||
@@ -216,7 +215,6 @@ func TestAccountAPI_Create(t *testing.T) {
|
||||
Phone: "13800000002",
|
||||
Password: "hashedpassword",
|
||||
UserType: constants.UserTypePlatform,
|
||||
ParentID: &rootAccount.ID,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
createTestAccount(t, env.db, existingAccount)
|
||||
@@ -227,7 +225,6 @@ func TestAccountAPI_Create(t *testing.T) {
|
||||
Phone: "13800000003",
|
||||
Password: "Password123",
|
||||
UserType: constants.UserTypePlatform,
|
||||
ParentID: &rootAccount.ID,
|
||||
}
|
||||
|
||||
jsonBody, _ := json.Marshal(reqBody)
|
||||
@@ -476,7 +473,7 @@ func TestAccountAPI_AssignRoles(t *testing.T) {
|
||||
// 创建测试角色
|
||||
testRole := &model.Role{
|
||||
RoleName: "测试角色",
|
||||
RoleType: constants.RoleTypeSuper,
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
env.db.Create(testRole)
|
||||
@@ -527,7 +524,7 @@ func TestAccountAPI_GetRoles(t *testing.T) {
|
||||
// 创建并分配角色
|
||||
testRole := &model.Role{
|
||||
RoleName: "获取角色测试",
|
||||
RoleType: constants.RoleTypeSuper,
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
env.db.Create(testRole)
|
||||
@@ -580,7 +577,7 @@ func TestAccountAPI_RemoveRole(t *testing.T) {
|
||||
// 创建并分配角色
|
||||
testRole := &model.Role{
|
||||
RoleName: "移除角色测试",
|
||||
RoleType: constants.RoleTypeSuper,
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
env.db.Create(testRole)
|
||||
|
||||
@@ -230,7 +230,7 @@ func TestAPIRegression_RouteModularization(t *testing.T) {
|
||||
// 创建测试数据
|
||||
role := &model.Role{
|
||||
RoleName: "回归测试角色",
|
||||
RoleType: constants.RoleTypeSuper,
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
env.db.Create(role)
|
||||
|
||||
130
tests/integration/permission_middleware_test.go
Normal file
130
tests/integration/permission_middleware_test.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// MockPermissionChecker 模拟权限检查器
|
||||
type MockPermissionChecker struct {
|
||||
permissions map[uint]map[string]bool // userID -> permCode -> hasPermission
|
||||
}
|
||||
|
||||
func NewMockPermissionChecker() *MockPermissionChecker {
|
||||
return &MockPermissionChecker{
|
||||
permissions: make(map[uint]map[string]bool),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockPermissionChecker) GrantPermission(userID uint, permCode string) {
|
||||
if m.permissions[userID] == nil {
|
||||
m.permissions[userID] = make(map[string]bool)
|
||||
}
|
||||
m.permissions[userID][permCode] = true
|
||||
}
|
||||
|
||||
func (m *MockPermissionChecker) CheckPermission(ctx context.Context, userID uint, permCode string, platform string) (bool, error) {
|
||||
if m.permissions[userID] == nil {
|
||||
return false, nil
|
||||
}
|
||||
return m.permissions[userID][permCode], nil
|
||||
}
|
||||
|
||||
// TestPermissionMiddleware_RequirePermission 测试权限校验中间件(单个权限)
|
||||
// TODO: 完整实现需要启动 Fiber 应用并模拟 HTTP 请求
|
||||
func TestPermissionMiddleware_RequirePermission(t *testing.T) {
|
||||
t.Skip("TODO: 需要完整的 Fiber 集成测试环境")
|
||||
|
||||
// 占位测试:验证 PermissionChecker 接口可以被 mock
|
||||
checker := NewMockPermissionChecker()
|
||||
checker.GrantPermission(1, "user:read")
|
||||
|
||||
ctx := context.Background()
|
||||
hasPermission, err := checker.CheckPermission(ctx, 1, "user:read", constants.PlatformAll)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, hasPermission)
|
||||
|
||||
hasPermission, err = checker.CheckPermission(ctx, 1, "user:write", constants.PlatformAll)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, hasPermission)
|
||||
}
|
||||
|
||||
// TestPermissionMiddleware_RequireAnyPermission 测试权限校验中间件(多个权限任一)
|
||||
func TestPermissionMiddleware_RequireAnyPermission(t *testing.T) {
|
||||
t.Skip("TODO: 需要完整的 Fiber 集成测试环境")
|
||||
}
|
||||
|
||||
// TestPermissionMiddleware_RequireAllPermissions 测试权限校验中间件(多个权限全部)
|
||||
func TestPermissionMiddleware_RequireAllPermissions(t *testing.T) {
|
||||
t.Skip("TODO: 需要完整的 Fiber 集成测试环境")
|
||||
}
|
||||
|
||||
// TestPermissionMiddleware_SkipSuperAdmin 测试超级管理员跳过权限检查
|
||||
func TestPermissionMiddleware_SkipSuperAdmin(t *testing.T) {
|
||||
t.Skip("TODO: 需要完整的 Fiber 集成测试环境")
|
||||
}
|
||||
|
||||
// TestPermissionMiddleware_PlatformFiltering 测试按 platform 过滤权限
|
||||
func TestPermissionMiddleware_PlatformFiltering(t *testing.T) {
|
||||
t.Skip("TODO: 需要完整的 Fiber 集成测试环境")
|
||||
|
||||
// 测试场景:
|
||||
// 1. Web 端请求需要 Web 权限
|
||||
// 2. H5 端请求需要 H5 权限
|
||||
// 3. all 权限在所有端口都有效
|
||||
}
|
||||
|
||||
// TestPermissionMiddleware_Unauthorized 测试未认证用户访问受保护路由
|
||||
func TestPermissionMiddleware_Unauthorized(t *testing.T) {
|
||||
t.Skip("TODO: 需要完整的 Fiber 集成测试环境")
|
||||
}
|
||||
|
||||
// 集成测试实现指南:
|
||||
//
|
||||
// 完整的集成测试应该:
|
||||
// 1. 启动 Fiber 应用
|
||||
// 2. 注册受权限保护的路由:
|
||||
// - 使用 middleware.RequirePermission("user:read", config)
|
||||
// - 使用 middleware.RequireAnyPermission([]string{"user:read", "user:write"}, config)
|
||||
// - 使用 middleware.RequireAllPermissions([]string{"user:read", "user:write"}, config)
|
||||
// 3. 模拟不同用户的 HTTP 请求
|
||||
// 4. 验证权限检查结果(200 OK 或 403 Forbidden)
|
||||
//
|
||||
// 示例代码结构:
|
||||
//
|
||||
// func TestPermissionMiddleware_Integration(t *testing.T) {
|
||||
// // 1. 初始化数据库和 Redis
|
||||
// db, redisClient := testutils.SetupTestDB(t)
|
||||
// defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
//
|
||||
// // 2. 创建测试数据(用户、角色、权限)
|
||||
// // ...
|
||||
//
|
||||
// // 3. 初始化 Service 和 Middleware
|
||||
// permissionService := permission.New(permissionStore)
|
||||
// config := middleware.PermissionConfig{
|
||||
// PermissionChecker: permissionService,
|
||||
// Platform: constants.PlatformWeb,
|
||||
// SkipSuperAdmin: true,
|
||||
// }
|
||||
//
|
||||
// // 4. 创建 Fiber 应用并注册路由
|
||||
// app := fiber.New()
|
||||
// app.Get("/protected",
|
||||
// middleware.RequirePermission("user:read", config),
|
||||
// func(c *fiber.Ctx) error {
|
||||
// return c.JSON(fiber.Map{"message": "success"})
|
||||
// },
|
||||
// )
|
||||
//
|
||||
// // 5. 模拟请求并验证响应
|
||||
// req := httptest.NewRequest("GET", "/protected", nil)
|
||||
// // 设置认证信息...
|
||||
// resp, err := app.Test(req)
|
||||
// require.NoError(t, err)
|
||||
// assert.Equal(t, fiber.StatusOK, resp.StatusCode)
|
||||
// }
|
||||
@@ -70,7 +70,7 @@ func TestRolePermissionAssociation_AssignPermissions(t *testing.T) {
|
||||
// 创建测试角色
|
||||
role := &model.Role{
|
||||
RoleName: "单权限测试角色",
|
||||
RoleType: constants.RoleTypeSuper,
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(role)
|
||||
@@ -96,7 +96,7 @@ func TestRolePermissionAssociation_AssignPermissions(t *testing.T) {
|
||||
// 创建测试角色
|
||||
role := &model.Role{
|
||||
RoleName: "多权限测试角色",
|
||||
RoleType: constants.RoleTypeSuper,
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(role)
|
||||
@@ -124,7 +124,7 @@ func TestRolePermissionAssociation_AssignPermissions(t *testing.T) {
|
||||
// 创建测试角色
|
||||
role := &model.Role{
|
||||
RoleName: "获取权限列表测试角色",
|
||||
RoleType: constants.RoleTypeSuper,
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(role)
|
||||
@@ -152,7 +152,7 @@ func TestRolePermissionAssociation_AssignPermissions(t *testing.T) {
|
||||
// 创建测试角色
|
||||
role := &model.Role{
|
||||
RoleName: "移除权限测试角色",
|
||||
RoleType: constants.RoleTypeSuper,
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(role)
|
||||
@@ -184,7 +184,7 @@ func TestRolePermissionAssociation_AssignPermissions(t *testing.T) {
|
||||
// 创建测试角色
|
||||
role := &model.Role{
|
||||
RoleName: "重复权限测试角色",
|
||||
RoleType: constants.RoleTypeSuper,
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(role)
|
||||
@@ -228,7 +228,7 @@ func TestRolePermissionAssociation_AssignPermissions(t *testing.T) {
|
||||
t.Run("权限不存在时分配失败", func(t *testing.T) {
|
||||
role := &model.Role{
|
||||
RoleName: "权限不存在测试角色",
|
||||
RoleType: constants.RoleTypeSuper,
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(role)
|
||||
@@ -276,7 +276,7 @@ func TestRolePermissionAssociation_SoftDelete(t *testing.T) {
|
||||
// 创建测试数据
|
||||
role := &model.Role{
|
||||
RoleName: "恢复权限测试角色",
|
||||
RoleType: constants.RoleTypeSuper,
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(role)
|
||||
@@ -312,7 +312,7 @@ func TestRolePermissionAssociation_SoftDelete(t *testing.T) {
|
||||
// 创建测试角色
|
||||
role := &model.Role{
|
||||
RoleName: "批量权限测试角色",
|
||||
RoleType: constants.RoleTypeSuper,
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(role)
|
||||
@@ -383,7 +383,7 @@ func TestRolePermissionAssociation_Cascade(t *testing.T) {
|
||||
// 创建角色和权限
|
||||
role := &model.Role{
|
||||
RoleName: "级联测试角色",
|
||||
RoleType: constants.RoleTypeSuper,
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(role)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user