在actions中添加权限
This commit is contained in:
1 parent
61451d149a
commit
abc8b271c4
4 files changed
+78
-3
No files matched your search
@@ -33,9 +33,13 @@ func IndexAction(m model.ActiveRecord, d dto.Dtor) gin.HandlerFunc {
|
|||||||
tools.HasError(err, "参数验证失败", 422)
|
tools.HasError(err, "参数验证失败", 422)
|
||||||
err = req.Validate()
|
err = req.Validate()
|
||||||
tools.HasError(err, "参数验证失败", 422)
|
tools.HasError(err, "参数验证失败", 422)
|
||||||
|
p, err := newDataPermission(db, tools.GetUserId(c))
|
||||||
err = db.WithContext(c).Model(object).
|
err = db.WithContext(c).Model(object).
|
||||||
Scopes(tools.MakeCondition(req.GetNeedSearch())).
|
Scopes(
|
||||||
Scopes(tools.Paginate(req.GetPageSize(), req.GetPageIndex())).
|
tools.MakeCondition(req.GetNeedSearch()),
|
||||||
|
tools.Paginate(req.GetPageSize(), req.GetPageIndex()),
|
||||||
|
Permission(object.TableName(), p),
|
||||||
|
).
|
||||||
Find(list).Limit(-1).Offset(-1).
|
Find(list).Limit(-1).Offset(-1).
|
||||||
Count(&count).Error
|
Count(&count).Error
|
||||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package actions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"go-admin/models"
|
||||||
|
"go-admin/tools/config"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
|
||||||
|
"go-admin/tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
type dataPermission struct {
|
||||||
|
DataScope string
|
||||||
|
UserId int
|
||||||
|
DeptId int
|
||||||
|
RoleId int
|
||||||
|
}
|
||||||
|
|
||||||
|
func newDataPermission(tx *gorm.DB, userId interface{}) (*dataPermission, error) {
|
||||||
|
var err error
|
||||||
|
p := &dataPermission{}
|
||||||
|
sysUser := new(models.SysUser)
|
||||||
|
sysRole := new(models.SysRole)
|
||||||
|
|
||||||
|
err = sysUser.GetByUserId(tx, userId)
|
||||||
|
if err != nil {
|
||||||
|
err = errors.New("获取用户数据出错 msg:" + err.Error())
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
p.UserId = sysUser.UserId
|
||||||
|
p.RoleId = sysUser.RoleId
|
||||||
|
p.DeptId = sysUser.DeptId
|
||||||
|
err = sysRole.GetById(tx, sysUser.RoleId)
|
||||||
|
if err != nil {
|
||||||
|
err = errors.New("获取用户数据出错 msg:" + err.Error())
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
p.DataScope = sysRole.DataScope
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Permission(tableName string, p *dataPermission) func(db *gorm.DB) *gorm.DB {
|
||||||
|
return func(db *gorm.DB) *gorm.DB {
|
||||||
|
if !config.ApplicationConfig.EnableDP {
|
||||||
|
return db
|
||||||
|
}
|
||||||
|
switch p.DataScope {
|
||||||
|
case "2":
|
||||||
|
return db.Where(tableName+".create_by in (select sys_user.user_id from sys_role_dept left join sys_user on sys_user.dept_id=sys_role_dept.dept_id where sys_role_dept.role_id = ?)", p.RoleId)
|
||||||
|
case "3":
|
||||||
|
return db.Where(tableName+".create_by in (SELECT user_id from sys_user where dept_id = ? )", p.DeptId)
|
||||||
|
case "4":
|
||||||
|
return db.Where(tableName+".create_by in (SELECT user_id from sys_user where sys_user.dept_id in(select dept_id from sys_dept where dept_path like ? ))", "%"+tools.IntToString(p.DeptId)+"%")
|
||||||
|
case "5":
|
||||||
|
return db.Where(tableName+".create_by = ?", p.UserId)
|
||||||
|
default:
|
||||||
|
return db
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
|
||||||
orm "go-admin/global"
|
orm "go-admin/global"
|
||||||
"go-admin/tools"
|
"go-admin/tools"
|
||||||
@@ -33,6 +34,10 @@ type MenuIdList struct {
|
|||||||
MenuId int `json:"menuId"`
|
MenuId int `json:"menuId"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (role *SysRole) GetById(tx *gorm.DB, id interface{}) error {
|
||||||
|
return tx.First(role, id).Error
|
||||||
|
}
|
||||||
|
|
||||||
func (role *SysRole) GetPage(pageSize int, pageIndex int) ([]SysRole, int, error) {
|
func (role *SysRole) GetPage(pageSize int, pageIndex int) ([]SysRole, int, error) {
|
||||||
var doc []SysRole
|
var doc []SysRole
|
||||||
|
|
||||||
|
|||||||
+6
-1
@@ -2,6 +2,7 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"gorm.io/gorm"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -224,7 +225,7 @@ func (e *SysUser) GetPage(pageSize int, pageIndex int) ([]SysUserPage, int, erro
|
|||||||
|
|
||||||
var count int64
|
var count int64
|
||||||
|
|
||||||
if err := table.Scopes(DataScopes(e.TableName(),userid)).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
|
if err := table.Scopes(DataScopes(e.TableName(), userid)).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
//table.Where("sys_user.deleted_at IS NULL").Count(&count)
|
//table.Where("sys_user.deleted_at IS NULL").Count(&count)
|
||||||
@@ -316,3 +317,7 @@ func (e *SysUser) SetPwd(pwd SysUserPwd) (Result bool, err error) {
|
|||||||
tools.HasError(err, "更新密码失败(代码202)", 500)
|
tools.HasError(err, "更新密码失败(代码202)", 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *SysUser) GetByUserId(tx *gorm.DB, id interface{}) error {
|
||||||
|
return tx.First(e, id).Error
|
||||||
|
}
|
||||||
Reference in new issue
Block a user