diff --git a/apis/actions/delete.go b/apis/actions/delete.go index e770c5c..bd2f91d 100644 --- a/apis/actions/delete.go +++ b/apis/actions/delete.go @@ -32,13 +32,11 @@ func DeleteAction(control dto.Control) gin.HandlerFunc { object, err = req.GenerateM() tools.HasError(err, "模型生成失败", 500) - //数据权限检查 object.SetUpdateBy(tools.GetUserIdStr(c)) - var p = new(dataPermission) - if userId := tools.GetUserIdStr(c); userId != "" { - p, err = newDataPermission(db, userId) - tools.HasError(err, "权限范围鉴定错误", 500) - } + + //数据权限检查 + p := getPermissionFromContext(c) + db = db.WithContext(c).Scopes( Permission(object.TableName(), p), ).Delete(object) diff --git a/apis/actions/index.go b/apis/actions/index.go index c66f51b..c80a99a 100644 --- a/apis/actions/index.go +++ b/apis/actions/index.go @@ -33,11 +33,10 @@ func IndexAction(m model.ActiveRecord, d dto.Index, f func() interface{}) gin.Ha tools.HasError(err, "参数验证失败", 422) err = req.Bind(c) tools.HasError(err, "参数验证失败", 422) - var p = new(dataPermission) - if userId := tools.GetUserIdStr(c); userId != "" { - p, err = newDataPermission(db, userId) - tools.HasError(err, "权限范围鉴定错误", 500) - } + + //数据权限检查 + p := getPermissionFromContext(c) + err = db.WithContext(c).Model(object). Scopes( tools.MakeCondition(req.GetNeedSearch()), diff --git a/apis/actions/permission.go b/apis/actions/permission.go index 7dc24f4..f309208 100644 --- a/apis/actions/permission.go +++ b/apis/actions/permission.go @@ -2,6 +2,7 @@ package actions import ( "errors" + "github.com/gin-gonic/gin" "go-admin/models" "go-admin/tools/config" @@ -17,6 +18,31 @@ type dataPermission struct { RoleId int } +func PermissionAction() gin.HandlerFunc { + return func(c *gin.Context) { + var err error + idb, exist := c.Get("db") + if !exist { + err = errors.New("db connect not exist") + tools.HasError(err, "", 500) + } + switch idb.(type) { + case *gorm.DB: + db := idb.(*gorm.DB) + var p = new(dataPermission) + if userId := tools.GetUserIdStr(c); userId != "" { + p, err = newDataPermission(db, userId) + tools.HasError(err, "权限范围鉴定错误", 500) + } + c.Set(PermissionKey, p) + default: + err = errors.New("db connect not exist") + tools.HasError(err, "", 500) + } + c.Next() + } +} + func newDataPermission(tx *gorm.DB, userId interface{}) (*dataPermission, error) { var err error p := &dataPermission{} @@ -59,3 +85,14 @@ func Permission(tableName string, p *dataPermission) func(db *gorm.DB) *gorm.DB } } } + +func getPermissionFromContext(c *gin.Context) *dataPermission { + p := new(dataPermission) + if pm, ok := c.Get(PermissionKey); ok { + switch pm.(type) { + case *dataPermission: + p = pm.(*dataPermission) + } + } + return p +} diff --git a/apis/actions/type.go b/apis/actions/type.go new file mode 100644 index 0000000..69beb8c --- /dev/null +++ b/apis/actions/type.go @@ -0,0 +1,5 @@ +package actions + +const ( + PermissionKey = "dataPermission" +) diff --git a/apis/actions/update.go b/apis/actions/update.go index e6e9469..356c402 100644 --- a/apis/actions/update.go +++ b/apis/actions/update.go @@ -34,11 +34,8 @@ func UpdateAction(control dto.Control) gin.HandlerFunc { object.SetUpdateBy(tools.GetUserIdStr(c)) //数据权限检查 - var p = new(dataPermission) - if userId := tools.GetUserIdStr(c); userId != "" { - p, err = newDataPermission(db, userId) - tools.HasError(err, "权限范围鉴定错误", 500) - } + p := getPermissionFromContext(c) + db = db.WithContext(c).Scopes( Permission(object.TableName(), p), ).Updates(object) diff --git a/apis/actions/view.go b/apis/actions/view.go index 7bd3763..9f291de 100644 --- a/apis/actions/view.go +++ b/apis/actions/view.go @@ -31,11 +31,10 @@ func ViewAction(control dto.Control) gin.HandlerFunc { var object model.ActiveRecord object, err = req.GenerateM() tools.HasError(err, "模型生成失败", 500) - var p = new(dataPermission) - if userId := tools.GetUserIdStr(c); userId != "" { - p, err = newDataPermission(db, userId) - tools.HasError(err, "权限范围鉴定错误", 500) - } + + //数据权限检查 + p := getPermissionFromContext(c) + err = db.WithContext(c).Scopes( Permission(object.TableName(), p), ).First(object).Error diff --git a/router/sysrouter.go b/router/sysrouter.go index ff3b36f..6f4f784 100644 --- a/router/sysrouter.go +++ b/router/sysrouter.go @@ -112,14 +112,14 @@ func registerSysJobRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddlew r := v1.Group("/sysjob").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole()) { sysJob := &models.SysJob{} - r.GET("", actions.IndexAction(sysJob, new(dto.SysJobSearch), func() interface{} { + r.GET("", actions.PermissionAction(), actions.IndexAction(sysJob, new(dto.SysJobSearch), func() interface{} { list := make([]models.SysJob, 0) return &list })) - r.GET("/:id", actions.ViewAction(new(dto.SysJobById))) + r.GET("/:id", actions.PermissionAction(), actions.ViewAction(new(dto.SysJobById))) r.POST("", actions.CreateAction(new(dto.SysJobControl))) - r.PUT("", actions.UpdateAction(new(dto.SysJobControl))) - r.DELETE("/:id", actions.DeleteAction(new(dto.SysJobById))) + r.PUT("", actions.PermissionAction(), actions.UpdateAction(new(dto.SysJobControl))) + r.DELETE("/:id", actions.PermissionAction(), actions.DeleteAction(new(dto.SysJobById))) } v1.GET("/job/remove/:jobId", sysjob.RemoveJob)