|
@@ -1,6 +1,5 @@
|
|
|
package org.dromara.common.mybatis.handler;
|
|
|
|
|
|
-import cn.hutool.core.annotation.AnnotationUtil;
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -9,7 +8,6 @@ import net.sf.jsqlparser.expression.Expression;
|
|
|
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
|
|
|
import net.sf.jsqlparser.expression.operators.relational.ParenthesedExpressionList;
|
|
|
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
|
|
-import org.apache.ibatis.io.Resources;
|
|
|
import org.dromara.common.core.domain.dto.RoleDTO;
|
|
|
import org.dromara.common.core.domain.model.LoginUser;
|
|
|
import org.dromara.common.core.exception.ServiceException;
|
|
@@ -21,27 +19,17 @@ import org.dromara.common.mybatis.annotation.DataPermission;
|
|
|
import org.dromara.common.mybatis.enums.DataScopeType;
|
|
|
import org.dromara.common.mybatis.helper.DataPermissionHelper;
|
|
|
import org.dromara.common.satoken.utils.LoginHelper;
|
|
|
-import org.springframework.context.ConfigurableApplicationContext;
|
|
|
import org.springframework.context.expression.BeanFactoryResolver;
|
|
|
-import org.springframework.core.io.Resource;
|
|
|
-import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
|
|
-import org.springframework.core.io.support.ResourcePatternResolver;
|
|
|
-import org.springframework.core.type.ClassMetadata;
|
|
|
-import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
|
|
|
import org.springframework.expression.BeanResolver;
|
|
|
import org.springframework.expression.ExpressionParser;
|
|
|
import org.springframework.expression.ParserContext;
|
|
|
import org.springframework.expression.common.TemplateParserContext;
|
|
|
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
|
|
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
|
|
-import org.springframework.util.ClassUtils;
|
|
|
|
|
|
-import java.lang.reflect.Method;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.HashSet;
|
|
|
-import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
-import java.util.concurrent.ConcurrentHashMap;
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
/**
|
|
@@ -53,11 +41,6 @@ import java.util.function.Function;
|
|
|
@Slf4j
|
|
|
public class PlusDataPermissionHandler {
|
|
|
|
|
|
- /**
|
|
|
- * 方法或类(名称) 与 注解的映射关系缓存
|
|
|
- */
|
|
|
- private final Map<String, DataPermission> dataPermissionCacheMap = new ConcurrentHashMap<>();
|
|
|
-
|
|
|
/**
|
|
|
* spel 解析器
|
|
|
*/
|
|
@@ -68,15 +51,6 @@ public class PlusDataPermissionHandler {
|
|
|
*/
|
|
|
private final BeanResolver beanResolver = new BeanFactoryResolver(SpringUtils.getBeanFactory());
|
|
|
|
|
|
- /**
|
|
|
- * 构造方法,扫描指定包下的 Mapper 类并初始化缓存
|
|
|
- *
|
|
|
- * @param mapperPackage Mapper 类所在的包路径
|
|
|
- */
|
|
|
- public PlusDataPermissionHandler(String mapperPackage) {
|
|
|
- scanMapperClasses(mapperPackage);
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 获取数据过滤条件的 SQL 片段
|
|
|
*
|
|
@@ -86,24 +60,24 @@ public class PlusDataPermissionHandler {
|
|
|
* @return 数据过滤条件的 SQL 片段
|
|
|
*/
|
|
|
public Expression getSqlSegment(Expression where, String mappedStatementId, boolean isSelect) {
|
|
|
- // 获取数据权限配置
|
|
|
- DataPermission dataPermission = getDataPermission(mappedStatementId);
|
|
|
- // 获取当前登录用户信息
|
|
|
- LoginUser currentUser = DataPermissionHelper.getVariable("user");
|
|
|
- if (ObjectUtil.isNull(currentUser)) {
|
|
|
- currentUser = LoginHelper.getLoginUser();
|
|
|
- DataPermissionHelper.setVariable("user", currentUser);
|
|
|
- }
|
|
|
- // 如果是超级管理员或租户管理员,则不过滤数据
|
|
|
- if (LoginHelper.isSuperAdmin() || LoginHelper.isTenantAdmin()) {
|
|
|
- return where;
|
|
|
- }
|
|
|
- // 构造数据过滤条件的 SQL 片段
|
|
|
- String dataFilterSql = buildDataFilter(dataPermission, isSelect);
|
|
|
- if (StringUtils.isBlank(dataFilterSql)) {
|
|
|
- return where;
|
|
|
- }
|
|
|
try {
|
|
|
+ // 获取数据权限配置
|
|
|
+ DataPermission dataPermission = DataPermissionHelper.getPermission();
|
|
|
+ // 获取当前登录用户信息
|
|
|
+ LoginUser currentUser = DataPermissionHelper.getVariable("user");
|
|
|
+ if (ObjectUtil.isNull(currentUser)) {
|
|
|
+ currentUser = LoginHelper.getLoginUser();
|
|
|
+ DataPermissionHelper.setVariable("user", currentUser);
|
|
|
+ }
|
|
|
+ // 如果是超级管理员或租户管理员,则不过滤数据
|
|
|
+ if (LoginHelper.isSuperAdmin() || LoginHelper.isTenantAdmin()) {
|
|
|
+ return where;
|
|
|
+ }
|
|
|
+ // 构造数据过滤条件的 SQL 片段
|
|
|
+ String dataFilterSql = buildDataFilter(dataPermission, isSelect);
|
|
|
+ if (StringUtils.isBlank(dataFilterSql)) {
|
|
|
+ return where;
|
|
|
+ }
|
|
|
Expression expression = CCJSqlParserUtil.parseExpression(dataFilterSql);
|
|
|
// 数据权限使用单独的括号 防止与其他条件冲突
|
|
|
ParenthesedExpressionList<Expression> parenthesis = new ParenthesedExpressionList<>(expression);
|
|
@@ -114,6 +88,8 @@ public class PlusDataPermissionHandler {
|
|
|
}
|
|
|
} catch (JSQLParserException e) {
|
|
|
throw new ServiceException("数据权限解析异常 => " + e.getMessage());
|
|
|
+ } finally {
|
|
|
+ DataPermissionHelper.removePermission();
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -170,8 +146,11 @@ public class PlusDataPermissionHandler {
|
|
|
context.setVariable(dataColumn.key()[i], dataColumn.value()[i]);
|
|
|
}
|
|
|
|
|
|
+ // 忽略数据权限 防止spel表达式内有其他sql查询导致死循环调用
|
|
|
+ String sql = DataPermissionHelper.ignore(() ->
|
|
|
+ parser.parseExpression(type.getSqlTemplate(), parserContext).getValue(context, String.class)
|
|
|
+ );
|
|
|
// 解析sql模板并填充
|
|
|
- String sql = parser.parseExpression(type.getSqlTemplate(), parserContext).getValue(context, String.class);
|
|
|
conditions.add(joinStr + sql);
|
|
|
isSuccess = true;
|
|
|
}
|
|
@@ -188,87 +167,12 @@ public class PlusDataPermissionHandler {
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 扫描指定包下的 Mapper 类,并查找其中带有特定注解的方法或类
|
|
|
- *
|
|
|
- * @param mapperPackage Mapper 类所在的包路径
|
|
|
- */
|
|
|
- private void scanMapperClasses(String mapperPackage) {
|
|
|
- // 创建资源解析器和元数据读取工厂
|
|
|
- PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
|
|
- CachingMetadataReaderFactory factory = new CachingMetadataReaderFactory();
|
|
|
- // 将 Mapper 包路径按分隔符拆分为数组
|
|
|
- String[] packagePatternArray = StringUtils.splitPreserveAllTokens(mapperPackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
|
|
|
- String classpath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX;
|
|
|
- try {
|
|
|
- for (String packagePattern : packagePatternArray) {
|
|
|
- // 将包路径转换为资源路径
|
|
|
- String path = ClassUtils.convertClassNameToResourcePath(packagePattern);
|
|
|
- // 获取指定路径下的所有 .class 文件资源
|
|
|
- Resource[] resources = resolver.getResources(classpath + path + "/*.class");
|
|
|
- for (Resource resource : resources) {
|
|
|
- // 获取资源的类元数据
|
|
|
- ClassMetadata classMetadata = factory.getMetadataReader(resource).getClassMetadata();
|
|
|
- // 获取资源对应的类对象
|
|
|
- Class<?> clazz = Resources.classForName(classMetadata.getClassName());
|
|
|
- // 查找类中的特定注解
|
|
|
- findAnnotation(clazz);
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("初始化数据安全缓存时出错:{}", e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 在指定的类中查找特定的注解 DataPermission,并将带有这个注解的方法或类存储到 dataPermissionCacheMap 中
|
|
|
- *
|
|
|
- * @param clazz 要查找的类
|
|
|
- */
|
|
|
- private void findAnnotation(Class<?> clazz) {
|
|
|
- DataPermission dataPermission;
|
|
|
- for (Method method : clazz.getMethods()) {
|
|
|
- if (method.isDefault() || method.isVarArgs()) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- String mappedStatementId = clazz.getName() + "." + method.getName();
|
|
|
- if (AnnotationUtil.hasAnnotation(method, DataPermission.class)) {
|
|
|
- dataPermission = AnnotationUtil.getAnnotation(method, DataPermission.class);
|
|
|
- dataPermissionCacheMap.put(mappedStatementId, dataPermission);
|
|
|
- }
|
|
|
- }
|
|
|
- if (AnnotationUtil.hasAnnotation(clazz, DataPermission.class)) {
|
|
|
- dataPermission = AnnotationUtil.getAnnotation(clazz, DataPermission.class);
|
|
|
- dataPermissionCacheMap.put(clazz.getName(), dataPermission);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据映射语句 ID 或类名获取对应的 DataPermission 注解对象
|
|
|
- *
|
|
|
- * @param mapperId 映射语句 ID
|
|
|
- * @return DataPermission 注解对象,如果不存在则返回 null
|
|
|
- */
|
|
|
- public DataPermission getDataPermission(String mapperId) {
|
|
|
- // 检查缓存中是否包含映射语句 ID 对应的 DataPermission 注解对象
|
|
|
- if (dataPermissionCacheMap.containsKey(mapperId)) {
|
|
|
- return dataPermissionCacheMap.get(mapperId);
|
|
|
- }
|
|
|
- // 如果缓存中不包含映射语句 ID 对应的 DataPermission 注解对象,则尝试使用类名作为键查找
|
|
|
- String clazzName = mapperId.substring(0, mapperId.lastIndexOf("."));
|
|
|
- if (dataPermissionCacheMap.containsKey(clazzName)) {
|
|
|
- return dataPermissionCacheMap.get(clazzName);
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 检查给定的映射语句 ID 是否有效,即是否能够找到对应的 DataPermission 注解对象
|
|
|
*
|
|
|
- * @param mapperId 映射语句 ID
|
|
|
* @return 如果找到对应的 DataPermission 注解对象,则返回 false;否则返回 true
|
|
|
*/
|
|
|
- public boolean invalid(String mapperId) {
|
|
|
- return getDataPermission(mapperId) == null;
|
|
|
+ public boolean invalid() {
|
|
|
+ return DataPermissionHelper.getPermission() == null;
|
|
|
}
|
|
|
}
|