ferry/middleware/permission.go

49 lines
1.0 KiB
Go
Raw Normal View History

2020-07-13 20:33:20 +08:00
package middleware
import (
"ferry/global/orm"
"ferry/models/system"
2020-07-13 20:33:20 +08:00
mycasbin "ferry/pkg/casbin"
"ferry/pkg/jwtauth"
_ "ferry/pkg/jwtauth"
2020-08-15 23:34:37 +08:00
"ferry/pkg/logger"
2020-07-13 20:33:20 +08:00
"ferry/tools"
"fmt"
2020-07-13 20:33:20 +08:00
"net/http"
"github.com/gin-gonic/gin"
)
//权限检查中间件
func AuthCheckRole() gin.HandlerFunc {
return func(c *gin.Context) {
var menuValue system.Menu
2020-07-13 20:33:20 +08:00
data, _ := c.Get("JWT_PAYLOAD")
v := data.(jwtauth.MapClaims)
e, err := mycasbin.Casbin()
tools.HasError(err, "", 500)
//检查权限
res, err := e.Enforce(v["rolekey"], c.Request.URL.Path, c.Request.Method)
2020-08-15 23:34:37 +08:00
logger.Info(v["rolekey"], c.Request.URL.Path, c.Request.Method)
2020-07-13 20:33:20 +08:00
tools.HasError(err, "", 500)
err = orm.Eloquent.Model(&menuValue).
Where("path = ? and action = ?", c.Request.URL.Path, c.Request.Method).
Find(&menuValue).Error
tools.HasError(err, "", 500)
2020-07-13 20:33:20 +08:00
if res {
c.Next()
} else {
c.JSON(http.StatusOK, gin.H{
"code": 403,
"msg": fmt.Sprintf("对不起,您没有 <%v> 访问权限,请联系管理员", menuValue.Title),
2020-07-13 20:33:20 +08:00
})
c.Abort()
return
}
}
}