From 825b9a147b8bd9c59526a803a0b7559b359cd36a Mon Sep 17 00:00:00 2001 From: PIGCLOUD Date: Wed, 10 Dec 2025 10:17:04 +0800 Subject: [PATCH] =?UTF-8?q?feat(data-permission):=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=9D=83=E9=99=90=E9=80=BB=E8=BE=91=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=A8=A1=E5=BC=8F=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增DataScopeLogicEnum枚举类定义AND/OR逻辑组合模式 - 在DataScope类中添加logicMode字段控制权限组合方式 - 修改DataScopeInnerInterceptor支持动态组合权限条件 - 默认保持OR逻辑以向后兼容现有功能 --- .../pigx/common/data/datascope/DataScope.java | 20 +++++++ .../datascope/DataScopeInnerInterceptor.java | 9 +++- .../data/datascope/DataScopeLogicEnum.java | 53 +++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeLogicEnum.java diff --git a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScope.java b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScope.java index f609b6aeb..34b088553 100644 --- a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScope.java +++ b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScope.java @@ -71,6 +71,16 @@ public class DataScope extends HashMap { */ private DataScopeFuncEnum func = DataScopeFuncEnum.ALL; + /** + * 逻辑组合模式,默认OR(向后兼容) + * 当同时有本人权限(username)和部门权限(deptList)时的逻辑组合方式 + * + */ + 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; + } + } diff --git a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeInnerInterceptor.java b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeInnerInterceptor.java index 90e736b1b..74f2e8d3e 100644 --- a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeInnerInterceptor.java +++ b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeInnerInterceptor.java @@ -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())) { diff --git a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeLogicEnum.java b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeLogicEnum.java new file mode 100644 index 000000000..5f17fa794 --- /dev/null +++ b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeLogicEnum.java @@ -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; + +}