feat(data-permission): 完善部门数据权限规则
-增加自定义部门条件方法,处理特定场景下的数据权限 - 优化条件组合逻辑,支持更多复杂的查询场景 - 添加注释说明,提高代码可读性和可维护性
This commit is contained in:
parent
a6b2f5831f
commit
bdda36048f
@ -18,6 +18,7 @@ import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
|||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.sf.jsqlparser.expression.*;
|
import net.sf.jsqlparser.expression.*;
|
||||||
|
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.expression.operators.relational.EqualsTo;
|
||||||
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
|
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
|
||||||
@ -134,7 +135,8 @@ public class DeptDataPermissionRule implements DataPermissionRule {
|
|||||||
// 情况三,拼接 Dept 和 User 的条件,最后组合
|
// 情况三,拼接 Dept 和 User 的条件,最后组合
|
||||||
Expression deptExpression = buildDeptExpression(tableName, tableAlias, deptDataPermission.getDeptIds());
|
Expression deptExpression = buildDeptExpression(tableName, tableAlias, deptDataPermission.getDeptIds());
|
||||||
Expression userExpression = buildUserExpression(tableName, tableAlias, deptDataPermission.getSelf(), loginUser.getId());
|
Expression userExpression = buildUserExpression(tableName, tableAlias, deptDataPermission.getSelf(), loginUser.getId());
|
||||||
if (deptExpression == null && userExpression == null) {
|
Expression selfDeptExpression = selfDeptExpression(tableName, tableAlias, deptDataPermission.getSelf(), deptDataPermission.getDeptId());
|
||||||
|
if (deptExpression == null && userExpression == null && selfDeptExpression == null) {
|
||||||
// TODO 芋艿:获得不到条件的时候,暂时不抛出异常,而是不返回数据
|
// TODO 芋艿:获得不到条件的时候,暂时不抛出异常,而是不返回数据
|
||||||
log.warn("[getExpression][LoginUser({}) Table({}/{}) DeptDataPermission({}) 构建的条件为空]",
|
log.warn("[getExpression][LoginUser({}) Table({}/{}) DeptDataPermission({}) 构建的条件为空]",
|
||||||
JsonUtils.toJsonString(loginUser), tableName, tableAlias, JsonUtils.toJsonString(deptDataPermission));
|
JsonUtils.toJsonString(loginUser), tableName, tableAlias, JsonUtils.toJsonString(deptDataPermission));
|
||||||
@ -143,13 +145,24 @@ public class DeptDataPermissionRule implements DataPermissionRule {
|
|||||||
return EXPRESSION_NULL;
|
return EXPRESSION_NULL;
|
||||||
}
|
}
|
||||||
if (deptExpression == null) {
|
if (deptExpression == null) {
|
||||||
return userExpression;
|
|
||||||
|
if (userExpression == null) {
|
||||||
|
return selfDeptExpression;
|
||||||
|
}else if (selfDeptExpression == null) {
|
||||||
|
return userExpression;
|
||||||
|
}else {
|
||||||
|
return new Parenthesis(new AndExpression(userExpression, selfDeptExpression));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (userExpression == null) {
|
if (userExpression == null) {
|
||||||
return deptExpression;
|
return deptExpression;
|
||||||
}
|
}
|
||||||
// 目前,如果有指定部门 + 可查看自己,采用 OR 条件。即,WHERE (dept_id IN ? OR user_id = ?)
|
if (selfDeptExpression == null) {
|
||||||
return new Parenthesis(new OrExpression(deptExpression, userExpression));
|
// 目前,如果有指定部门 + 可查看自己,采用 OR 条件。即,WHERE (dept_id IN ? OR user_id = ? )
|
||||||
|
return new Parenthesis(new OrExpression(deptExpression, userExpression));
|
||||||
|
}
|
||||||
|
// 目前,如果有指定部门 + 可查看自己,采用 OR 条件。即,WHERE (dept_id IN ? OR (user_id = ? AND dept_id = ? ))
|
||||||
|
return new Parenthesis(new OrExpression(deptExpression, new AndExpression(userExpression, selfDeptExpression)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Expression buildDeptExpression(String tableName, Alias tableAlias, Set<Long> deptIds) {
|
private Expression buildDeptExpression(String tableName, Alias tableAlias, Set<Long> deptIds) {
|
||||||
@ -180,6 +193,19 @@ public class DeptDataPermissionRule implements DataPermissionRule {
|
|||||||
return new EqualsTo(MyBatisUtils.buildColumn(tableName, tableAlias, columnName), new LongValue(userId));
|
return new EqualsTo(MyBatisUtils.buildColumn(tableName, tableAlias, columnName), new LongValue(userId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Expression selfDeptExpression(String tableName, Alias tableAlias, Boolean self, Long deptId) {
|
||||||
|
// 如果不查看自己,则无需作为条件
|
||||||
|
if (Boolean.FALSE.equals(self)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String columnName = deptColumns.get(tableName);
|
||||||
|
if (StrUtil.isEmpty(columnName)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// 拼接条件
|
||||||
|
return new EqualsTo(MyBatisUtils.buildColumn(tableName, tableAlias, columnName), new LongValue(deptId));
|
||||||
|
}
|
||||||
|
|
||||||
// ==================== 添加配置 ====================
|
// ==================== 添加配置 ====================
|
||||||
|
|
||||||
public void addDeptColumn(Class<? extends BaseDO> entityClass) {
|
public void addDeptColumn(Class<? extends BaseDO> entityClass) {
|
||||||
|
Loading…
Reference in New Issue
Block a user