mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-20 02:08:15 +08:00
update 抽象数据权限功能 增加内置数据 与 内置服务 实现更强大的扩展性
This commit is contained in:
parent
85a7c64bf3
commit
f99ffb5f6c
@ -162,7 +162,7 @@ public class SysUser extends BaseEntity {
|
|||||||
private Long[] postIds;
|
private Long[] postIds;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 角色ID
|
* 数据权限 当前角色ID
|
||||||
*/
|
*/
|
||||||
@ApiModelProperty(value = "角色ID")
|
@ApiModelProperty(value = "角色ID")
|
||||||
@TableField(exist = false)
|
@TableField(exist = false)
|
||||||
|
|||||||
@ -9,7 +9,10 @@ import lombok.Getter;
|
|||||||
*
|
*
|
||||||
* 语法支持 spel 模板表达式
|
* 语法支持 spel 模板表达式
|
||||||
*
|
*
|
||||||
* 内置数据 当前用户的 deptId 部门id roleId 角色id userId 用户id
|
* 内置数据 user 当前用户 内容参考 SysUser
|
||||||
|
* 如需扩展数据 需往 SysUser 内注入
|
||||||
|
* 内置服务 sdss 系统数据权限服务 内容参考 SysDataScopeService
|
||||||
|
* 如需扩展更多自定义服务 可以参考 sdss 自行编写
|
||||||
*
|
*
|
||||||
* @author Lion Li
|
* @author Lion Li
|
||||||
*/
|
*/
|
||||||
@ -25,22 +28,22 @@ public enum DataScopeType {
|
|||||||
/**
|
/**
|
||||||
* 自定数据权限
|
* 自定数据权限
|
||||||
*/
|
*/
|
||||||
CUSTOM("2", " #{#deptName} IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = #{#roleId} ) "),
|
CUSTOM("2", " #{#deptName} IN ( #{@sdss.getRoleCustom( #user.roleId )} ) "),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 部门数据权限
|
* 部门数据权限
|
||||||
*/
|
*/
|
||||||
DEPT("3", " #{#deptName} = #{#deptId} "),
|
DEPT("3", " #{#deptName} = #{#user.deptId} "),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 部门及以下数据权限
|
* 部门及以下数据权限
|
||||||
*/
|
*/
|
||||||
DEPT_AND_CHILD("4", " #{#deptName} IN ( SELECT dept_id FROM sys_dept WHERE dept_id = #{#deptId} OR find_in_set( #{#deptId} , ancestors ) )"),
|
DEPT_AND_CHILD("4", " #{#deptName} IN ( #{@sdss.getDeptAndChild( #user.deptId )} )"),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 仅本人数据权限
|
* 仅本人数据权限
|
||||||
*/
|
*/
|
||||||
SELF("5", " #{#userName?:1} = #{#userId} ");
|
SELF("5", " #{#userName?:1} = #{#user.userId} ");
|
||||||
|
|
||||||
private final String code;
|
private final String code;
|
||||||
|
|
||||||
|
|||||||
@ -19,7 +19,8 @@ import net.sf.jsqlparser.JSQLParserException;
|
|||||||
import net.sf.jsqlparser.expression.Expression;
|
import net.sf.jsqlparser.expression.Expression;
|
||||||
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
|
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
|
||||||
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
||||||
import org.springframework.expression.EvaluationContext;
|
import org.springframework.context.expression.BeanFactoryResolver;
|
||||||
|
import org.springframework.expression.BeanResolver;
|
||||||
import org.springframework.expression.ExpressionParser;
|
import org.springframework.expression.ExpressionParser;
|
||||||
import org.springframework.expression.ParserContext;
|
import org.springframework.expression.ParserContext;
|
||||||
import org.springframework.expression.common.TemplateParserContext;
|
import org.springframework.expression.common.TemplateParserContext;
|
||||||
@ -41,6 +42,7 @@ public class PlusDataPermissionHandler {
|
|||||||
|
|
||||||
private final ExpressionParser parser = new SpelExpressionParser();
|
private final ExpressionParser parser = new SpelExpressionParser();
|
||||||
private final ParserContext parserContext = new TemplateParserContext();
|
private final ParserContext parserContext = new TemplateParserContext();
|
||||||
|
private final BeanResolver beanResolver = new BeanFactoryResolver(SpringUtils.getBeanFactory());
|
||||||
|
|
||||||
public Expression getSqlSegment(Expression where, String mappedStatementId, boolean isSelect) {
|
public Expression getSqlSegment(Expression where, String mappedStatementId, boolean isSelect) {
|
||||||
DataColumn[] dataColumns = findAnnotation(mappedStatementId);
|
DataColumn[] dataColumns = findAnnotation(mappedStatementId);
|
||||||
@ -74,15 +76,16 @@ public class PlusDataPermissionHandler {
|
|||||||
private String buildDataFilter(SysUser user, DataColumn[] dataColumns, boolean isSelect) {
|
private String buildDataFilter(SysUser user, DataColumn[] dataColumns, boolean isSelect) {
|
||||||
StringBuilder sqlString = new StringBuilder();
|
StringBuilder sqlString = new StringBuilder();
|
||||||
|
|
||||||
EvaluationContext context = new StandardEvaluationContext();
|
StandardEvaluationContext context = new StandardEvaluationContext();
|
||||||
context.setVariable("userId", user.getUserId());
|
context.setBeanResolver(beanResolver);
|
||||||
context.setVariable("deptId", user.getDeptId());
|
context.setVariable("user", user);
|
||||||
|
|
||||||
for (DataColumn dataColumn : dataColumns) {
|
for (DataColumn dataColumn : dataColumns) {
|
||||||
// 设置注解变量 key 为表达式变量 value 为变量值
|
// 设置注解变量 key 为表达式变量 value 为变量值
|
||||||
context.setVariable(dataColumn.key(), dataColumn.value());
|
context.setVariable(dataColumn.key(), dataColumn.value());
|
||||||
for (SysRole role : user.getRoles()) {
|
for (SysRole role : user.getRoles()) {
|
||||||
context.setVariable("roleId", role.getRoleId());
|
user.setRoleId(role.getRoleId());
|
||||||
|
|
||||||
// 获取角色权限泛型
|
// 获取角色权限泛型
|
||||||
DataScopeType type = DataScopeType.findCode(role.getDataScope());
|
DataScopeType type = DataScopeType.findCode(role.getDataScope());
|
||||||
if (ObjectUtil.isNull(type)) {
|
if (ObjectUtil.isNull(type)) {
|
||||||
|
|||||||
@ -1,7 +1,5 @@
|
|||||||
package com.ruoyi.system.mapper;
|
package com.ruoyi.system.mapper;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
|
||||||
import com.ruoyi.common.annotation.DataColumn;
|
import com.ruoyi.common.annotation.DataColumn;
|
||||||
import com.ruoyi.common.annotation.DataPermission;
|
import com.ruoyi.common.annotation.DataPermission;
|
||||||
import com.ruoyi.common.core.domain.entity.SysDept;
|
import com.ruoyi.common.core.domain.entity.SysDept;
|
||||||
@ -28,12 +26,6 @@ public interface SysDeptMapper extends BaseMapperPlus<SysDept> {
|
|||||||
})
|
})
|
||||||
List<SysDept> selectDeptList(SysDept dept);
|
List<SysDept> selectDeptList(SysDept dept);
|
||||||
|
|
||||||
@Override
|
|
||||||
@DataPermission({
|
|
||||||
@DataColumn(key = "deptName", value = "dept_id")
|
|
||||||
})
|
|
||||||
List<SysDept> selectList(@Param(Constants.WRAPPER) Wrapper<SysDept> queryWrapper);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据角色ID查询部门树信息
|
* 根据角色ID查询部门树信息
|
||||||
*
|
*
|
||||||
|
|||||||
@ -0,0 +1,24 @@
|
|||||||
|
package com.ruoyi.system.service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通用 数据权限 服务
|
||||||
|
*
|
||||||
|
* @author Lion Li
|
||||||
|
*/
|
||||||
|
public interface SysDataScopeService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取角色自定义权限
|
||||||
|
* @param roleId 角色id
|
||||||
|
* @return 部门id组
|
||||||
|
*/
|
||||||
|
String getRoleCustom(Long roleId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取部门及以下权限
|
||||||
|
* @param deptId 部门id
|
||||||
|
* @return 部门id组
|
||||||
|
*/
|
||||||
|
String getDeptAndChild(Long deptId);
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,49 @@
|
|||||||
|
package com.ruoyi.system.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.ruoyi.common.core.domain.entity.SysDept;
|
||||||
|
import com.ruoyi.system.domain.SysRoleDept;
|
||||||
|
import com.ruoyi.system.mapper.SysDeptMapper;
|
||||||
|
import com.ruoyi.system.mapper.SysRoleDeptMapper;
|
||||||
|
import com.ruoyi.system.service.SysDataScopeService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service("sdss")
|
||||||
|
public class SysDataScopeServiceImpl implements SysDataScopeService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SysRoleDeptMapper roleDeptMapper;
|
||||||
|
@Autowired
|
||||||
|
private SysDeptMapper deptMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getRoleCustom(Long roleId) {
|
||||||
|
List<SysRoleDept> list = roleDeptMapper.selectList(
|
||||||
|
new LambdaQueryWrapper<SysRoleDept>()
|
||||||
|
.select(SysRoleDept::getDeptId)
|
||||||
|
.eq(SysRoleDept::getRoleId, roleId));
|
||||||
|
if (CollUtil.isNotEmpty(list)) {
|
||||||
|
return StrUtil.join(",", list);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDeptAndChild(Long deptId) {
|
||||||
|
List<SysDept> list = deptMapper.selectList(new LambdaQueryWrapper<SysDept>()
|
||||||
|
.select(SysDept::getDeptId)
|
||||||
|
.eq(SysDept::getDeptId, deptId)
|
||||||
|
.or()
|
||||||
|
.apply("find_in_set({0},ancestors)", deptId));
|
||||||
|
if (CollUtil.isNotEmpty(list)) {
|
||||||
|
return StrUtil.join(",", list);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user