优化页面、修复BUG
This commit is contained in:
@@ -2,7 +2,6 @@ package middleware
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -10,6 +9,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/niangaodev/art-code/repositories"
|
||||
"github.com/niangaodev/art-code/utils"
|
||||
)
|
||||
|
||||
// JWT密钥
|
||||
@@ -96,7 +96,7 @@ func AuthMiddleware() gin.HandlerFunc {
|
||||
// 从请求头中获取令牌
|
||||
authHeader := c.GetHeader("Authorization")
|
||||
if authHeader == "" {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization header is required"})
|
||||
utils.Error(c, 401, "登录已过期,请重新登录")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
@@ -104,7 +104,7 @@ func AuthMiddleware() gin.HandlerFunc {
|
||||
// 检查令牌格式
|
||||
parts := strings.SplitN(authHeader, " ", 2)
|
||||
if !(len(parts) == 2 && parts[0] == "Bearer") {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization header format must be Bearer {token}"})
|
||||
utils.Error(c, 401, "登录已过期,请重新登录")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
@@ -112,7 +112,7 @@ func AuthMiddleware() gin.HandlerFunc {
|
||||
// 解析令牌
|
||||
claims, err := ParseToken(parts[1])
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid or expired token"})
|
||||
utils.Error(c, 401, "登录已过期,请重新登录")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
@@ -133,7 +133,7 @@ func RoleMiddleware(roles ...string) gin.HandlerFunc {
|
||||
// 从上下文中获取用户角色
|
||||
role, exists := c.Get("role")
|
||||
if !exists {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
utils.Error(c, 401, "未授权,请重新登录")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
@@ -148,7 +148,7 @@ func RoleMiddleware(roles ...string) gin.HandlerFunc {
|
||||
}
|
||||
|
||||
if !allowed {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Insufficient permissions"})
|
||||
utils.Error(c, 403, "权限不足")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
@@ -166,13 +166,13 @@ func PermissionMiddleware(resource, action string) gin.HandlerFunc {
|
||||
// 尝试从role name获取
|
||||
roleName, exists := c.Get("role")
|
||||
if !exists {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
utils.Error(c, 401, "未授权,请重新登录")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
role, err := repositories.GetRoleByName(roleName.(string))
|
||||
if err != nil || role == nil {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Role not found"})
|
||||
utils.Error(c, 403, "角色不存在")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
@@ -184,7 +184,7 @@ func PermissionMiddleware(resource, action string) gin.HandlerFunc {
|
||||
// 获取该角色的所有权限
|
||||
permissions, err := repositories.GetPermissionsByRoleID(roleID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to check permissions"})
|
||||
utils.Error(c, 500, "权限校验失败")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
@@ -199,7 +199,7 @@ func PermissionMiddleware(resource, action string) gin.HandlerFunc {
|
||||
}
|
||||
|
||||
if !allowed {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Insufficient permissions"})
|
||||
utils.Error(c, 403, "权限不足")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user