优化casbin接口鉴权问题,不再重复查询数据库

This commit is contained in:
linwenxiang committed 2020-09-21 11:39:19 +08:00
1 parent a06bd289b3
commit a8d5846483
4 files changed
+24 -4

No files matched your search

+13
View File
@@ -2,6 +2,7 @@ package system
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
mycasbin "go-admin/pkg/casbin"
"go-admin/app/admin/models" "go-admin/app/admin/models"
"go-admin/tools" "go-admin/tools"
@@ -86,6 +87,10 @@ func InsertRole(c *gin.Context) {
_, err = t.Insert(id, data.MenuIds) _, err = t.Insert(id, data.MenuIds)
tools.HasError(err, "", -1) tools.HasError(err, "", -1)
} }
_, err = mycasbin.LoadPolicy()
tools.HasError(err, "", -1)
app.OK(c, data, "添加成功") app.OK(c, data, "添加成功")
} }
@@ -112,6 +117,10 @@ func UpdateRole(c *gin.Context) {
_, err2 := t.Insert(data.RoleId, data.MenuIds) _, err2 := t.Insert(data.RoleId, data.MenuIds)
tools.HasError(err2, "修改失败(insert)", -1) tools.HasError(err2, "修改失败(insert)", -1)
} }
_, err = mycasbin.LoadPolicy()
tools.HasError(err, "", -1)
app.OK(c, result, "修改成功") app.OK(c, result, "修改成功")
} }
@@ -146,5 +155,9 @@ func DeleteRole(c *gin.Context) {
IDS := tools.IdsStrToIdsIntGroup("roleId", c) IDS := tools.IdsStrToIdsIntGroup("roleId", c)
_, err := Role.BatchDelete(IDS) _, err := Role.BatchDelete(IDS)
tools.HasError(err, "删除失败", -1) tools.HasError(err, "删除失败", -1)
_, err = mycasbin.LoadPolicy()
tools.HasError(err, "", -1)
app.OK(c, "", "删除成功") app.OK(c, "", "删除成功")
} }
+1 -2
View File
@@ -16,8 +16,7 @@ func AuthCheckRole() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
data, _ := c.Get(jwtauth.JwtPayloadKey) data, _ := c.Get(jwtauth.JwtPayloadKey)
v := data.(jwtauth.MapClaims) v := data.(jwtauth.MapClaims)
e, err := mycasbin.Casbin() e := mycasbin.Casbin()
tools.HasError(err, "", 500)
//检查权限 //检查权限
res, err := e.Enforce(v["rolekey"], c.Request.URL.Path, c.Request.Method) res, err := e.Enforce(v["rolekey"], c.Request.URL.Path, c.Request.Method)
tools.HasError(err, "", 500) tools.HasError(err, "", 500)
+9 -1
View File
@@ -39,10 +39,18 @@ func Setup() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = e.LoadPolicy()
if err != nil {
panic(err)
}
global.CasbinEnforcer = e global.CasbinEnforcer = e
} }
func Casbin() (*casbin.SyncedEnforcer, error) { func Casbin() *casbin.SyncedEnforcer {
return global.CasbinEnforcer
}
func LoadPolicy() (*casbin.SyncedEnforcer, error) {
if err := global.CasbinEnforcer.LoadPolicy(); err == nil { if err := global.CasbinEnforcer.LoadPolicy(); err == nil {
return global.CasbinEnforcer, err return global.CasbinEnforcer, err
} else { } else {
+1 -1
View File
@@ -53,7 +53,7 @@ func HasError(err error, msg string, code ...int) {
msg = err.Error() msg = err.Error()
} }
_, file, line, _ := runtime.Caller(1) _, file, line, _ := runtime.Caller(1)
log.Printf("%s:%v", file, line) log.Printf("%s:%v error: %#v", file, line, err)
panic("CustomError#" + strconv.Itoa(statusCode) + "#" + msg) panic("CustomError#" + strconv.Itoa(statusCode) + "#" + msg)
} }
} }