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
3e381671a4
commit
22e1bc19f0
@ -0,0 +1,19 @@
|
||||
package com.ruoyi.common.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 数据权限
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface DataColumn {
|
||||
|
||||
String key();
|
||||
|
||||
String value();
|
||||
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package com.ruoyi.common.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 数据权限组
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface DataPermission {
|
||||
|
||||
DataColumn[] value();
|
||||
|
||||
}
|
||||
@ -4,7 +4,8 @@ import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.ruoyi.common.annotation.DataScope;
|
||||
import com.ruoyi.common.annotation.DataColumn;
|
||||
import com.ruoyi.common.annotation.DataPermission;
|
||||
import com.ruoyi.common.core.mybatisplus.core.BaseMapperPlus;
|
||||
import com.ruoyi.demo.domain.TestDemo;
|
||||
import com.ruoyi.demo.domain.vo.TestDemoVo;
|
||||
@ -20,14 +21,23 @@ import java.util.List;
|
||||
*/
|
||||
public interface TestDemoMapper extends BaseMapperPlus<TestDemo> {
|
||||
|
||||
@DataScope(deptName = "dept_id", userName = "user_id")
|
||||
@DataPermission({
|
||||
@DataColumn(key = "deptName", value = "dept_id"),
|
||||
@DataColumn(key = "userName", value = "user_id")
|
||||
})
|
||||
Page<TestDemoVo> customPageList(@Param("page") Page<TestDemo> page, @Param("ew") Wrapper<TestDemo> wrapper);
|
||||
|
||||
@Override
|
||||
@DataScope(deptName = "dept_id", userName = "user_id")
|
||||
@DataPermission({
|
||||
@DataColumn(key = "deptName", value = "dept_id"),
|
||||
@DataColumn(key = "userName", value = "user_id")
|
||||
})
|
||||
<P extends IPage<TestDemo>> P selectPage(P page, @Param(Constants.WRAPPER) Wrapper<TestDemo> queryWrapper);
|
||||
|
||||
@Override
|
||||
@DataScope(deptName = "dept_id", userName = "user_id")
|
||||
@DataPermission({
|
||||
@DataColumn(key = "deptName", value = "dept_id"),
|
||||
@DataColumn(key = "userName", value = "user_id")
|
||||
})
|
||||
List<TestDemo> selectList(@Param(Constants.WRAPPER) Wrapper<TestDemo> queryWrapper);
|
||||
}
|
||||
|
||||
@ -2,7 +2,8 @@ package com.ruoyi.demo.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||
import com.ruoyi.common.annotation.DataScope;
|
||||
import com.ruoyi.common.annotation.DataColumn;
|
||||
import com.ruoyi.common.annotation.DataPermission;
|
||||
import com.ruoyi.common.core.mybatisplus.core.BaseMapperPlus;
|
||||
import com.ruoyi.demo.domain.TestTree;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
@ -18,6 +19,9 @@ import java.util.List;
|
||||
public interface TestTreeMapper extends BaseMapperPlus<TestTree> {
|
||||
|
||||
@Override
|
||||
@DataScope(deptName = "dept_id", userName = "user_id")
|
||||
@DataPermission({
|
||||
@DataColumn(key = "deptName", value = "dept_id"),
|
||||
@DataColumn(key = "userName", value = "user_id")
|
||||
})
|
||||
List<TestTree> selectList(@Param(Constants.WRAPPER) Wrapper<TestTree> queryWrapper);
|
||||
}
|
||||
|
||||
@ -59,14 +59,11 @@ public class TestDemoServiceImpl extends ServicePlusImpl<TestDemoMapper, TestDem
|
||||
|
||||
private LambdaQueryWrapper<TestDemo> buildQueryWrapper(TestDemoBo bo) {
|
||||
Map<String, Object> params = bo.getParams();
|
||||
Object dataScope = params.get("dataScope");
|
||||
LambdaQueryWrapper<TestDemo> lqw = Wrappers.lambdaQuery();
|
||||
lqw.like(StringUtils.isNotBlank(bo.getTestKey()), TestDemo::getTestKey, bo.getTestKey());
|
||||
lqw.eq(StringUtils.isNotBlank(bo.getValue()), TestDemo::getValue, bo.getValue());
|
||||
lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
|
||||
TestDemo::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
|
||||
lqw.apply(dataScope != null && StringUtils.isNotBlank(dataScope.toString()),
|
||||
dataScope != null ? dataScope.toString() : null);
|
||||
return lqw;
|
||||
}
|
||||
|
||||
|
||||
@ -40,13 +40,10 @@ public class TestTreeServiceImpl extends ServicePlusImpl<TestTreeMapper, TestTre
|
||||
|
||||
private LambdaQueryWrapper<TestTree> buildQueryWrapper(TestTreeBo bo) {
|
||||
Map<String, Object> params = bo.getParams();
|
||||
Object dataScope = params.get("dataScope");
|
||||
LambdaQueryWrapper<TestTree> lqw = Wrappers.lambdaQuery();
|
||||
lqw.like(StringUtils.isNotBlank(bo.getTreeName()), TestTree::getTreeName, bo.getTreeName());
|
||||
lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
|
||||
TestTree::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
|
||||
lqw.apply(dataScope != null && StringUtils.isNotBlank(dataScope.toString()),
|
||||
dataScope != null ? dataScope.toString() : null);
|
||||
return lqw;
|
||||
}
|
||||
|
||||
|
||||
@ -1,10 +1,12 @@
|
||||
package com.ruoyi.framework.handler;
|
||||
|
||||
import cn.hutool.core.annotation.AnnotationUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.baomidou.mybatisplus.extension.plugins.handler.DataPermissionHandler;
|
||||
import com.ruoyi.common.annotation.DataScope;
|
||||
import com.ruoyi.common.annotation.DataColumn;
|
||||
import com.ruoyi.common.annotation.DataPermission;
|
||||
import com.ruoyi.common.core.domain.entity.SysRole;
|
||||
import com.ruoyi.common.core.domain.entity.SysUser;
|
||||
import com.ruoyi.common.core.service.UserService;
|
||||
@ -26,7 +28,6 @@ import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -38,76 +39,64 @@ import java.util.stream.Collectors;
|
||||
@Slf4j
|
||||
public class PlusDataPermissionHandler implements DataPermissionHandler {
|
||||
|
||||
private final ExpressionParser parser = new SpelExpressionParser();
|
||||
private final TemplateParserContext parserContext = new TemplateParserContext();
|
||||
|
||||
@Override
|
||||
public Expression getSqlSegment(Expression where, String mappedStatementId) {
|
||||
DataScope dataScope = findDataScopeAnnotation(mappedStatementId);
|
||||
if (ObjectUtil.isNull(dataScope)) {
|
||||
DataColumn[] dataColumns = findAnnotation(mappedStatementId);
|
||||
if (ArrayUtil.isEmpty(dataColumns)) {
|
||||
return where;
|
||||
}
|
||||
SysUser currentUser = SpringUtils.getBean(UserService.class).selectUserById(SecurityUtils.getUserId());
|
||||
// 如果是超级管理员,则不过滤数据
|
||||
if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin()) {
|
||||
String dataScopeSql = dataScopeFilter(currentUser, dataScope);
|
||||
if (StringUtils.isNotBlank(dataScopeSql)) {
|
||||
try {
|
||||
Expression expression = CCJSqlParserUtil.parseExpression(dataScopeSql);
|
||||
return new AndExpression(where, expression);
|
||||
} catch (JSQLParserException e) {
|
||||
throw new ServiceException("数据权限解析异常 => " + e.getMessage());
|
||||
}
|
||||
}
|
||||
if (StringUtils.isNull(currentUser) || currentUser.isAdmin()) {
|
||||
return where;
|
||||
}
|
||||
return where;
|
||||
}
|
||||
|
||||
private DataScope findDataScopeAnnotation(String mappedStatementId) {
|
||||
StringBuilder sb = new StringBuilder(mappedStatementId);
|
||||
int index = sb.lastIndexOf(".");
|
||||
String clazzName = sb.substring(0, index);
|
||||
String methodName = sb.substring(index + 1, sb.length());
|
||||
Class<?> clazz = ClassUtil.loadClass(clazzName);
|
||||
List<Method> methods = Arrays.stream(ClassUtil.getDeclaredMethods(clazz))
|
||||
.filter(method -> method.getName().equals(methodName)).collect(Collectors.toList());
|
||||
DataScope dataScope = null;
|
||||
for (Method method : methods) {
|
||||
if (AnnotationUtil.hasAnnotation(method, DataScope.class)) {
|
||||
dataScope = AnnotationUtil.getAnnotation(method, DataScope.class);
|
||||
break;
|
||||
}
|
||||
String dataFilterSql = buildDataFilter(currentUser, dataColumns);
|
||||
if (StringUtils.isBlank(dataFilterSql)) {
|
||||
return where;
|
||||
}
|
||||
try {
|
||||
Expression expression = CCJSqlParserUtil.parseExpression(dataFilterSql);
|
||||
if (ObjectUtil.isNotNull(where)) {
|
||||
return new AndExpression(where, expression);
|
||||
} else {
|
||||
return expression;
|
||||
}
|
||||
} catch (JSQLParserException e) {
|
||||
throw new ServiceException("数据权限解析异常 => " + e.getMessage());
|
||||
}
|
||||
return dataScope;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据范围过滤
|
||||
*
|
||||
* @param user 用户
|
||||
* 构造数据过滤sql
|
||||
*/
|
||||
public static String dataScopeFilter(SysUser user, DataScope annotation) {
|
||||
private String buildDataFilter(SysUser user, DataColumn[] dataColumns) {
|
||||
StringBuilder sqlString = new StringBuilder();
|
||||
|
||||
ExpressionParser parser = new SpelExpressionParser();
|
||||
TemplateParserContext parserContext = new TemplateParserContext();
|
||||
EvaluationContext context = new StandardEvaluationContext(new HashMap<>());
|
||||
context.setVariable("deptName", annotation.deptName());
|
||||
context.setVariable("userName", annotation.userName());
|
||||
EvaluationContext context = new StandardEvaluationContext();
|
||||
context.setVariable("userId", user.getUserId());
|
||||
context.setVariable("deptId", user.getDeptId());
|
||||
|
||||
|
||||
for (SysRole role : user.getRoles()) {
|
||||
context.setVariable("roleId", role.getRoleId());
|
||||
DataScopeType type = DataScopeType.findCode(role.getDataScope());
|
||||
if (ObjectUtil.isNull(type)) {
|
||||
throw new ServiceException("角色数据范围异常 => " + role.getDataScope());
|
||||
for (DataColumn dataColumn : dataColumns) {
|
||||
// 设置注解变量 key 为表达式变量 value 为变量值
|
||||
context.setVariable(dataColumn.key(), dataColumn.value());
|
||||
for (SysRole role : user.getRoles()) {
|
||||
context.setVariable("roleId", role.getRoleId());
|
||||
// 获取角色权限泛型
|
||||
DataScopeType type = DataScopeType.findCode(role.getDataScope());
|
||||
if (ObjectUtil.isNull(type)) {
|
||||
throw new ServiceException("角色数据范围异常 => " + role.getDataScope());
|
||||
}
|
||||
// 全部数据权限直接返回
|
||||
if (type == DataScopeType.DATA_SCOPE_ALL) {
|
||||
return "";
|
||||
}
|
||||
// 解析sql模板并填充
|
||||
String sql = parser.parseExpression(type.getSql(), parserContext).getValue(context, String.class);
|
||||
sqlString.append(sql);
|
||||
}
|
||||
if (type == DataScopeType.DATA_SCOPE_ALL) {
|
||||
sqlString = new StringBuilder();
|
||||
break;
|
||||
}
|
||||
org.springframework.expression.Expression expression = parser.parseExpression(type.getSql(), parserContext);
|
||||
String sql = expression.getValue(context, String.class);
|
||||
sqlString.append(sql);
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(sqlString.toString())) {
|
||||
@ -115,4 +104,22 @@ public class PlusDataPermissionHandler implements DataPermissionHandler {
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private DataColumn[] findAnnotation(String mappedStatementId) {
|
||||
StringBuilder sb = new StringBuilder(mappedStatementId);
|
||||
int index = sb.lastIndexOf(".");
|
||||
String clazzName = sb.substring(0, index);
|
||||
String methodName = sb.substring(index + 1, sb.length());
|
||||
Class<?> clazz = ClassUtil.loadClass(clazzName);
|
||||
List<Method> methods = Arrays.stream(ClassUtil.getDeclaredMethods(clazz))
|
||||
.filter(method -> method.getName().equals(methodName)).collect(Collectors.toList());
|
||||
DataPermission dataPermission;
|
||||
for (Method method : methods) {
|
||||
if (AnnotationUtil.hasAnnotation(method, DataPermission.class)) {
|
||||
dataPermission = AnnotationUtil.getAnnotation(method, DataPermission.class);
|
||||
return dataPermission.value();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,7 +2,8 @@ package com.ruoyi.system.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||
import com.ruoyi.common.annotation.DataScope;
|
||||
import com.ruoyi.common.annotation.DataColumn;
|
||||
import com.ruoyi.common.annotation.DataPermission;
|
||||
import com.ruoyi.common.core.domain.entity.SysDept;
|
||||
import com.ruoyi.common.core.mybatisplus.core.BaseMapperPlus;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
@ -22,11 +23,15 @@ public interface SysDeptMapper extends BaseMapperPlus<SysDept> {
|
||||
* @param dept 部门信息
|
||||
* @return 部门信息集合
|
||||
*/
|
||||
@DataScope(deptName = "d.dept_id")
|
||||
@DataPermission({
|
||||
@DataColumn(key = "deptName", value = "d.dept_id")
|
||||
})
|
||||
List<SysDept> selectDeptList(SysDept dept);
|
||||
|
||||
@Override
|
||||
@DataScope(deptName = "dept_id")
|
||||
@DataPermission({
|
||||
@DataColumn(key = "deptName", value = "dept_id")
|
||||
})
|
||||
List<SysDept> selectList(@Param(Constants.WRAPPER) Wrapper<SysDept> queryWrapper);
|
||||
|
||||
/**
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
package com.ruoyi.system.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.ruoyi.common.annotation.DataScope;
|
||||
import com.ruoyi.common.annotation.DataColumn;
|
||||
import com.ruoyi.common.annotation.DataPermission;
|
||||
import com.ruoyi.common.core.domain.entity.SysRole;
|
||||
import com.ruoyi.common.core.mybatisplus.core.BaseMapperPlus;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
@ -15,7 +16,9 @@ import java.util.List;
|
||||
*/
|
||||
public interface SysRoleMapper extends BaseMapperPlus<SysRole> {
|
||||
|
||||
@DataScope(deptName = "d.dept_id")
|
||||
@DataPermission({
|
||||
@DataColumn(key = "deptName", value = "d.dept_id")
|
||||
})
|
||||
Page<SysRole> selectPageRoleList(@Param("page") Page<SysRole> page, @Param("role") SysRole role);
|
||||
|
||||
/**
|
||||
@ -24,7 +27,9 @@ public interface SysRoleMapper extends BaseMapperPlus<SysRole> {
|
||||
* @param role 角色信息
|
||||
* @return 角色数据集合信息
|
||||
*/
|
||||
@DataScope(deptName = "d.dept_id")
|
||||
@DataPermission({
|
||||
@DataColumn(key = "deptName", value = "d.dept_id")
|
||||
})
|
||||
List<SysRole> selectRoleList(SysRole role);
|
||||
|
||||
/**
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
package com.ruoyi.system.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.ruoyi.common.annotation.DataScope;
|
||||
import com.ruoyi.common.annotation.DataColumn;
|
||||
import com.ruoyi.common.annotation.DataPermission;
|
||||
import com.ruoyi.common.core.domain.entity.SysUser;
|
||||
import com.ruoyi.common.core.mybatisplus.core.BaseMapperPlus;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
@ -15,7 +16,10 @@ import java.util.List;
|
||||
*/
|
||||
public interface SysUserMapper extends BaseMapperPlus<SysUser> {
|
||||
|
||||
@DataScope(deptName = "d.dept_id", userName = "u.user_id")
|
||||
@DataPermission({
|
||||
@DataColumn(key = "deptName", value = "d.dept_id"),
|
||||
@DataColumn(key = "userName", value = "u.user_id")
|
||||
})
|
||||
Page<SysUser> selectPageUserList(@Param("page") Page<SysUser> page, @Param("user") SysUser user);
|
||||
|
||||
/**
|
||||
@ -24,7 +28,10 @@ public interface SysUserMapper extends BaseMapperPlus<SysUser> {
|
||||
* @param sysUser 用户信息
|
||||
* @return 用户信息集合信息
|
||||
*/
|
||||
@DataScope(deptName = "d.dept_id", userName = "u.user_id")
|
||||
@DataPermission({
|
||||
@DataColumn(key = "deptName", value = "d.dept_id"),
|
||||
@DataColumn(key = "userName", value = "u.user_id")
|
||||
})
|
||||
List<SysUser> selectUserList(SysUser sysUser);
|
||||
|
||||
/**
|
||||
@ -33,7 +40,10 @@ public interface SysUserMapper extends BaseMapperPlus<SysUser> {
|
||||
* @param user 用户信息
|
||||
* @return 用户信息集合信息
|
||||
*/
|
||||
@DataScope(deptName = "d.dept_id", userName = "u.user_id")
|
||||
@DataPermission({
|
||||
@DataColumn(key = "deptName", value = "d.dept_id"),
|
||||
@DataColumn(key = "userName", value = "u.user_id")
|
||||
})
|
||||
Page<SysUser> selectAllocatedList(@Param("page") Page<SysUser> page, @Param("user") SysUser user);
|
||||
|
||||
/**
|
||||
@ -42,7 +52,10 @@ public interface SysUserMapper extends BaseMapperPlus<SysUser> {
|
||||
* @param user 用户信息
|
||||
* @return 用户信息集合信息
|
||||
*/
|
||||
@DataScope(deptName = "d.dept_id", userName = "u.user_id")
|
||||
@DataPermission({
|
||||
@DataColumn(key = "deptName", value = "d.dept_id"),
|
||||
@DataColumn(key = "userName", value = "u.user_id")
|
||||
})
|
||||
Page<SysUser> selectUnallocatedList(@Param("page") Page<SysUser> page, @Param("user") SysUser user);
|
||||
|
||||
/**
|
||||
|
||||
@ -1,16 +1,15 @@
|
||||
package com.ruoyi.system.service.impl;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.ruoyi.common.annotation.DataSource;
|
||||
import com.ruoyi.common.constant.Constants;
|
||||
import com.ruoyi.common.constant.UserConstants;
|
||||
import com.ruoyi.common.core.domain.PageQuery;
|
||||
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
|
||||
import com.ruoyi.common.core.page.TableDataInfo;
|
||||
import com.ruoyi.common.core.service.ConfigService;
|
||||
import com.ruoyi.common.enums.DataSourceType;
|
||||
import com.ruoyi.common.exception.ServiceException;
|
||||
import com.ruoyi.common.utils.PageUtils;
|
||||
import com.ruoyi.common.utils.RedisUtils;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user