拆分数据权限验证,让用户选择是否加

This commit is contained in:
linwenxiang committed 2020-09-03 19:52:15 +08:00
1 parent 3a9831fc8c
commit 560e650070
7 files changed
+60 -25

No files matched your search

+4 -6
View File
@@ -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)
+4 -5
View File
@@ -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()),
+37
View File
@@ -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
}
+5
View File
@@ -0,0 +1,5 @@
package actions
const (
PermissionKey = "dataPermission"
)
+2 -5
View File
@@ -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)
+4 -5
View File
@@ -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
+4 -4
View File
@@ -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)