mc-client-updater-server/internal/api/v1/middleware/admin.go

40 lines
817 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package middleware
import (
"github.com/gin-gonic/gin"
"mc-client-updater-server/internal/service"
"mc-client-updater-server/pkg/result"
"strings"
)
func AdminRequired(c *gin.Context) {
res := result.NewResult(c)
authorization := c.GetHeader("Authorization")
if authorization == "" {
res.UnLogin()
return
}
split := strings.Split(authorization, " ")
if len(split) <= 1 || (len(split) >= 2 && split[0] != "Bearer") {
res.BadRequest()
return
}
tokenSrv := service.NewTokenService(c)
token := split[1]
tokenRow, ok := tokenSrv.VerifyToken(token)
// 若!ok则返回值已被service处理无需再次返回
if !ok {
return
}
userSrv := service.NewUserService(c)
hasRole := userSrv.JudgeRoleByToken("ROLE_admin", tokenRow)
if !hasRole {
res.NoPermission()
return
}
c.Next()
}