refactor(dept):优化部门数据权限规则逻辑
- 引入 GreaterThan 表达式支持 FIND_IN_SET 函数判断 - 将原有的 IN 表达式替换为基于 FIND_IN_SET 的大于比较 - 支持多部门 ID 的逐个条件构建与组合-保持原有 OR 条件拼接逻辑不变 - 增强部门权限匹配的灵活性和准确性
This commit is contained in:
@@ -19,6 +19,7 @@ import net.sf.jsqlparser.expression.*;
|
||||
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
|
||||
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
|
||||
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
|
||||
import net.sf.jsqlparser.expression.operators.relational.GreaterThan;
|
||||
import net.sf.jsqlparser.expression.operators.relational.InExpression;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@@ -140,17 +141,17 @@ public class DeptDataPermissionRule implements DataPermissionRule {
|
||||
return customDeptExpression;
|
||||
}
|
||||
|
||||
if(Objects.nonNull(deptExpression) && Objects.nonNull(userExpression) && Objects.nonNull(customDeptExpression)){
|
||||
if (Objects.nonNull(deptExpression) && Objects.nonNull(userExpression) && Objects.nonNull(customDeptExpression)) {
|
||||
// 目前,如果有指定部门 + 可查看自己,采用 OR 条件。即,WHERE (dept_id IN ? OR user_id = ?)
|
||||
return new Parenthesis(new OrExpression(new OrExpression(deptExpression, userExpression), customDeptExpression));
|
||||
}
|
||||
|
||||
if(Objects.nonNull(deptExpression) && Objects.nonNull(userExpression)){
|
||||
if (Objects.nonNull(deptExpression) && Objects.nonNull(userExpression)) {
|
||||
// 目前,如果有指定部门 + 可查看自己,采用 OR 条件。即,WHERE (dept_id IN ? OR user_id = ?)
|
||||
return new Parenthesis(new OrExpression(deptExpression, userExpression));
|
||||
}
|
||||
|
||||
if(Objects.nonNull(deptExpression)){
|
||||
if (Objects.nonNull(deptExpression)) {
|
||||
// 目前,如果有指定部门 + 可查看自己,采用 OR 条件。即,WHERE (dept_id IN ? OR user_id = ?)
|
||||
return new Parenthesis(new OrExpression(deptExpression, customDeptExpression));
|
||||
}
|
||||
@@ -195,12 +196,16 @@ public class DeptDataPermissionRule implements DataPermissionRule {
|
||||
Expression returnExpression = null;
|
||||
String[] columnNames = columnName.split(",");
|
||||
for (String column : columnNames) {
|
||||
Expression deptExpression = new InExpression(MyBatisUtils.buildColumn(tableName, tableAlias, column),
|
||||
new ExpressionList(CollectionUtils.convertList(deptIds, LongValue::new)));
|
||||
if (Objects.isNull(returnExpression)) {
|
||||
returnExpression = deptExpression;
|
||||
} else {
|
||||
returnExpression = new Parenthesis(new OrExpression(returnExpression, deptExpression));
|
||||
for (Long deptId : deptIds) {
|
||||
Expression deptExpression = new GreaterThan()
|
||||
.withLeftExpression(new Function().withName("FIND_IN_SET")
|
||||
.withParameters(new ExpressionList(new StringValue(deptId.toString()), MyBatisUtils.buildColumn(tableName, tableAlias, column))))
|
||||
.withRightExpression(new LongValue(0));
|
||||
if (Objects.isNull(returnExpression)) {
|
||||
returnExpression = deptExpression;
|
||||
} else {
|
||||
returnExpression = new Parenthesis(new OrExpression(returnExpression, deptExpression));
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnExpression;
|
||||
|
||||
Reference in New Issue
Block a user