This repository has been archived on 2026-06-21. You can view files and clone it, but cannot push or open issues or pull requests.
Files
MetaLab/internal/middleware/admin.go
Victor_Jay 39d13993ba fix: 设计原则审查修复 — DIP/ISP, LoD, DRY, OCP, URL, 301缓存
- P0 DIP+ISP: 全链路注入接口,消除零接口紧耦合
- P0 URL: auth 301→302,修复登出后浏览器缓存陷阱
- P1 DRY: JWT 认证逻辑收敛至 TokenService+中间件
- P2 DRY: 前后端角色/状态映射统一为 model 常量
- P2 LoD: 新增 SettingsController,router 不再跨层调 repo
- P2 URL: settings ?tab= → /settings/:tab 伪静态
- P3 OCP: 角色权限 map 化,告别硬编码 switch
2026-05-26 21:12:19 +08:00

41 lines
987 B
Go

package middleware
import (
"log"
"net/http"
"metazone.cc/metalab/internal/model"
"github.com/gin-gonic/gin"
)
// RequireMinRole 角色权限检查(用于 API 路由)
// 不足时返回 JSON 403
func RequireMinRole(minRole string) gin.HandlerFunc {
return func(c *gin.Context) {
role, exists := c.Get("role")
if !exists || !model.HasMinRole(role.(string), minRole) {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
"success": false, "message": "权限不足",
})
return
}
c.Next()
}
}
// RequirePageRole 角色权限检查(用于 SSR 页面路由)
// 不足时 302 跳转首页
func RequirePageRole(minRole string) gin.HandlerFunc {
return func(c *gin.Context) {
role, exists := c.Get("role")
if !exists || !model.HasMinRole(role.(string), minRole) {
log.Printf("[RequirePageRole] DENY: role=%v need=%s path=%s → 302", role, minRole, c.Request.URL.Path)
c.Redirect(http.StatusFound, "/")
c.Abort()
return
}
c.Next()
}
}