印度包网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.1 KiB

2 months ago
package middleware
import (
"server/modules/customer/app"
"server/modules/customer/bdb"
"server/modules/customer/values"
"strings"
"github.com/gin-gonic/gin"
)
// 进行权限校验
func PowerMiddleWare() gin.HandlerFunc {
return func(c *gin.Context) {
path := c.Request.RequestURI
if PassURL(path) {
c.Next()
return
}
a := app.NewApp(c)
if !powerPass(a.User, path) {
a.Code = values.CodePower
a.Response()
c.Abort()
return
}
c.Next()
}
}
func powerPass(u *values.User, path string) bool {
if u == nil {
return false
}
if u.Role == values.UserRoleAdmin {
return true
}
// 第一步找到主页签
p := 0
for s, v := range values.PowerMap {
if strings.Contains(path, s) {
p = v
break
}
}
// 不在权限控制范围
if p == 0 {
return true
}
buttons, ok := bdb.GetPowerByRole(u.Role)[p]
if !ok {
return false
}
pbutton, ok2 := values.PowerButtonMap[p]
// 该页签没有子按钮
if !ok2 {
return true
}
for i, v := range pbutton {
if v == path {
if i > len(pbutton)-1 {
return true
}
return buttons[i] == 1
}
}
return true
}