|
@@ -10,11 +10,11 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import net.sf.jsqlparser.expression.Alias;
|
|
|
import net.sf.jsqlparser.expression.Expression;
|
|
|
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
|
|
|
-import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
|
|
|
-import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
|
|
|
import net.sf.jsqlparser.schema.Table;
|
|
|
|
|
|
import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 基于 {@link DataPermissionRule} 的数据权限处理器
|
|
@@ -39,16 +39,9 @@ public class DataPermissionRuleHandler implements MultiDataPermissionHandler {
|
|
|
// 获得 Mapper 对应的数据权限的规则
|
|
|
List<DataPermissionRule> rules = ruleFactory.getDataPermissionRule(mappedStatementId);
|
|
|
|
|
|
- // 调试日志
|
|
|
- // if (log.isDebugEnabled()) {
|
|
|
- // String tableName = MyBatisUtils.getTableName(table);
|
|
|
- // log.debug("[DataPermission] MappedStatement: {}, Table: {}, Rules count: {}",
|
|
|
- // mappedStatementId, tableName, rules.size());
|
|
|
-
|
|
|
- for (int i = 0; i < rules.size(); i++) {
|
|
|
- log.debug("[DataPermission] Rule {}: {}", i + 1, rules.get(i).getClass().getSimpleName());
|
|
|
- }
|
|
|
- // }
|
|
|
+ for (int i = 0; i < rules.size(); i++) {
|
|
|
+ log.debug("[DataPermission] Rule {}: {}", i + 1, rules.get(i).getClass().getSimpleName());
|
|
|
+ }
|
|
|
|
|
|
if (CollUtil.isEmpty(rules)) {
|
|
|
return where;
|
|
@@ -57,6 +50,15 @@ public class DataPermissionRuleHandler implements MultiDataPermissionHandler {
|
|
|
// 2. 生成条件表达式
|
|
|
String tableName = MyBatisUtils.getTableName(table);
|
|
|
|
|
|
+ // 关键修复:仅处理有权限规则的表
|
|
|
+ Set<String> allRuleTables = rules.stream()
|
|
|
+ .flatMap(rule -> rule.getTableNames().stream())
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+
|
|
|
+ if (!allRuleTables.contains(tableName)) {
|
|
|
+ return where; // 跳过无权限规则的表
|
|
|
+ }
|
|
|
+
|
|
|
log.debug("[DataPermission] 开始处理数据权限: MappedStatement={}, Table={}", mappedStatementId, tableName);
|
|
|
|
|
|
Expression permissionExpression = buildPermissionExpression(tableName, table.getAlias(), rules);
|
|
@@ -66,16 +68,9 @@ public class DataPermissionRuleHandler implements MultiDataPermissionHandler {
|
|
|
return where;
|
|
|
}
|
|
|
|
|
|
- // 避免重复添加逻辑删除条件
|
|
|
- if (where != null) {
|
|
|
- log.debug("[DataPermission] 原始 WHERE 条件: {}", where.toString());
|
|
|
- // 检查原始条件是否已包含逻辑删除条件(deleted = 0)
|
|
|
- if (containsDeletedCondition(where)) {
|
|
|
- // 只添加权限表达式,避免重复
|
|
|
- return new AndExpression(where, permissionExpression);
|
|
|
- }
|
|
|
- } else {
|
|
|
- log.debug("[DataPermission] 原始 WHERE 条件为空");
|
|
|
+ // 直接组合条件(不再特殊处理逻辑删除)
|
|
|
+ if (where == null) {
|
|
|
+ return permissionExpression;
|
|
|
}
|
|
|
|
|
|
log.debug("[DataPermission] 权限表达式: {}", permissionExpression.toString());
|
|
@@ -87,30 +82,6 @@ public class DataPermissionRuleHandler implements MultiDataPermissionHandler {
|
|
|
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 检查表达式是否包含逻辑删除条件(deleted = 0)
|
|
|
- */
|
|
|
- private boolean containsDeletedCondition(Expression expression) {
|
|
|
- if (expression instanceof EqualsTo) {
|
|
|
- EqualsTo equalsTo = (EqualsTo) expression;
|
|
|
- String left = equalsTo.getLeftExpression().toString();
|
|
|
- String right = equalsTo.getRightExpression().toString();
|
|
|
- // 更宽松的匹配逻辑
|
|
|
- if (left.toLowerCase().contains("deleted") && "0".equals(right)) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- } else if (expression instanceof AndExpression) {
|
|
|
- AndExpression and = (AndExpression) expression;
|
|
|
- return containsDeletedCondition(and.getLeftExpression()) ||
|
|
|
- containsDeletedCondition(and.getRightExpression());
|
|
|
- } else if (expression instanceof OrExpression) {
|
|
|
- OrExpression or = (OrExpression) expression;
|
|
|
- return containsDeletedCondition(or.getLeftExpression()) ||
|
|
|
- containsDeletedCondition(or.getRightExpression());
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
private Expression buildPermissionExpression(String tableName, Alias tableAlias,
|
|
|
List<DataPermissionRule> rules) {
|
|
|
Expression result = null;
|
|
@@ -129,14 +100,7 @@ public class DataPermissionRuleHandler implements MultiDataPermissionHandler {
|
|
|
if (result == null) {
|
|
|
result = expression;
|
|
|
} else {
|
|
|
- // 处理组合规则:保持其 OR 关系
|
|
|
- if (expression instanceof OrExpression) {
|
|
|
- // 如果当前规则是组合规则,直接与之前结果 AND 连接
|
|
|
- result = new AndExpression(result, expression);
|
|
|
- } else {
|
|
|
- // 普通规则使用 AND 连接
|
|
|
- result = new AndExpression(result, expression);
|
|
|
- }
|
|
|
+ result = new AndExpression(result, expression);
|
|
|
}
|
|
|
}
|
|
|
|