default
This commit is contained in:
38
api/admin/auth.go
Normal file
38
api/admin/auth.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
."main.go/type"
|
||||
"main.go/api"
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
// AuthRequired 用于管理员权限控制的中间件
|
||||
func AuthRequired()gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
session, err := api.Store.Get(c.Request, "SNCTFSESSID")
|
||||
if err != nil {
|
||||
c.JSON(200, gin.H{"code": 400, "msg": "Get SNCTFSESSID error"})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
user, ok := session.Values["user"].(User)
|
||||
if !ok {
|
||||
c.JSON(200, gin.H{"code": 400, "msg": "No session"})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
if user.Role != 1 {
|
||||
c.JSON(200, gin.H{"code": 400, "msg": "Unauthorized access!"})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user