| | |
| | | package org.dromara.common.mybatis.handler; |
| | | |
| | | import cn.hutool.core.annotation.AnnotationUtil; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.collection.ConcurrentHashSet; |
| | | import cn.hutool.core.util.ArrayUtil; |
| | | import cn.hutool.core.util.ClassUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import net.sf.jsqlparser.JSQLParserException; |
| | | 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.dromara.common.core.domain.dto.RoleDTO; |
| | | import org.dromara.common.core.domain.model.LoginUser; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | |
| | | import org.dromara.common.mybatis.enums.DataScopeType; |
| | | import org.dromara.common.mybatis.helper.DataPermissionHelper; |
| | | import org.dromara.common.satoken.utils.LoginHelper; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import net.sf.jsqlparser.JSQLParserException; |
| | | import net.sf.jsqlparser.expression.Expression; |
| | | import net.sf.jsqlparser.expression.Parenthesis; |
| | | import net.sf.jsqlparser.expression.operators.conditional.AndExpression; |
| | | import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| | | import org.springframework.context.expression.BeanFactoryResolver; |
| | | import org.springframework.expression.BeanResolver; |
| | | import org.springframework.expression.ExpressionParser; |
| | |
| | | import org.springframework.expression.spel.standard.SpelExpressionParser; |
| | | import org.springframework.expression.spel.support.StandardEvaluationContext; |
| | | |
| | | import java.lang.reflect.Method; |
| | | import java.util.*; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | import java.util.Arrays; |
| | | import java.util.HashSet; |
| | | import java.util.Set; |
| | | import java.util.function.Function; |
| | | |
| | | /** |
| | |
| | | public class PlusDataPermissionHandler { |
| | | |
| | | /** |
| | | * 方法或类(名称) 与 注解的映射关系缓存 |
| | | */ |
| | | private final Map<String, DataPermission> dataPermissionCacheMap = new ConcurrentHashMap<>(); |
| | | |
| | | /** |
| | | * 无效注解方法缓存用于快速返回 |
| | | */ |
| | | private final Set<String> invalidCacheSet = new ConcurrentHashSet<>(); |
| | | |
| | | /** |
| | | * spel 解析器 |
| | | */ |
| | | private final ExpressionParser parser = new SpelExpressionParser(); |
| | |
| | | */ |
| | | private final BeanResolver beanResolver = new BeanFactoryResolver(SpringUtils.getBeanFactory()); |
| | | |
| | | |
| | | /** |
| | | * 获取数据过滤条件的 SQL 片段 |
| | | * |
| | | * @param where 原始的查询条件表达式 |
| | | * @param mappedStatementId Mapper 方法的 ID |
| | | * @param isSelect 是否为查询语句 |
| | | * @return 数据过滤条件的 SQL 片段 |
| | | */ |
| | | public Expression getSqlSegment(Expression where, String mappedStatementId, boolean isSelect) { |
| | | DataColumn[] dataColumns = findAnnotation(mappedStatementId); |
| | | if (ArrayUtil.isEmpty(dataColumns)) { |
| | | invalidCacheSet.add(mappedStatementId); |
| | | return where; |
| | | } |
| | | LoginUser currentUser = DataPermissionHelper.getVariable("user"); |
| | | if (ObjectUtil.isNull(currentUser)) { |
| | | currentUser = LoginHelper.getLoginUser(); |
| | | DataPermissionHelper.setVariable("user", currentUser); |
| | | } |
| | | // 如果是超级管理员或租户管理员,则不过滤数据 |
| | | if (LoginHelper.isSuperAdmin() || LoginHelper.isTenantAdmin()) { |
| | | return where; |
| | | } |
| | | String dataFilterSql = buildDataFilter(dataColumns, 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); |
| | | // 数据权限使用单独的括号 防止与其他条件冲突 |
| | | Parenthesis parenthesis = new Parenthesis(expression); |
| | | ParenthesedExpressionList<Expression> parenthesis = new ParenthesedExpressionList<>(expression); |
| | | if (ObjectUtil.isNotNull(where)) { |
| | | return new AndExpression(where, parenthesis); |
| | | } else { |
| | |
| | | } |
| | | } catch (JSQLParserException e) { |
| | | throw new ServiceException("数据权限解析异常 => " + e.getMessage()); |
| | | } finally { |
| | | DataPermissionHelper.removePermission(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 构造数据过滤sql |
| | | * 构建数据过滤条件的 SQL 语句 |
| | | * |
| | | * @param dataPermission 数据权限注解 |
| | | * @param isSelect 标志当前操作是否为查询操作,查询操作和更新或删除操作在处理过滤条件时会有不同的处理方式 |
| | | * @return 构建的数据过滤条件的 SQL 语句 |
| | | * @throws ServiceException 如果角色的数据范围异常或者 key 与 value 的长度不匹配,则抛出 ServiceException 异常 |
| | | */ |
| | | private String buildDataFilter(DataColumn[] dataColumns, boolean isSelect) { |
| | | private String buildDataFilter(DataPermission dataPermission, boolean isSelect) { |
| | | // 更新或删除需满足所有条件 |
| | | String joinStr = isSelect ? " OR " : " AND "; |
| | | if (StringUtils.isNotBlank(dataPermission.joinStr())) { |
| | | joinStr = " " + dataPermission.joinStr() + " "; |
| | | } |
| | | LoginUser user = DataPermissionHelper.getVariable("user"); |
| | | StandardEvaluationContext context = new StandardEvaluationContext(); |
| | | context.setBeanResolver(beanResolver); |
| | |
| | | return ""; |
| | | } |
| | | boolean isSuccess = false; |
| | | for (DataColumn dataColumn : dataColumns) { |
| | | for (DataColumn dataColumn : dataPermission.value()) { |
| | | if (dataColumn.key().length != dataColumn.value().length) { |
| | | throw new ServiceException("角色数据范围异常 => key与value长度不匹配"); |
| | | } |
| | |
| | | )) { |
| | | continue; |
| | | } |
| | | // 包含权限标识符 这直接跳过 |
| | | if (StringUtils.isNotBlank(dataColumn.permission()) && |
| | | CollUtil.contains(user.getMenuPermission(), dataColumn.permission()) |
| | | ) { |
| | | isSuccess = true; |
| | | continue; |
| | | } |
| | | // 设置注解变量 key 为表达式变量 value 为变量值 |
| | | for (int i = 0; i < dataColumn.key().length; i++) { |
| | | 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; |
| | | } |
| | |
| | | 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)).toList(); |
| | | DataPermission dataPermission; |
| | | // 获取方法注解 |
| | | for (Method method : methods) { |
| | | dataPermission = dataPermissionCacheMap.get(mappedStatementId); |
| | | if (ObjectUtil.isNotNull(dataPermission)) { |
| | | return dataPermission.value(); |
| | | } |
| | | if (AnnotationUtil.hasAnnotation(method, DataPermission.class)) { |
| | | dataPermission = AnnotationUtil.getAnnotation(method, DataPermission.class); |
| | | dataPermissionCacheMap.put(mappedStatementId, dataPermission); |
| | | return dataPermission.value(); |
| | | } |
| | | } |
| | | dataPermission = dataPermissionCacheMap.get(clazz.getName()); |
| | | if (ObjectUtil.isNotNull(dataPermission)) { |
| | | return dataPermission.value(); |
| | | } |
| | | // 获取类注解 |
| | | if (AnnotationUtil.hasAnnotation(clazz, DataPermission.class)) { |
| | | dataPermission = AnnotationUtil.getAnnotation(clazz, DataPermission.class); |
| | | dataPermissionCacheMap.put(clazz.getName(), dataPermission); |
| | | return dataPermission.value(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 是否为无效方法 无数据权限 |
| | | * 检查给定的映射语句 ID 是否有效,即是否能够找到对应的 DataPermission 注解对象 |
| | | * |
| | | * @return 如果找到对应的 DataPermission 注解对象,则返回 false;否则返回 true |
| | | */ |
| | | public boolean isInvalid(String mappedStatementId) { |
| | | return invalidCacheSet.contains(mappedStatementId); |
| | | public boolean invalid() { |
| | | return DataPermissionHelper.getPermission() == null; |
| | | } |
| | | } |