You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
531 B
26 lines
531 B
package middleware |
|
|
|
import ( |
|
"net/http" |
|
|
|
"github.com/gin-gonic/gin" |
|
) |
|
|
|
func PermissionMiddleware(permission string) gin.HandlerFunc { |
|
return func(c *gin.Context) { |
|
userID, exists := c.Get("user_id") |
|
if !exists { |
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "未认证"}) |
|
return |
|
} |
|
|
|
// 检查用户是否有该权限 |
|
hasPerm := checkPermission(userID.(uint), permission) |
|
if !hasPerm { |
|
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "没有权限"}) |
|
return |
|
} |
|
|
|
c.Next() |
|
} |
|
}
|
|
|