53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package middleware
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"go-admin/app/admin/models"
|
|
mycasbin "go-admin/pkg/casbin"
|
|
"net/http"
|
|
|
|
"go-admin/pkg/jwtauth"
|
|
"go-admin/tools"
|
|
)
|
|
|
|
// 权限检查中间件
|
|
func AuthCheckRole() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
data, _ := c.Get(jwtauth.JwtPayloadKey)
|
|
v := data.(jwtauth.MapClaims)
|
|
e := mycasbin.Casbin()
|
|
//检查权限
|
|
res, err := e.Enforce(v["rolekey"], c.Request.URL.Path, c.Request.Method)
|
|
tools.HasError(err, "", 500)
|
|
|
|
fmt.Printf("%s [INFO] %s %s %s \r\n",
|
|
tools.GetCurrentTimeStr(),
|
|
c.Request.Method,
|
|
c.Request.URL.Path,
|
|
v["rolekey"],
|
|
)
|
|
|
|
if res || v["rolekey"] == "admin" {
|
|
c.Set("userInfo", models.GetUserById(uint32(tools.GetUserId(c))))
|
|
c.Next()
|
|
} else {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 403,
|
|
"msg": "对不起,您没有该接口访问权限,请联系管理员",
|
|
})
|
|
c.Abort()
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// GetCooperativeBusinessId 获取用户合作商id
|
|
func GetCooperativeBusinessId(c *gin.Context) uint32 {
|
|
u, b := c.Get("userInfo")
|
|
if !b {
|
|
return 0
|
|
}
|
|
return u.(*models.SysUserB).CooperativeBusinessId
|
|
}
|