From a94b226e59ffc809147bb7d42447319c4ae3d80d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= <2270033969@qq.com> Date: Thu, 21 Nov 2024 21:01:46 +0800 Subject: [PATCH] =?UTF-8?q?refactor(data):=20=E6=94=AF=E6=8C=81=E5=A4=9A?= =?UTF-8?q?=E8=A7=92=E8=89=B2=E6=95=B0=E6=8D=AE=E6=9D=83=E9=99=90=E8=AE=A1?= =?UTF-8?q?=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../datascope/DataScopeInnerInterceptor.java | 124 +++++++++--------- .../data/datascope/DataScopeTypeEnum.java | 70 ++++++---- 2 files changed, 107 insertions(+), 87 deletions(-) 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 fdbdf8388..ba6f3d065 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 @@ -20,73 +20,79 @@ import java.util.Map; */ public class DataScopeInnerInterceptor implements DataScopeInterceptor { - @Setter - private DataScopeHandle dataScopeHandle; + @Setter + private DataScopeHandle dataScopeHandle; - @Override - public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, - ResultHandler resultHandler, BoundSql boundSql) { - PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql); + @Override + public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, + ResultHandler resultHandler, BoundSql boundSql) { + PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql); - String originalSql = boundSql.getSql(); - Object parameterObject = boundSql.getParameterObject(); + String originalSql = boundSql.getSql(); + Object parameterObject = boundSql.getParameterObject(); - // 查找参数中包含DataScope类型的参数 - DataScope dataScope = findDataScopeObject(parameterObject); - if (dataScope == null) { - return; - } + // 查找参数中包含DataScope类型的参数 + DataScope dataScope = findDataScopeObject(parameterObject); + if (dataScope == null) { + return; + } - // 返回true 不拦截直接返回原始 SQL (只针对 * 查询) - if (dataScopeHandle.calcScope(dataScope) && DataScopeFuncEnum.ALL.equals(dataScope.getFunc())) { - return; - } + // 返回true 不拦截直接返回原始 SQL (只针对 * 查询) + if (DataScopeFuncEnum.ALL.equals(dataScope.getFunc()) && dataScopeHandle.calcScope(dataScope)) { + return; + } - // 返回true 不拦截直接返回原始 SQL (只针对 COUNT 查询) - if (dataScopeHandle.calcScope(dataScope) && DataScopeFuncEnum.COUNT.equals(dataScope.getFunc())) { - mpBs.sql(String.format("SELECT %s FROM (%s) temp_data_scope", dataScope.getFunc().getType(), originalSql)); - return; - } + // 返回true 不拦截直接返回原始 SQL (只针对 COUNT 查询) + if (DataScopeFuncEnum.COUNT.equals(dataScope.getFunc()) && dataScopeHandle.calcScope(dataScope)) { + mpBs.sql(String.format("SELECT %s FROM (%s) temp_data_scope", dataScope.getFunc().getType(), originalSql)); + return; + } - List deptIds = dataScope.getDeptList(); + List deptIds = dataScope.getDeptList(); - // 1.无数据权限限制,则直接返回 0 条数据 - if (CollUtil.isEmpty(deptIds) && StrUtil.isBlank(dataScope.getUsername())) { - originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE 1 = 2", - dataScope.getFunc().getType(), originalSql); - } - // 2.如果为本人权限则走下面 - else if (StrUtil.isNotBlank(dataScope.getUsername())) { - originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE temp_data_scope.%s = '%s'", - dataScope.getFunc().getType(), originalSql, dataScope.getScopeUserName(), dataScope.getUsername()); - } - // 3.都没有,则是其他权限,走下面 - else { - String join = CollectionUtil.join(deptIds, ","); - originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE temp_data_scope.%s IN (%s)", - dataScope.getFunc().getType(), originalSql, dataScope.getScopeDeptName(), join); - } + // 1.无数据权限限制,则直接返回 0 条数据 + if (CollUtil.isEmpty(deptIds) && StrUtil.isBlank(dataScope.getUsername())) { + originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE 1 = 2", + dataScope.getFunc().getType(), originalSql); + } + // 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); + } + // 3. 如果为本人 + else if (StrUtil.isNotBlank(dataScope.getUsername())) { + originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE temp_data_scope.%s = '%s'", + dataScope.getFunc().getType(), originalSql, dataScope.getScopeUserName(), dataScope.getUsername()); + } + // 4.部门权限控制 + else { + String join = CollectionUtil.join(deptIds, ","); + originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE temp_data_scope.%s IN (%s)", + dataScope.getFunc().getType(), originalSql, dataScope.getScopeDeptName(), join); + } - mpBs.sql(originalSql); - } + mpBs.sql(originalSql); + } - /** - * 查找参数是否包括DataScope对象 - * @param parameterObj 参数列表 - * @return DataScope - */ - private DataScope findDataScopeObject(Object parameterObj) { - if (parameterObj instanceof DataScope) { - return (DataScope) parameterObj; - } - else if (parameterObj instanceof Map) { - for (Object val : ((Map) parameterObj).values()) { - if (val instanceof DataScope) { - return (DataScope) val; - } - } - } - return null; - } + /** + * 查找参数是否包括DataScope对象 + * + * @param parameterObj 参数列表 + * @return DataScope + */ + private DataScope findDataScopeObject(Object parameterObj) { + if (parameterObj instanceof DataScope) { + return (DataScope) parameterObj; + } else if (parameterObj instanceof Map) { + for (Object val : ((Map) parameterObj).values()) { + if (val instanceof DataScope) { + return (DataScope) val; + } + } + } + return null; + } } diff --git a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeTypeEnum.java b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeTypeEnum.java index 0dbacefb8..6a29a15cf 100644 --- a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeTypeEnum.java +++ b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeTypeEnum.java @@ -30,39 +30,53 @@ import lombok.Getter; @AllArgsConstructor public enum DataScopeTypeEnum { - /** - * 查询全部数据 - */ - ALL(0, "全部"), + /** + * 查询全部数据 + */ + ALL(0, "全部"), - /** - * 自定义 - */ - CUSTOM(1, "自定义"), + /** + * 自定义 + */ + CUSTOM(1, "自定义"), - /** - * 本级及子级 - */ - OWN_CHILD_LEVEL(2, "本级及子级"), + /** + * 本级及子级 + */ + OWN_CHILD_LEVEL(2, "本级及子级"), - /** - * 本级 - */ - OWN_LEVEL(3, "本级"), + /** + * 本级 + */ + OWN_LEVEL(3, "本级"), - /** - * 本人 - */ - SELF_LEVEL(4, "本人"); + /** + * 本人 + */ + SELF_LEVEL(4, "本人"); - /** - * 类型 - */ - private final int type; + /** + * 类型 + */ + private final int type; - /** - * 描述 - */ - private final String description; + /** + * 描述 + */ + private final String description; + /** + * 按类型获取 + * + * @param dsType DS 型 + * @return {@link DataScopeTypeEnum } + */ + public static DataScopeTypeEnum getByType(Integer dsType) { + for (DataScopeTypeEnum value : DataScopeTypeEnum.values()) { + if (value.getType() == dsType) { + return value; + } + } + return null; + } }