feat(data-permission): 新增数据权限逻辑组合模式支持

- 新增DataScopeLogicEnum枚举类定义AND/OR逻辑组合模式
- 在DataScope类中添加logicMode字段控制权限组合方式
- 修改DataScopeInnerInterceptor支持动态组合权限条件
- 默认保持OR逻辑以向后兼容现有功能
This commit is contained in:
PIGCLOUD committed 2025-12-28 11:25:42 +08:00
1 parent 16a7f9309f
commit 825b9a147b
3 files changed
+80 -2

No files matched your search

@@ -71,6 +71,16 @@ public class DataScope extends HashMap {
*/
private DataScopeFuncEnum func = DataScopeFuncEnum.ALL;
/**
* 逻辑组合模式,默认OR(向后兼容)
* 当同时有本人权限(username)和部门权限(deptList)时的逻辑组合方式
* <ul>
* <li>OR: 满足任一条件即可(默认)</li>
* <li>AND: 需同时满足所有条件</li>
* </ul>
*/
private DataScopeLogicEnum logicMode = DataScopeLogicEnum.OR;
/**
* of 获取实例
*/
@@ -93,4 +103,14 @@ public class DataScope extends HashMap {
return this;
}
/**
* 设置逻辑组合模式
* @param logicMode ��辑模式(AND/OR)
* @return DataScope
*/
public DataScope logic(DataScopeLogicEnum logicMode) {
this.logicMode = logicMode;
return this;
}
}
@@ -63,8 +63,13 @@ public class DataScopeInnerInterceptor implements DataScopeInterceptor {
// 2.如果为本人权限 + 部门权限控制
else if (StrUtil.isNotBlank(dataScope.getUsername()) && CollUtil.isNotEmpty(deptIds)) {
String join = CollectionUtil.join(deptIds, ",");
originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE temp_data_scope.%s = '%s' OR temp_data_scope.%s IN (%s)",
dataScope.getFunc().getType(), originalSql, dataScope.getScopeUserName(), dataScope.getUsername(), dataScope.getScopeDeptName(), join);
// 使用配置的组合模式(AND/OR),默认OR保持向后兼容
String logicKeyword = dataScope.getLogicMode() != null ? dataScope.getLogicMode().getKeyword()
: DataScopeLogicEnum.OR.getKeyword();
originalSql = String.format(
"SELECT %s FROM (%s) temp_data_scope WHERE temp_data_scope.%s = '%s' %s temp_data_scope.%s IN (%s)",
dataScope.getFunc().getType(), originalSql, dataScope.getScopeUserName(), dataScope.getUsername(),
logicKeyword, dataScope.getScopeDeptName(), join);
}
// 3. 如果为本人
else if (StrUtil.isNotBlank(dataScope.getUsername())) {
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.common.data.datascope;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* 数据权限逻辑组合类型
*
* @author lengleng
* @date 2025-12-10
*/
@Getter
@AllArgsConstructor
public enum DataScopeLogicEnum {
/**
* OR逻辑 - 满足任一条件即可(默认) 示例: WHERE create_by='user' OR dept_id IN (1,2,3)
*/
OR("OR", "或"),
/**
* AND逻辑 - 需同时满足所有条件 示例: WHERE create_by='user' AND dept_id IN (1,2,3)
*/
AND("AND", "且");
/**
* SQL关键字
*/
private final String keyword;
/**
* 描述
*/
private final String description;
}