Commit 93c31394 authored by shajiaiming's avatar shajiaiming

客服权限

parent 9a119911
package auth
import (
"github.com/gin-gonic/gin"
"bwallet/pkg/e"
"bwallet/models"
"net/http"
)
func AUTH() gin.HandlerFunc {
return func(c *gin.Context) {
var code int
var data interface{}
code = e.SUCCESS
token := c.Request.Header.Get("Token")
if token == "" {
code = e.INVALID_PARAMS
} else {
_, err := models.CheckToken(token)
if err != nil {
code = e.ERROR_AUTH_CHECK_TOKEN_FAIL
}
}
if code != e.SUCCESS {
c.JSON(http.StatusUnauthorized, gin.H{
"code": code,
"msg": e.GetMsg(code),
"data": data,
})
c.Abort()
return
}
c.Next()
}
}
package auth
import (
"bwallet/pkg/errno"
"bwallet/pkg/handler"
"bwallet/pkg/util"
"github.com/gin-gonic/gin"
"strings"
)
func AUTH(handlerTableName map[string]string) gin.HandlerFunc {
return func(ctx *gin.Context) {
token := ctx.Request.Header.Get("Token")
user, _ := util.ParseToken(token)
if 81 != user.UserInfo.Uid {
ctx.Next()
}
handlerName := strings.Split(ctx.HandlerName(), ".")[1]
if _, ok := handlerTableName[handlerName]; !ok {
ctx.Next()
}
handleAlowed := []string{"GetWallets", "GetWalletCoinRelationCoinRelations", "GetCoins"}
_, handle := util.Contains(handleAlowed, handlerName)
if !handle {
handler.SendResponse(ctx, errno.PermissionDenied, nil)
ctx.Abort()
return
}
ctx.Next()
}
}
package e
type HandleAllowedDesc map[int32][]string
var HandleAllowed = HandleAllowedDesc{
}
...@@ -69,6 +69,8 @@ var ( ...@@ -69,6 +69,8 @@ var (
ErrSn = &Errno{Code: 20004, Message: "Sn error."} ErrSn = &Errno{Code: 20004, Message: "Sn error."}
ErrAk = &Errno{Code: 20005, Message: "Ak error."} ErrAk = &Errno{Code: 20005, Message: "Ak error."}
PermissionDenied = &Errno{Code:403, Message:"Permission Denied"}
// coin errors // coin errors
ErrCoinNotFound = &Errno{Code: 20101, Message: "The coin was not found."} ErrCoinNotFound = &Errno{Code: 20101, Message: "The coin was not found."}
ErrCountCoin = &Errno{Code: 20102, Message: "The coins statistic error."} ErrCountCoin = &Errno{Code: 20102, Message: "The coins statistic error."}
......
package routers package routers
import ( import (
"bwallet/middleware/auth"
"bwallet/middleware/jwt" "bwallet/middleware/jwt"
"bwallet/middleware/log" "bwallet/middleware/log"
"bwallet/pkg/e" "bwallet/pkg/e"
...@@ -52,8 +53,8 @@ func InitRouter() *gin.Engine { ...@@ -52,8 +53,8 @@ func InitRouter() *gin.Engine {
api := r.Group("/api") api := r.Group("/api")
//api.Use(auth.AUTH())
api.Use(jwt.JWT()) api.Use(jwt.JWT())
api.Use(auth.AUTH(e.HandleTableName))
api.POST("/log", backend.AddOperationLog) api.POST("/log", backend.AddOperationLog)
api.GET("/logs", backend.GetOperationLogs) api.GET("/logs", backend.GetOperationLogs)
api.Use(log.LogMiddleware(e.HandleTableName)) api.Use(log.LogMiddleware(e.HandleTableName))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment