161 lines
4.0 KiB
Go
161 lines
4.0 KiB
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"hfb_sys/backend/internal/platformscope"
|
|
"hfb_sys/backend/internal/tenancy"
|
|
"hfb_sys/backend/pkg/response"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/redis/go-redis/v9"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// RequirePermission 检查当前管理员是否拥有指定权限。
|
|
// 超级管理员(拥有 super_admin 角色的管理员)自动放行。
|
|
func RequirePermission(permCode string, rdb *redis.Client) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
if checkPermission(c, permCode, rdb) {
|
|
c.Next()
|
|
}
|
|
}
|
|
}
|
|
|
|
// RequireAnyPermission 检查当前管理员是否拥有任意一个指定的权限。
|
|
// 超级管理员(拥有 super_admin 角色或 * 权限)自动放行。
|
|
func RequireAnyPermission(rdb *redis.Client, permCodes ...string) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
if checkAnyPermission(c, rdb, permCodes...) {
|
|
c.Next()
|
|
}
|
|
}
|
|
}
|
|
|
|
func RequirePermissionIf(permCode string, rdb *redis.Client, predicate func(*gin.Context) bool) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
if predicate == nil || !predicate(c) {
|
|
c.Next()
|
|
return
|
|
}
|
|
if checkPermission(c, permCode, rdb) {
|
|
c.Next()
|
|
}
|
|
}
|
|
}
|
|
|
|
// RequirePlatformSuperAdmin 仅放行平台超级管理员,用于不属于单一经营站点的全局财务数据。
|
|
func RequirePlatformSuperAdmin(db *gorm.DB) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
value, ok := c.Get(ContextAdminID)
|
|
if !ok {
|
|
response.Unauthorized(c, "缺少管理员上下文")
|
|
c.Abort()
|
|
return
|
|
}
|
|
adminID, ok := value.(uint64)
|
|
if !ok || adminID == 0 {
|
|
response.Unauthorized(c, "管理员上下文无效")
|
|
c.Abort()
|
|
return
|
|
}
|
|
if db == nil {
|
|
response.ServiceUnavailable(c, "权限校验服务暂时不可用")
|
|
c.Abort()
|
|
return
|
|
}
|
|
var count int64
|
|
err := db.WithContext(c.Request.Context()).
|
|
Table("admin_user_roles AS aur").
|
|
Joins("JOIN roles AS r ON r.id = aur.role_id").
|
|
Where("aur.admin_user_id = ? AND r.code = ?", adminID, "super_admin").
|
|
Count(&count).Error
|
|
if err != nil {
|
|
response.ServiceUnavailable(c, "权限校验服务暂时不可用")
|
|
c.Abort()
|
|
return
|
|
}
|
|
if count == 0 {
|
|
response.Error(c, http.StatusForbidden, "platform_super_admin_required", "仅平台超级管理员可访问")
|
|
c.Abort()
|
|
return
|
|
}
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
func checkAnyPermission(c *gin.Context, rdb *redis.Client, permCodes ...string) bool {
|
|
value, ok := c.Get(ContextAdminID)
|
|
if !ok {
|
|
response.Unauthorized(c, "缺少管理员上下文")
|
|
c.Abort()
|
|
return false
|
|
}
|
|
adminID, ok := value.(uint64)
|
|
if !ok {
|
|
response.Unauthorized(c, "管理员上下文无效")
|
|
c.Abort()
|
|
return false
|
|
}
|
|
|
|
codes, err := getPermCodes(c, rdb, adminID)
|
|
if err != nil {
|
|
response.Error(c, http.StatusInternalServerError, "perm_check_failed", "权限校验服务暂时不可用")
|
|
c.Abort()
|
|
return false
|
|
}
|
|
|
|
for _, code := range codes {
|
|
if code == "*" {
|
|
return true
|
|
}
|
|
for _, target := range permCodes {
|
|
if code == target {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
|
|
response.Error(c, http.StatusForbidden, "forbidden", "没有操作权限")
|
|
c.Abort()
|
|
return false
|
|
}
|
|
|
|
func checkPermission(c *gin.Context, permCode string, rdb *redis.Client) bool {
|
|
return checkAnyPermission(c, rdb, permCode)
|
|
}
|
|
|
|
func getPermCodes(c *gin.Context, rdb *redis.Client, adminID uint64) ([]string, error) {
|
|
if rdb == nil {
|
|
return nil, errors.New("redis unavailable")
|
|
}
|
|
key := adminPermissionCacheKey(c.Request.Context(), adminID)
|
|
raw, err := rdb.Get(c.Request.Context(), key).Result()
|
|
if errors.Is(err, redis.Nil) {
|
|
return nil, nil // 缓存未命中,视为无权限
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var codes []string
|
|
if err := json.Unmarshal([]byte(raw), &codes); err != nil {
|
|
return nil, err
|
|
}
|
|
return codes, nil
|
|
}
|
|
|
|
func adminPermissionCacheKey(ctx context.Context, adminID uint64) string {
|
|
key := fmt.Sprintf("admin:perms:%d", adminID)
|
|
if platformscope.IsScope(ctx) {
|
|
return key + ":platform"
|
|
}
|
|
if scope := tenancy.ScopeKey(ctx); scope != "" {
|
|
key += ":" + scope
|
|
}
|
|
return key
|
|
}
|