fix: 角色数据权限范围配置示例系统日志区分

This commit is contained in:
TsMask
2024-06-18 11:31:53 +08:00
parent 857f7de6d1
commit 10e361ba7f
11 changed files with 51 additions and 17 deletions

View File

@@ -212,6 +212,11 @@ func LoginUserToDataScopeSQL(c *gin.Context, deptAlias string, userAlias string)
conditions = append(conditions, sql)
}
if roledatascope.DEPT == dataScope {
sql := fmt.Sprintf(`%s.dept_id = '%s'`, deptAlias, userInfo.DeptID)
conditions = append(conditions, sql)
}
if roledatascope.DEPT_AND_CHILD == dataScope {
sql := fmt.Sprintf(`%s.dept_id IN ( SELECT dept_id FROM sys_dept WHERE dept_id = '%s' or find_in_set('%s' , ancestors ) )`, deptAlias, userInfo.DeptID, userInfo.DeptID)
conditions = append(conditions, sql)
@@ -220,7 +225,7 @@ func LoginUserToDataScopeSQL(c *gin.Context, deptAlias string, userAlias string)
if roledatascope.SELF == dataScope {
// 数据权限为仅本人且没有userAlias别名不查询任何数据
if userAlias == "" {
sql := fmt.Sprintf(`%s.dept_id = '0'`, deptAlias)
sql := fmt.Sprintf(`%s.parent_id = '0'`, deptAlias)
conditions = append(conditions, sql)
} else {
sql := fmt.Sprintf(`%s.user_id = '%s'`, userAlias, userInfo.UserID)

View File

@@ -40,7 +40,8 @@ type SysLogLoginController struct {
// GET /list
func (s *SysLogLoginController) List(c *gin.Context) {
querys := ctx.QueryMap(c)
data := s.sysLogLoginService.SelectSysLogLoginPage(querys)
dataScopeSQL := ctx.LoginUserToDataScopeSQL(c, "d", "u")
data := s.sysLogLoginService.SelectSysLogLoginPage(querys, dataScopeSQL)
rows := data["rows"].([]model.SysLogLogin)
// 闭包函数处理多语言

View File

@@ -42,7 +42,8 @@ func (s *SysLogOperateController) List(c *gin.Context) {
querys["title"] = i18n.TFindKeyPrefix(language, "log.operate.title", v.(string))
}
data := s.SysLogOperateService.SelectSysLogOperatePage(querys)
dataScopeSQL := ctx.LoginUserToDataScopeSQL(c, "d", "u")
data := s.SysLogOperateService.SelectSysLogOperatePage(querys, dataScopeSQL)
rows := data["rows"].([]model.SysLogOperate)
// 闭包函数处理多语言

View File

@@ -5,7 +5,7 @@ import "be.ems/src/modules/system/model"
// ISysLogLogin 系统登录日志表 数据层接口
type ISysLogLogin interface {
// SelectSysLogLoginPage 分页查询系统登录日志集合
SelectSysLogLoginPage(query map[string]any) map[string]any
SelectSysLogLoginPage(query map[string]any, dataScopeSQL string) map[string]any
// SelectSysLogLoginList 查询系统登录日志集合
SelectSysLogLoginList(sysLogLogin model.SysLogLogin) []model.SysLogLogin

View File

@@ -53,7 +53,7 @@ func (r *SysLogLoginImpl) convertResultRows(rows []map[string]any) []model.SysLo
}
// SelectSysLogLoginPage 分页查询系统登录日志集合
func (r *SysLogLoginImpl) SelectSysLogLoginPage(query map[string]any) map[string]any {
func (r *SysLogLoginImpl) SelectSysLogLoginPage(query map[string]any, dataScopeSQL string) map[string]any {
// 查询条件拼接
var conditions []string
var params []any
@@ -87,9 +87,23 @@ func (r *SysLogLoginImpl) SelectSysLogLoginPage(query map[string]any) map[string
}
// 构建查询条件语句
selectSql := r.selectSql
totalSql := "select count(login_id) as 'total' from sys_log_login"
whereSql := ""
if len(conditions) > 0 {
whereSql += " where " + strings.Join(conditions, " and ")
whereSql += dataScopeSQL
} else if dataScopeSQL != "" {
totalSql = `select count(o.login_id) as 'total'
from sys_log_login o
left join sys_user u on u.user_name = o.user_name
left join sys_dept d on u.dept_id = d.dept_id`
selectSql = `select o.login_id, o.user_name, o.ipaddr, o.login_location,
o.browser, o.os, o.status, o.msg, o.login_time
from sys_log_login o
left join sys_user u on u.user_name = o.user_name
left join sys_dept d on u.dept_id = d.dept_id`
whereSql += " where 1=1" + dataScopeSQL
}
// 查询结果
@@ -99,7 +113,6 @@ func (r *SysLogLoginImpl) SelectSysLogLoginPage(query map[string]any) map[string
}
// 查询数量 长度为0直接返回
totalSql := "select count(1) as 'total' from sys_log_login"
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
if err != nil {
logger.Errorf("total err => %v", err)
@@ -119,7 +132,7 @@ func (r *SysLogLoginImpl) SelectSysLogLoginPage(query map[string]any) map[string
params = append(params, pageSize)
// 查询数据
querySql := r.selectSql + whereSql + pageSql
querySql := selectSql + whereSql + dataScopeSQL + pageSql
results, err := datasource.RawDB("", querySql, params)
if err != nil {
logger.Errorf("query err => %v", err)

View File

@@ -5,7 +5,7 @@ import "be.ems/src/modules/system/model"
// ISysLogOperate 操作日志表 数据层接口
type ISysLogOperate interface {
// SelectSysLogOperatePage 分页查询系统操作日志集合
SelectSysLogOperatePage(query map[string]any) map[string]any
SelectSysLogOperatePage(query map[string]any, dataScopeSQL string) map[string]any
// SelectSysLogOperateList 查询系统操作日志集合
SelectSysLogOperateList(sysLogOperate model.SysLogOperate) []model.SysLogOperate

View File

@@ -62,7 +62,7 @@ func (r *SysLogOperateImpl) convertResultRows(rows []map[string]any) []model.Sys
}
// SelectSysLogOperatePage 分页查询系统操作日志集合
func (r *SysLogOperateImpl) SelectSysLogOperatePage(query map[string]any) map[string]any {
func (r *SysLogOperateImpl) SelectSysLogOperatePage(query map[string]any, dataScopeSQL string) map[string]any {
// 查询条件拼接
var conditions []string
var params []any
@@ -100,9 +100,24 @@ func (r *SysLogOperateImpl) SelectSysLogOperatePage(query map[string]any) map[st
}
// 构建查询条件语句
selectSql := r.selectSql
totalSql := "select count(oper_id) as 'total' from sys_log_operate"
whereSql := ""
if len(conditions) > 0 {
whereSql += " where " + strings.Join(conditions, " and ")
whereSql += dataScopeSQL
} else if dataScopeSQL != "" {
totalSql = `select count(o.oper_id) as 'total'
from sys_log_operate o
left join sys_user u on u.user_name = o.oper_name
left join sys_dept d on u.dept_id = d.dept_id`
selectSql = `select
o.oper_id, o.title, o.business_type, o.method, o.request_method, o.operator_type, o.oper_name, o.dept_name,
o.oper_url, o.oper_ip, o.oper_location, o.oper_param, o.oper_msg, o.status, o.oper_time, o.cost_time
from sys_log_operate o
left join sys_user u on u.user_name = o.oper_name
left join sys_dept d on u.dept_id = d.dept_id`
whereSql += " where 1=1" + dataScopeSQL
}
// 查询结果
@@ -112,7 +127,6 @@ func (r *SysLogOperateImpl) SelectSysLogOperatePage(query map[string]any) map[st
}
// 查询数量 长度为0直接返回
totalSql := "select count(1) as 'total' from sys_log_operate"
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
if err != nil {
logger.Errorf("total err => %v", err)
@@ -132,7 +146,7 @@ func (r *SysLogOperateImpl) SelectSysLogOperatePage(query map[string]any) map[st
params = append(params, pageSize)
// 查询数据
querySql := r.selectSql + whereSql + pageSql
querySql := selectSql + whereSql + pageSql
results, err := datasource.RawDB("", querySql, params)
if err != nil {
logger.Errorf("query err => %v", err)

View File

@@ -5,7 +5,7 @@ import "be.ems/src/modules/system/model"
// ISysLogLogin 系统登录日志 服务层接口
type ISysLogLogin interface {
// SelectSysLogLoginPage 分页查询系统登录日志集合
SelectSysLogLoginPage(query map[string]any) map[string]any
SelectSysLogLoginPage(query map[string]any, dataScopeSQL string) map[string]any
// SelectSysLogLoginList 查询系统登录日志集合
SelectSysLogLoginList(sysLogLogin model.SysLogLogin) []model.SysLogLogin

View File

@@ -17,8 +17,8 @@ type SysLogLoginImpl struct {
}
// SelectSysLogLoginPage 分页查询系统登录日志集合
func (s *SysLogLoginImpl) SelectSysLogLoginPage(query map[string]any) map[string]any {
return s.sysLogLoginService.SelectSysLogLoginPage(query)
func (s *SysLogLoginImpl) SelectSysLogLoginPage(query map[string]any, dataScopeSQL string) map[string]any {
return s.sysLogLoginService.SelectSysLogLoginPage(query, dataScopeSQL)
}
// SelectSysLogLoginList 查询系统登录日志集合

View File

@@ -5,7 +5,7 @@ import "be.ems/src/modules/system/model"
// ISysLogOperate 操作日志表 服务层接口
type ISysLogOperate interface {
// SelectSysLogOperatePage 分页查询系统操作日志集合
SelectSysLogOperatePage(query map[string]any) map[string]any
SelectSysLogOperatePage(query map[string]any, dataScopeSQL string) map[string]any
// SelectSysLogOperateList 查询系统操作日志集合
SelectSysLogOperateList(sysLogOperate model.SysLogOperate) []model.SysLogOperate

View File

@@ -17,8 +17,8 @@ type SysLogOperateImpl struct {
}
// SelectSysLogOperatePage 分页查询系统操作日志集合
func (r *SysLogOperateImpl) SelectSysLogOperatePage(query map[string]any) map[string]any {
return r.SysLogOperateService.SelectSysLogOperatePage(query)
func (r *SysLogOperateImpl) SelectSysLogOperatePage(query map[string]any, dataScopeSQL string) map[string]any {
return r.SysLogOperateService.SelectSysLogOperatePage(query, dataScopeSQL)
}
// SelectSysLogOperateList 查询系统操作日志集合