39 lines
657 B
Go
39 lines
657 B
Go
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()
|
|
}
|
|
|
|
}
|