- 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
41 lines
987 B
Go
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()
|
|
}
|
|
}
|