实现角色权限体系重构
本次提交完成了角色权限体系的重构,主要包括: 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:
@@ -60,9 +60,8 @@ const (
|
||||
|
||||
// RBAC 角色类型常量
|
||||
const (
|
||||
RoleTypeSuper = 1 // 超级角色
|
||||
RoleTypeAgent = 2 // 代理角色
|
||||
RoleTypeEnterprise = 3 // 企业角色
|
||||
RoleTypePlatform = 1 // 平台角色(适用于平台用户)
|
||||
RoleTypeCustomer = 2 // 客户角色(适用于代理/企业账号)
|
||||
)
|
||||
|
||||
// RBAC 权限类型常量
|
||||
@@ -71,6 +70,13 @@ const (
|
||||
PermissionTypeButton = 2 // 按钮权限
|
||||
)
|
||||
|
||||
// RBAC 权限端口常量
|
||||
const (
|
||||
PlatformAll = "all" // 全部端口(Web + H5)
|
||||
PlatformWeb = "web" // Web 后台
|
||||
PlatformH5 = "h5" // H5 端
|
||||
)
|
||||
|
||||
// RBAC 状态常量
|
||||
const (
|
||||
StatusDisabled = 0 // 禁用
|
||||
|
||||
37
pkg/constants/rbac.go
Normal file
37
pkg/constants/rbac.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package constants
|
||||
|
||||
// IsRoleTypeMatchUserType 检查角色类型是否与用户类型匹配
|
||||
// 返回 true 表示匹配,false 表示不匹配
|
||||
func IsRoleTypeMatchUserType(roleType, userType int) bool {
|
||||
switch userType {
|
||||
case UserTypeSuperAdmin:
|
||||
// 超级管理员不需要角色
|
||||
return false
|
||||
case UserTypePlatform:
|
||||
// 平台用户只能分配平台角色
|
||||
return roleType == RoleTypePlatform
|
||||
case UserTypeAgent, UserTypeEnterprise:
|
||||
// 代理/企业账号只能分配客户角色
|
||||
return roleType == RoleTypeCustomer
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// GetMaxRolesForUserType 获取用户类型允许的最大角色数量
|
||||
// 返回 0 表示不允许分配角色,-1 表示无限制
|
||||
func GetMaxRolesForUserType(userType int) int {
|
||||
switch userType {
|
||||
case UserTypeSuperAdmin:
|
||||
// 超级管理员不需要角色
|
||||
return 0
|
||||
case UserTypePlatform:
|
||||
// 平台用户可分配多个角色,无限制
|
||||
return -1
|
||||
case UserTypeAgent, UserTypeEnterprise:
|
||||
// 代理/企业账号只能分配一个角色
|
||||
return 1
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
184
pkg/middleware/permission.go
Normal file
184
pkg/middleware/permission.go
Normal file
@@ -0,0 +1,184 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
// PermissionChecker 权限检查器接口
|
||||
// 用于查询用户的权限列表
|
||||
type PermissionChecker interface {
|
||||
// CheckPermission 检查用户是否拥有指定权限
|
||||
// userID: 用户ID
|
||||
// permCode: 权限编码
|
||||
// platform: 端口类型 (all/web/h5)
|
||||
CheckPermission(ctx context.Context, userID uint, permCode string, platform string) (bool, error)
|
||||
}
|
||||
|
||||
// PermissionConfig 权限校验中间件配置
|
||||
type PermissionConfig struct {
|
||||
// PermissionChecker 权限检查器
|
||||
PermissionChecker PermissionChecker
|
||||
|
||||
// Platform 端口类型 (all/web/h5)
|
||||
// 如果为空,默认为 "all"
|
||||
Platform string
|
||||
|
||||
// SkipSuperAdmin 是否跳过超级管理员的权限检查
|
||||
// 默认为 true
|
||||
SkipSuperAdmin bool
|
||||
}
|
||||
|
||||
// RequirePermission 权限校验中间件
|
||||
// 检查当前用户是否拥有指定权限
|
||||
// 如果没有权限,返回 403 错误
|
||||
func RequirePermission(permCode string, config PermissionConfig) fiber.Handler {
|
||||
return func(c *fiber.Ctx) error {
|
||||
// 获取用户信息
|
||||
userID := GetUserIDFromContext(c.UserContext())
|
||||
if userID == 0 {
|
||||
return errors.New(errors.CodeUnauthorized, "未认证的请求")
|
||||
}
|
||||
|
||||
// 如果配置为跳过超级管理员,且当前用户是超级管理员,则跳过权限检查
|
||||
if config.SkipSuperAdmin {
|
||||
userType := GetUserTypeFromContext(c.UserContext())
|
||||
if userType == constants.UserTypeSuperAdmin {
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// 确定端口类型
|
||||
platform := config.Platform
|
||||
if platform == "" {
|
||||
platform = constants.PlatformAll
|
||||
}
|
||||
|
||||
// 检查权限检查器是否已配置
|
||||
if config.PermissionChecker == nil {
|
||||
return errors.New(errors.CodeInternalError, "权限检查器未配置")
|
||||
}
|
||||
|
||||
// 检查用户是否拥有该权限
|
||||
hasPermission, err := config.PermissionChecker.CheckPermission(c.UserContext(), userID, permCode, platform)
|
||||
if err != nil {
|
||||
// 如果是 AppError,直接返回
|
||||
if appErr, ok := err.(*errors.AppError); ok {
|
||||
return appErr
|
||||
}
|
||||
// 否则包装为 AppError
|
||||
return errors.Wrap(errors.CodeInternalError, "权限检查失败", err)
|
||||
}
|
||||
|
||||
if !hasPermission {
|
||||
return errors.New(errors.CodeForbidden, "无权限访问该资源")
|
||||
}
|
||||
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// RequireAnyPermission 检查用户是否拥有指定权限列表中的任意一个权限
|
||||
// 如果没有任何权限,返回 403 错误
|
||||
func RequireAnyPermission(permCodes []string, config PermissionConfig) fiber.Handler {
|
||||
return func(c *fiber.Ctx) error {
|
||||
// 获取用户信息
|
||||
userID := GetUserIDFromContext(c.UserContext())
|
||||
if userID == 0 {
|
||||
return errors.New(errors.CodeUnauthorized, "未认证的请求")
|
||||
}
|
||||
|
||||
// 如果配置为跳过超级管理员,且当前用户是超级管理员,则跳过权限检查
|
||||
if config.SkipSuperAdmin {
|
||||
userType := GetUserTypeFromContext(c.UserContext())
|
||||
if userType == constants.UserTypeSuperAdmin {
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// 确定端口类型
|
||||
platform := config.Platform
|
||||
if platform == "" {
|
||||
platform = constants.PlatformAll
|
||||
}
|
||||
|
||||
// 检查权限检查器是否已配置
|
||||
if config.PermissionChecker == nil {
|
||||
return errors.New(errors.CodeInternalError, "权限检查器未配置")
|
||||
}
|
||||
|
||||
// 检查用户是否拥有任意一个权限
|
||||
for _, permCode := range permCodes {
|
||||
hasPermission, err := config.PermissionChecker.CheckPermission(c.UserContext(), userID, permCode, platform)
|
||||
if err != nil {
|
||||
// 如果是 AppError,直接返回
|
||||
if appErr, ok := err.(*errors.AppError); ok {
|
||||
return appErr
|
||||
}
|
||||
// 否则包装为 AppError
|
||||
return errors.Wrap(errors.CodeInternalError, "权限检查失败", err)
|
||||
}
|
||||
|
||||
// 如果拥有任意一个权限,则放行
|
||||
if hasPermission {
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
return errors.New(errors.CodeForbidden, "无权限访问该资源")
|
||||
}
|
||||
}
|
||||
|
||||
// RequireAllPermissions 检查用户是否拥有指定权限列表中的所有权限
|
||||
// 如果缺少任意一个权限,返回 403 错误
|
||||
func RequireAllPermissions(permCodes []string, config PermissionConfig) fiber.Handler {
|
||||
return func(c *fiber.Ctx) error {
|
||||
// 获取用户信息
|
||||
userID := GetUserIDFromContext(c.UserContext())
|
||||
if userID == 0 {
|
||||
return errors.New(errors.CodeUnauthorized, "未认证的请求")
|
||||
}
|
||||
|
||||
// 如果配置为跳过超级管理员,且当前用户是超级管理员,则跳过权限检查
|
||||
if config.SkipSuperAdmin {
|
||||
userType := GetUserTypeFromContext(c.UserContext())
|
||||
if userType == constants.UserTypeSuperAdmin {
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// 确定端口类型
|
||||
platform := config.Platform
|
||||
if platform == "" {
|
||||
platform = constants.PlatformAll
|
||||
}
|
||||
|
||||
// 检查权限检查器是否已配置
|
||||
if config.PermissionChecker == nil {
|
||||
return errors.New(errors.CodeInternalError, "权限检查器未配置")
|
||||
}
|
||||
|
||||
// 检查用户是否拥有所有权限
|
||||
for _, permCode := range permCodes {
|
||||
hasPermission, err := config.PermissionChecker.CheckPermission(c.UserContext(), userID, permCode, platform)
|
||||
if err != nil {
|
||||
// 如果是 AppError,直接返回
|
||||
if appErr, ok := err.(*errors.AppError); ok {
|
||||
return appErr
|
||||
}
|
||||
// 否则包装为 AppError
|
||||
return errors.Wrap(errors.CodeInternalError, "权限检查失败", err)
|
||||
}
|
||||
|
||||
// 如果缺少任意一个权限,则拒绝访问
|
||||
if !hasPermission {
|
||||
return errors.New(errors.CodeForbidden, "无权限访问该资源")
|
||||
}
|
||||
}
|
||||
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user