From d5c354da95be922c4a04d12e5ac83d1e134ab6e0 Mon Sep 17 00:00:00 2001 From: K Date: Tue, 23 Nov 2021 23:47:02 +0800 Subject: [PATCH 1/2] chore: fix db connector nil pointer dereference --- server/go.mod | 1 - server/service/system/sys_user.go | 5 +++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/server/go.mod b/server/go.mod index 365a8f2b..5a52a65e 100644 --- a/server/go.mod +++ b/server/go.mod @@ -16,7 +16,6 @@ require ( github.com/go-redis/redis/v8 v8.11.0 github.com/go-sql-driver/mysql v1.5.0 github.com/gookit/color v1.3.1 - github.com/jackc/pgx/v4 v4.7.1 github.com/jordan-wright/email v0.0.0-20200824153738-3f5bafa1cd84 github.com/mojocn/base64Captcha v1.3.1 github.com/natefinch/lumberjack v2.0.0+incompatible diff --git a/server/service/system/sys_user.go b/server/service/system/sys_user.go index f39f804a..9b260538 100644 --- a/server/service/system/sys_user.go +++ b/server/service/system/sys_user.go @@ -2,6 +2,7 @@ package system import ( "errors" + "fmt" "github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/model/common/request" @@ -39,6 +40,10 @@ func (userService *UserService) Register(u system.SysUser) (err error, userInter //@return: err error, userInter *model.SysUser func (userService *UserService) Login(u *system.SysUser) (err error, userInter *system.SysUser) { + if nil == global.GVA_DB { + return fmt.Errorf("db not init"), nil + } + var user system.SysUser u.Password = utils.MD5V([]byte(u.Password)) err = global.GVA_DB.Where("username = ? AND password = ?", u.Username, u.Password).Preload("Authorities").Preload("Authority").First(&user).Error From 62153288158dd7cc6d0d46175aaad51d952b8d31 Mon Sep 17 00:00:00 2001 From: dyzsoft Date: Wed, 24 Nov 2021 15:23:22 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9Acasbin=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E4=BC=98=E5=8C=96=EF=BC=8C=E5=9C=A8=E4=B8=AD=E9=97=B4?= =?UTF-8?q?=E4=BB=B6=E4=B8=AD=E8=8E=B7=E5=8F=96=E8=AF=B7=E6=B1=82=E7=9A=84?= =?UTF-8?q?=20Path=EF=BC=8C=E5=9C=A8CasbinModel=E5=AE=9A=E4=B9=89=E4=B8=AD?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E4=BD=BF=E7=94=A8keymatch2=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=EF=BC=8C=E5=87=8F=E5=B0=91=E4=B8=80=E6=AC=A1?= =?UTF-8?q?path=E8=8E=B7=E5=8F=96=E5=92=8C=E5=87=BD=E6=95=B0=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E3=80=82=20server/middleware/casbin=5Frbac.go?= =?UTF-8?q?=EF=BC=9A18=E8=A1=8C=E4=BF=AE=E6=94=B9=E4=B8=BA=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E7=9A=84path=E4=BD=9C=E4=B8=BAobj=20server/service/sy?= =?UTF-8?q?stem/sys=5Fcasbin.go:=E5=88=A0=E6=8E=89=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E6=B3=A8=E5=86=8C=E5=87=BD=E6=95=B0=20server/resource?= =?UTF-8?q?/rbac=5Fmodel.conf=EF=BC=9A=E9=AA=8C=E8=AF=81=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=BAkeyMathc2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/middleware/casbin_rbac.go | 4 ++-- server/resource/rbac_model.conf | 2 +- server/service/system/sys_casbin.go | 28 ---------------------------- 3 files changed, 3 insertions(+), 31 deletions(-) diff --git a/server/middleware/casbin_rbac.go b/server/middleware/casbin_rbac.go index b21345ec..ea9c9824 100644 --- a/server/middleware/casbin_rbac.go +++ b/server/middleware/casbin_rbac.go @@ -14,8 +14,8 @@ var casbinService = service.ServiceGroupApp.SystemServiceGroup.CasbinService func CasbinHandler() gin.HandlerFunc { return func(c *gin.Context) { waitUse, _ := utils.GetClaims(c) - // 获取请求的URI - obj := c.Request.URL.RequestURI() + // 获取请求的PATH + obj := c.Request.URL.Path // 获取请求方法 act := c.Request.Method // 获取用户的角色 diff --git a/server/resource/rbac_model.conf b/server/resource/rbac_model.conf index 04ebedff..b5918d45 100644 --- a/server/resource/rbac_model.conf +++ b/server/resource/rbac_model.conf @@ -11,4 +11,4 @@ g = _, _ e = some(where (p.eft == allow)) [matchers] -m = r.sub == p.sub && ParamsMatch(r.obj,p.obj) && r.act == p.act +m = r.sub == p.sub && keyMatch2(r.obj,p.obj) && r.act == p.act diff --git a/server/service/system/sys_casbin.go b/server/service/system/sys_casbin.go index c583ab07..118173c6 100644 --- a/server/service/system/sys_casbin.go +++ b/server/service/system/sys_casbin.go @@ -2,11 +2,9 @@ package system import ( "errors" - "strings" "sync" "github.com/casbin/casbin/v2" - "github.com/casbin/casbin/v2/util" gormadapter "github.com/casbin/gorm-adapter/v3" "github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/model/system" @@ -104,33 +102,7 @@ func (casbinService *CasbinService) Casbin() *casbin.SyncedEnforcer { once.Do(func() { a, _ := gormadapter.NewAdapterByDB(global.GVA_DB) syncedEnforcer, _ = casbin.NewSyncedEnforcer(global.GVA_CONFIG.Casbin.ModelPath, a) - syncedEnforcer.AddFunction("ParamsMatch", casbinService.ParamsMatchFunc) }) _ = syncedEnforcer.LoadPolicy() return syncedEnforcer } - -//@author: [piexlmax](https://github.com/piexlmax) -//@function: ParamsMatch -//@description: 自定义规则函数 -//@param: fullNameKey1 string, key2 string -//@return: bool - -func (casbinService *CasbinService) ParamsMatch(fullNameKey1 string, key2 string) bool { - key1 := strings.Split(fullNameKey1, "?")[0] - // 剥离路径后再使用casbin的keyMatch2 - return util.KeyMatch2(key1, key2) -} - -//@author: [piexlmax](https://github.com/piexlmax) -//@function: ParamsMatchFunc -//@description: 自定义规则函数 -//@param: args ...interface{} -//@return: interface{}, error - -func (casbinService *CasbinService) ParamsMatchFunc(args ...interface{}) (interface{}, error) { - name1 := args[0].(string) - name2 := args[1].(string) - - return casbinService.ParamsMatch(name1, name2), nil -}