1.0
This commit is contained in:
@@ -0,0 +1,213 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"awesomeProject/internal/common"
|
||||
"awesomeProject/internal/models"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// AdminRoleMiddleware 管理员权限中间件
|
||||
func AdminRoleMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 获取token中的用户信息
|
||||
tokenMap, exists := c.Get("tokenMap")
|
||||
if !exists {
|
||||
c.AbortWithStatusJSON(http.StatusOK, common.Error(401, "未授权访问"))
|
||||
return
|
||||
}
|
||||
|
||||
claims := tokenMap.(map[string]interface{})
|
||||
userIDFloat, ok := claims["user_id"].(float64)
|
||||
if !ok {
|
||||
c.AbortWithStatusJSON(http.StatusOK, common.Error(401, "用户信息无效"))
|
||||
return
|
||||
}
|
||||
|
||||
userID := uint(userIDFloat)
|
||||
|
||||
// 查询用户信息
|
||||
var user models.User
|
||||
if err := common.GetDB().First(&user, userID).Error; err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusOK, common.Error(401, "用户不存在"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查用户角色:0管理员,1代理商,2普通用户
|
||||
if user.SystemRole != 0 {
|
||||
c.AbortWithStatusJSON(http.StatusOK, common.Error(403, "权限不足,仅管理员可访问"))
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("current_user", user)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// AgentRoleMiddleware 代理商权限中间件
|
||||
func AgentRoleMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 获取token中的用户信息
|
||||
tokenMap, exists := c.Get("tokenMap")
|
||||
if !exists {
|
||||
c.AbortWithStatusJSON(http.StatusOK, common.Error(401, "未授权访问"))
|
||||
return
|
||||
}
|
||||
|
||||
claims := tokenMap.(map[string]interface{})
|
||||
userIDFloat, ok := claims["user_id"].(float64)
|
||||
if !ok {
|
||||
c.AbortWithStatusJSON(http.StatusOK, common.Error(401, "用户信息无效"))
|
||||
return
|
||||
}
|
||||
|
||||
userID := uint(userIDFloat)
|
||||
|
||||
// 查询用户信息
|
||||
var user models.User
|
||||
if err := common.GetDB().First(&user, userID).Error; err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusOK, common.Error(401, "用户不存在"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查用户角色:0管理员,1代理商
|
||||
if user.SystemRole != 0 && user.SystemRole != 1 {
|
||||
c.AbortWithStatusJSON(http.StatusOK, common.Error(403, "权限不足,仅管理员和代理商可访问"))
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("current_user", user)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
func UserRoleMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
|
||||
// 获取token中的用户信息
|
||||
tokenMap, exists := c.Get("tokenMap")
|
||||
if !exists {
|
||||
c.AbortWithStatusJSON(http.StatusOK, common.Error(401, "未授权访问"))
|
||||
return
|
||||
}
|
||||
|
||||
claims := tokenMap.(map[string]interface{})
|
||||
userIDFloat, ok := claims["user_id"].(float64)
|
||||
if !ok {
|
||||
c.AbortWithStatusJSON(http.StatusOK, common.Error(401, "用户信息无效"))
|
||||
return
|
||||
}
|
||||
|
||||
userID := uint(userIDFloat)
|
||||
|
||||
// 查询用户信息
|
||||
var user models.User
|
||||
if err := common.GetDB().First(&user, userID).Error; err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusOK, common.Error(401, "用户不存在"))
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("current_user", user)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// CheckUserPermission 检查用户权限(代理商只能查看自己邀请的用户)
|
||||
func CheckUserPermission() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
currentUser, exists := c.Get("current_user")
|
||||
if !exists {
|
||||
c.AbortWithStatusJSON(http.StatusOK, common.Error(401, "用户信息获取失败"))
|
||||
return
|
||||
}
|
||||
|
||||
user := currentUser.(models.User)
|
||||
|
||||
// 如果是管理员,拥有所有权限
|
||||
if user.SystemRole == 0 {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
// 如果是代理商,需要检查权限
|
||||
if user.SystemRole == 1 {
|
||||
// 获取请求路径
|
||||
//path := c.Request.URL.Path
|
||||
method := c.Request.Method
|
||||
|
||||
//// 代理商权限限制
|
||||
//allowedPaths := []string{
|
||||
// "/back/admin/users", // 查看用户列表
|
||||
// "/back/admin/purchase-orders", // 查看买单
|
||||
// "/back/admin/sales-orders", // 查看卖单
|
||||
// "/back/admin/score-records", // 查看积分记录
|
||||
//}
|
||||
//
|
||||
//// 检查是否为允许的路径
|
||||
//pathAllowed := false
|
||||
//for _, allowedPath := range allowedPaths {
|
||||
// if strings.HasPrefix(path, allowedPath) {
|
||||
// pathAllowed = true
|
||||
// break
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//if !pathAllowed {
|
||||
// c.AbortWithStatusJSON(http.StatusOK, common.Error(403, "代理商权限不足"))
|
||||
// return
|
||||
//}
|
||||
|
||||
// 对于GET请求,添加查询过滤条件
|
||||
if method == "GET" {
|
||||
// 设置代理商身份码,用于数据过滤
|
||||
c.Set("agent_identity_code", user.IdentityCode)
|
||||
} else {
|
||||
// 非GET请求,代理商不允许
|
||||
c.AbortWithStatusJSON(http.StatusOK, common.Error(403, "代理商只能查看数据,不能进行修改操作"))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// GetUserIDFromParam 从URL参数中获取用户ID并检查权限
|
||||
func GetUserIDFromParam(c *gin.Context, paramName string) (uint, error) {
|
||||
currentUser, exists := c.Get("current_user")
|
||||
if !exists {
|
||||
return 0, fmt.Errorf("用户信息获取失败")
|
||||
}
|
||||
|
||||
user := currentUser.(models.User)
|
||||
userIDStr := c.Param(paramName)
|
||||
userID, err := strconv.ParseUint(userIDStr, 10, 32)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("用户ID无效")
|
||||
}
|
||||
|
||||
targetUserID := uint(userID)
|
||||
|
||||
// 如果是管理员,可以查看所有用户
|
||||
if user.SystemRole == 0 {
|
||||
return targetUserID, nil
|
||||
}
|
||||
|
||||
// 如果是代理商,只能查看自己邀请的用户
|
||||
if user.SystemRole == 1 {
|
||||
var targetUser models.User
|
||||
if err := common.GetDB().First(&targetUser, targetUserID).Error; err != nil {
|
||||
return 0, fmt.Errorf("目标用户不存在")
|
||||
}
|
||||
|
||||
// 检查是否为代理商邀请的用户
|
||||
if targetUser.ReferrerIdentityCode != user.IdentityCode {
|
||||
return 0, fmt.Errorf("权限不足,只能查看您邀请的用户")
|
||||
}
|
||||
|
||||
return targetUserID, nil
|
||||
}
|
||||
|
||||
return 0, fmt.Errorf("权限不足")
|
||||
}
|
||||
Reference in New Issue
Block a user