From 35c41b3e16a218bb6282be90851888c8724edd65 Mon Sep 17 00:00:00 2001 From: ALI <1554198280@qq.com> Date: Mon, 19 Sep 2022 22:36:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=A7=86=E9=A2=91mybatis=E5=88=86=E9=A1=B5?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataScopeInnerInterceptor.java | 69 ++++++++++++------- .../MybatisDataScopeInterceptor.java | 2 + 2 files changed, 48 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/liry/mybatis/datascope/interceptor/DataScopeInnerInterceptor.java b/src/main/java/com/liry/mybatis/datascope/interceptor/DataScopeInnerInterceptor.java index 52b1e58..225b600 100644 --- a/src/main/java/com/liry/mybatis/datascope/interceptor/DataScopeInnerInterceptor.java +++ b/src/main/java/com/liry/mybatis/datascope/interceptor/DataScopeInnerInterceptor.java @@ -32,6 +32,7 @@ import net.sf.jsqlparser.statement.Statements; import net.sf.jsqlparser.statement.select.FromItem; import net.sf.jsqlparser.statement.select.PlainSelect; import net.sf.jsqlparser.statement.select.Select; +import net.sf.jsqlparser.statement.select.SubSelect; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlCommandType; @@ -64,35 +65,22 @@ public class DataScopeInnerInterceptor { StringBuilder sb = new StringBuilder(); for (Statement statement : statements.getStatements()) { if (statement instanceof Select) { - PlainSelect selectBody = (PlainSelect) ((Select) statement).getSelectBody(); - where(selectBody, args, dataScopeProperty); + PlainSelect plainSelect = (PlainSelect) ((Select) statement).getSelectBody(); + where(plainSelect, args, dataScopeProperty); - String tableName = ((Table) selectBody.getFromItem()).getName(); - List tableColumns = MybatisPlusUtil.getTableColumns(tableName); - - // 不确定的字段 - if (likeProperty != null) { - for (DataColumnProperty column : likeProperty.getColumns()) { - if (tableColumns.contains(column.getName())) { - where(selectBody, args, new DataScopeProperty(column)); - break; - } - } - } - // 当前线程 - if (threadDataScopeProperty != null) { - for (DataColumnProperty column : threadDataScopeProperty.getColumns()) { - if (tableColumns.contains(column.getName())) { - where(selectBody, args, new DataScopeProperty(column)); - break; - } + if (plainSelect.getFromItem() instanceof Table) { + tableSelectBuild(plainSelect, args, likeProperty, threadDataScopeProperty); + } else if (plainSelect.getFromItem() instanceof SubSelect) { + if (ms.getId().endsWith("_mpCount")) { + SubSelect subSelect = (SubSelect) plainSelect.getFromItem(); + PlainSelect subPlainSelect = (PlainSelect) subSelect.getSelectBody(); + tableSelectBuild(subPlainSelect, args, likeProperty, threadDataScopeProperty); } } - sb.append(selectBody).append(";"); + sb.append(plainSelect); } } - MappedStatement newMs = buildMappedStatement(ms, ms.getSqlCommandType(), sb.toString(), boundSql); // 将旧的声明替换 args[0] = newMs; @@ -105,6 +93,41 @@ public class DataScopeInnerInterceptor { } } + /** + * 构建selectbody + * + * @param plainSelect select 对象 + * @param args 实际参数 + * @param likeProperty 全局参数对象 + * @param threadDataScopeProperty 全局线程参数对象 + */ + private static void tableSelectBuild(PlainSelect plainSelect, Object[] args, DataScopeProperty likeProperty, + DataScopeProperty threadDataScopeProperty) { + if (plainSelect.getFromItem() instanceof Table) { + String tableName = ((Table) plainSelect.getFromItem()).getName(); + List tableColumns = MybatisPlusUtil.getTableColumns(tableName); + + // 不确定的字段 + if (likeProperty != null) { + for (DataColumnProperty column : likeProperty.getColumns()) { + if (tableColumns.contains(column.getName())) { + where(plainSelect, args, new DataScopeProperty(column)); + break; + } + } + } + // 当前线程 + if (threadDataScopeProperty != null) { + for (DataColumnProperty column : threadDataScopeProperty.getColumns()) { + if (tableColumns.contains(column.getName())) { + where(plainSelect, args, new DataScopeProperty(column)); + break; + } + } + } + } + } + /** * 构建新的声明 * diff --git a/src/main/java/com/liry/mybatis/datascope/interceptor/MybatisDataScopeInterceptor.java b/src/main/java/com/liry/mybatis/datascope/interceptor/MybatisDataScopeInterceptor.java index 4a7af04..5168ec0 100644 --- a/src/main/java/com/liry/mybatis/datascope/interceptor/MybatisDataScopeInterceptor.java +++ b/src/main/java/com/liry/mybatis/datascope/interceptor/MybatisDataScopeInterceptor.java @@ -29,6 +29,8 @@ import org.apache.ibatis.session.RowBounds; ) public class MybatisDataScopeInterceptor implements Interceptor { + public static final String COUNT = "_COUNT"; + @Override public Object intercept(Invocation invocation) throws Throwable { Object target = invocation.getTarget(); -- Gitee From 9216eb872e03f0993daf4c878a6aae892c807630 Mon Sep 17 00:00:00 2001 From: ALI <1554198280@qq.com> Date: Mon, 19 Sep 2022 22:47:37 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- readme.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 325dbe7..08740c8 100644 --- a/readme.md +++ b/readme.md @@ -16,6 +16,8 @@ 3. 支持动态添加数据权限`DataScopeUtil.put(方法全名, DataScopeProperty)` 4. 暂不支持自定义条件构建,并且,这个版本只支持=,>,>=,<,<=,in,not in,like 5. 支持不确定字段条件处理`DataScopeUtil.addGlobalLikeProperty` + 6. 支持线程层面的不确定字段处理`DataScopeUtil.putThreadDataScopeProperty(globalProperty)` + 注意:对于这些不确定字段,因为可能存在相同语言的字段,但名称不同,尽量将特殊的字段名设置在前面 5. 配置类示例 @@ -89,6 +91,17 @@ public interface DemoMapper extends MPJBaseMapper { columnProperty.setValue("ht"); columnProperty.setOperator("like"); globalProperty.setColumns(Arrays.asList(columnProperty)); - DataScopeUtil.addGlobalProperty(globalProperty); + DataScopeUtil.putGlobalProperty(globalProperty); + 示例三:设置线程权限 + + DataScopeProperty globalProperty=new DataScopeProperty(); + DataColumnProperty columnProperty=new DataColumnProperty(); + columnProperty.setName("url"); + columnProperty.setValue("ht"); + columnProperty.setOperator("like"); + globalProperty.setColumns(Arrays.asList(columnProperty)); + DataScopeUtil.putThreadDataScopeProperty(globalProperty); + //...过滤器链业务逻辑 + DataScopeUtil.destroyThread(); ``` \ No newline at end of file -- Gitee