|
@@ -11,6 +11,7 @@ import net.sf.jsqlparser.expression.Alias;
|
|
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.expression.operators.conditional.OrExpression;
|
|
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
|
|
|
|
+import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
|
|
import net.sf.jsqlparser.schema.Table;
|
|
import net.sf.jsqlparser.schema.Table;
|
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
@@ -31,6 +32,10 @@ public class DataPermissionRuleHandler implements MultiDataPermissionHandler {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public Expression getSqlSegment(Table table, Expression where, String mappedStatementId) {
|
|
public Expression getSqlSegment(Table table, Expression where, String mappedStatementId) {
|
|
|
|
+ // 打印当前线程和 MappedStatement 用于调试重复调用问题
|
|
|
|
+ log.debug("[DataPermission] 线程: {} 处理: {}",
|
|
|
|
+ Thread.currentThread().getName(), mappedStatementId);
|
|
|
|
+
|
|
// 获得 Mapper 对应的数据权限的规则
|
|
// 获得 Mapper 对应的数据权限的规则
|
|
List<DataPermissionRule> rules = ruleFactory.getDataPermissionRule(mappedStatementId);
|
|
List<DataPermissionRule> rules = ruleFactory.getDataPermissionRule(mappedStatementId);
|
|
|
|
|
|
@@ -52,8 +57,7 @@ public class DataPermissionRuleHandler implements MultiDataPermissionHandler {
|
|
// 2. 生成条件表达式
|
|
// 2. 生成条件表达式
|
|
String tableName = MyBatisUtils.getTableName(table);
|
|
String tableName = MyBatisUtils.getTableName(table);
|
|
|
|
|
|
- log.debug("[DataPermission] MappedStatement: {}, Table: {}, Rules count: {}",
|
|
|
|
- mappedStatementId, tableName, rules.size());
|
|
|
|
|
|
+ log.debug("[DataPermission] 开始处理数据权限: MappedStatement={}, Table={}", mappedStatementId, tableName);
|
|
|
|
|
|
Expression permissionExpression = buildPermissionExpression(tableName, table.getAlias(), rules);
|
|
Expression permissionExpression = buildPermissionExpression(tableName, table.getAlias(), rules);
|
|
|
|
|
|
@@ -62,9 +66,49 @@ public class DataPermissionRuleHandler implements MultiDataPermissionHandler {
|
|
return where;
|
|
return where;
|
|
}
|
|
}
|
|
|
|
|
|
- return where == null ? permissionExpression
|
|
|
|
|
|
+ // 避免重复添加逻辑删除条件
|
|
|
|
+ if (where != null) {
|
|
|
|
+ log.debug("[DataPermission] 原始 WHERE 条件: {}", where.toString());
|
|
|
|
+ // 检查原始条件是否已包含逻辑删除条件(deleted = 0)
|
|
|
|
+ if (containsDeletedCondition(where)) {
|
|
|
|
+ // 只添加权限表达式,避免重复
|
|
|
|
+ return new AndExpression(where, permissionExpression);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ log.debug("[DataPermission] 原始 WHERE 条件为空");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ log.debug("[DataPermission] 权限表达式: {}", permissionExpression.toString());
|
|
|
|
+
|
|
|
|
+ Expression result = where == null ? permissionExpression
|
|
: new AndExpression(where, permissionExpression);
|
|
: new AndExpression(where, permissionExpression);
|
|
|
|
+ log.debug("[DataPermission] 最终 WHERE 条件: {}", result != null ? result.toString() : "null");
|
|
|
|
+ return result;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 检查表达式是否包含逻辑删除条件(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,
|
|
private Expression buildPermissionExpression(String tableName, Alias tableAlias,
|