1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- package middleware
- import (
- "github.com/gin-contrib/sessions"
- "github.com/gin-gonic/gin"
- "message-pusher/common"
- "net/http"
- )
- func authHelper(c *gin.Context, minRole int) {
- session := sessions.Default(c)
- username := session.Get("username")
- role := session.Get("role")
- id := session.Get("id")
- status := session.Get("status")
- if username == nil {
- c.JSON(http.StatusUnauthorized, gin.H{
- "success": false,
- "message": "无权进行此操作,未登录",
- })
- c.Abort()
- return
- }
- if status.(int) == common.UserStatusDisabled {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": "用户已被封禁",
- })
- c.Abort()
- return
- }
- if role.(int) < minRole {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": "无权进行此操作,权限不足",
- })
- c.Abort()
- return
- }
- c.Set("username", username)
- c.Set("role", role)
- c.Set("id", id)
- c.Next()
- }
- func UserAuth() func(c *gin.Context) {
- return func(c *gin.Context) {
- authHelper(c, common.RoleCommonUser)
- }
- }
- func AdminAuth() func(c *gin.Context) {
- return func(c *gin.Context) {
- authHelper(c, common.RoleAdminUser)
- }
- }
- func RootAuth() func(c *gin.Context) {
- return func(c *gin.Context) {
- authHelper(c, common.RoleRootUser)
- }
- }
|