拆分数据权限验证,让用户选择是否加
This commit is contained in:
1 parent
3a9831fc8c
commit
560e650070
7 files changed
+60
-25
No files matched your search
@@ -32,13 +32,11 @@ func DeleteAction(control dto.Control) gin.HandlerFunc {
|
|||||||
object, err = req.GenerateM()
|
object, err = req.GenerateM()
|
||||||
tools.HasError(err, "模型生成失败", 500)
|
tools.HasError(err, "模型生成失败", 500)
|
||||||
|
|
||||||
//数据权限检查
|
|
||||||
object.SetUpdateBy(tools.GetUserIdStr(c))
|
object.SetUpdateBy(tools.GetUserIdStr(c))
|
||||||
var p = new(dataPermission)
|
|
||||||
if userId := tools.GetUserIdStr(c); userId != "" {
|
//数据权限检查
|
||||||
p, err = newDataPermission(db, userId)
|
p := getPermissionFromContext(c)
|
||||||
tools.HasError(err, "权限范围鉴定错误", 500)
|
|
||||||
}
|
|
||||||
db = db.WithContext(c).Scopes(
|
db = db.WithContext(c).Scopes(
|
||||||
Permission(object.TableName(), p),
|
Permission(object.TableName(), p),
|
||||||
).Delete(object)
|
).Delete(object)
|
||||||
|
|||||||
@@ -33,11 +33,10 @@ func IndexAction(m model.ActiveRecord, d dto.Index, f func() interface{}) gin.Ha
|
|||||||
tools.HasError(err, "参数验证失败", 422)
|
tools.HasError(err, "参数验证失败", 422)
|
||||||
err = req.Bind(c)
|
err = req.Bind(c)
|
||||||
tools.HasError(err, "参数验证失败", 422)
|
tools.HasError(err, "参数验证失败", 422)
|
||||||
var p = new(dataPermission)
|
|
||||||
if userId := tools.GetUserIdStr(c); userId != "" {
|
//数据权限检查
|
||||||
p, err = newDataPermission(db, userId)
|
p := getPermissionFromContext(c)
|
||||||
tools.HasError(err, "权限范围鉴定错误", 500)
|
|
||||||
}
|
|
||||||
err = db.WithContext(c).Model(object).
|
err = db.WithContext(c).Model(object).
|
||||||
Scopes(
|
Scopes(
|
||||||
tools.MakeCondition(req.GetNeedSearch()),
|
tools.MakeCondition(req.GetNeedSearch()),
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package actions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"go-admin/models"
|
"go-admin/models"
|
||||||
"go-admin/tools/config"
|
"go-admin/tools/config"
|
||||||
|
|
||||||
@@ -17,6 +18,31 @@ type dataPermission struct {
|
|||||||
RoleId int
|
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) {
|
func newDataPermission(tx *gorm.DB, userId interface{}) (*dataPermission, error) {
|
||||||
var err error
|
var err error
|
||||||
p := &dataPermission{}
|
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
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package actions
|
||||||
|
|
||||||
|
const (
|
||||||
|
PermissionKey = "dataPermission"
|
||||||
|
)
|
||||||
@@ -34,11 +34,8 @@ func UpdateAction(control dto.Control) gin.HandlerFunc {
|
|||||||
object.SetUpdateBy(tools.GetUserIdStr(c))
|
object.SetUpdateBy(tools.GetUserIdStr(c))
|
||||||
|
|
||||||
//数据权限检查
|
//数据权限检查
|
||||||
var p = new(dataPermission)
|
p := getPermissionFromContext(c)
|
||||||
if userId := tools.GetUserIdStr(c); userId != "" {
|
|
||||||
p, err = newDataPermission(db, userId)
|
|
||||||
tools.HasError(err, "权限范围鉴定错误", 500)
|
|
||||||
}
|
|
||||||
db = db.WithContext(c).Scopes(
|
db = db.WithContext(c).Scopes(
|
||||||
Permission(object.TableName(), p),
|
Permission(object.TableName(), p),
|
||||||
).Updates(object)
|
).Updates(object)
|
||||||
|
|||||||
@@ -31,11 +31,10 @@ func ViewAction(control dto.Control) gin.HandlerFunc {
|
|||||||
var object model.ActiveRecord
|
var object model.ActiveRecord
|
||||||
object, err = req.GenerateM()
|
object, err = req.GenerateM()
|
||||||
tools.HasError(err, "模型生成失败", 500)
|
tools.HasError(err, "模型生成失败", 500)
|
||||||
var p = new(dataPermission)
|
|
||||||
if userId := tools.GetUserIdStr(c); userId != "" {
|
//数据权限检查
|
||||||
p, err = newDataPermission(db, userId)
|
p := getPermissionFromContext(c)
|
||||||
tools.HasError(err, "权限范围鉴定错误", 500)
|
|
||||||
}
|
|
||||||
err = db.WithContext(c).Scopes(
|
err = db.WithContext(c).Scopes(
|
||||||
Permission(object.TableName(), p),
|
Permission(object.TableName(), p),
|
||||||
).First(object).Error
|
).First(object).Error
|
||||||
|
|||||||
+4
-4
@@ -112,14 +112,14 @@ func registerSysJobRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddlew
|
|||||||
r := v1.Group("/sysjob").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole())
|
r := v1.Group("/sysjob").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole())
|
||||||
{
|
{
|
||||||
sysJob := &models.SysJob{}
|
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)
|
list := make([]models.SysJob, 0)
|
||||||
return &list
|
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.POST("", actions.CreateAction(new(dto.SysJobControl)))
|
||||||
r.PUT("", actions.UpdateAction(new(dto.SysJobControl)))
|
r.PUT("", actions.PermissionAction(), actions.UpdateAction(new(dto.SysJobControl)))
|
||||||
r.DELETE("/:id", actions.DeleteAction(new(dto.SysJobById)))
|
r.DELETE("/:id", actions.PermissionAction(), actions.DeleteAction(new(dto.SysJobById)))
|
||||||
}
|
}
|
||||||
|
|
||||||
v1.GET("/job/remove/:jobId", sysjob.RemoveJob)
|
v1.GET("/job/remove/:jobId", sysjob.RemoveJob)
|
||||||
|
|||||||
Reference in new issue
Block a user