From 00c87d7c80f8ecac3db4da428b72ef17a6c881f5 Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Sun, 13 Jun 2021 21:22:55 +0800 Subject: [PATCH] =?UTF-8?q?feat=E2=9C=A8:=20api=20rabc=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=E6=97=B6=E5=B0=86=E6=8E=92=E9=99=A4=E5=88=97=E8=A1=A8=E4=B8=AD?= =?UTF-8?q?=E7=9A=84path=E5=89=94=E9=99=A4=E4=B8=8D=E5=9C=A8=E5=8F=82?= =?UTF-8?q?=E4=B8=8E=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/middleware/permission.go | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/common/middleware/permission.go b/common/middleware/permission.go index 766f254..00d1b1f 100644 --- a/common/middleware/permission.go +++ b/common/middleware/permission.go @@ -1,6 +1,7 @@ package middleware import ( + "github.com/casbin/casbin/v2/util" "net/http" "github.com/gin-gonic/gin" @@ -17,19 +18,31 @@ func AuthCheckRole() gin.HandlerFunc { data, _ := c.Get(jwtauth.JwtPayloadKey) v := data.(jwtauth.MapClaims) e := sdk.Runtime.GetCasbinKey(c.Request.Host) - var res bool + var res, casbinExclude bool var err error //检查权限 if v["rolekey"] == "admin" { res = true - } else { - res, err = e.Enforce(v["rolekey"], c.Request.URL.Path, c.Request.Method) - if err != nil { - log.Errorf("AuthCheckRole error:%s method:%s path:%s", err, c.Request.Method, c.Request.URL.Path) - response.Error(c, 500, err, "") - return + c.Next() + return + } + for _, i := range CasbinExclude { + if util.KeyMatch2(c.Request.URL.Path, i.Url) && c.Request.Method == i.Method { + casbinExclude = true + break } } + if casbinExclude { + log.Infof("Casbin exclusion, no validation method:%s path:%s", c.Request.Method, c.Request.URL.Path) + c.Next() + return + } + res, err = e.Enforce(v["rolekey"], c.Request.URL.Path, c.Request.Method) + if err != nil { + log.Errorf("AuthCheckRole error:%s method:%s path:%s", err, c.Request.Method, c.Request.URL.Path) + response.Error(c, 500, err, "") + return + } if res { log.Infof("isTrue: %v role: %s method: %s path: %s", res, v["rolekey"], c.Request.Method, c.Request.URL.Path) @@ -43,5 +56,6 @@ func AuthCheckRole() gin.HandlerFunc { c.Abort() return } + } }