From 084d5647400e46bae6c89ed2bb01befbf9dad558 Mon Sep 17 00:00:00 2001
From: lorne <1991wangliang@gmail.com>
Date: Thu, 19 Mar 2026 20:22:07 +0800
Subject: [PATCH] add IConditionSQLHandler
---
.../example-app-cmd-domain/pom.xml | 2 +-
.../example-app/example-app-cmd-meta/pom.xml | 2 +-
example/example-app/example-app-query/pom.xml | 2 +-
example/example-app/pom.xml | 2 +-
.../example-domain-leave/pom.xml | 2 +-
.../example-domain-user/pom.xml | 2 +-
example/example-domain/pom.xml | 2 +-
.../example-infra/example-infra-flow/pom.xml | 2 +-
.../example-infra/example-infra-jpa/pom.xml | 2 +-
.../example-infra-security/pom.xml | 2 +-
example/example-infra/pom.xml | 2 +-
example/example-interface/pom.xml | 2 +-
example/example-server/pom.xml | 2 +-
example/pom.xml | 2 +-
pom.xml | 2 +-
springboot-starter-data-authorization/pom.xml | 2 +-
.../condition/IConditionSQL.java | 6 ++
.../condition/JoinConditionSQL.java | 41 ++++++++++
.../condition/WhereConditionSQL.java | 18 +++++
.../enhancer/DataPermissionSQLEnhancer.java | 19 ++---
.../handler/ConditionSQLHandlerContext.java | 38 +++++++++
.../handler/IConditionSQLHandler.java | 15 ++++
.../handler/JoinConditionSQLHandler.java | 52 ++++++++++++
.../handler/WhereConditionSQLHandler.java | 31 +++++++
.../authorization/handler/Condition.java | 23 ++++--
.../DataAuthorizationContextTest.java | 81 ++++++++++++++++++-
springboot-starter-data-fast/pom.xml | 2 +-
springboot-starter-flow/pom.xml | 2 +-
springboot-starter-security/pom.xml | 2 +-
springboot-starter/pom.xml | 2 +-
30 files changed, 321 insertions(+), 43 deletions(-)
create mode 100644 springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/condition/IConditionSQL.java
create mode 100644 springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/condition/JoinConditionSQL.java
create mode 100644 springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/condition/WhereConditionSQL.java
create mode 100644 springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/handler/ConditionSQLHandlerContext.java
create mode 100644 springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/handler/IConditionSQLHandler.java
create mode 100644 springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/handler/JoinConditionSQLHandler.java
create mode 100644 springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/handler/WhereConditionSQLHandler.java
diff --git a/example/example-app/example-app-cmd-domain/pom.xml b/example/example-app/example-app-cmd-domain/pom.xml
index caa94bd25..f4c87dcc8 100644
--- a/example/example-app/example-app-cmd-domain/pom.xml
+++ b/example/example-app/example-app-cmd-domain/pom.xml
@@ -6,7 +6,7 @@
com.codingapi.springboot
example-app
- 3.4.36
+ 3.4.37
../pom.xml
diff --git a/example/example-app/example-app-cmd-meta/pom.xml b/example/example-app/example-app-cmd-meta/pom.xml
index 1f99e3d75..5a791d3bf 100644
--- a/example/example-app/example-app-cmd-meta/pom.xml
+++ b/example/example-app/example-app-cmd-meta/pom.xml
@@ -6,7 +6,7 @@
com.codingapi.springboot
example-app
- 3.4.36
+ 3.4.37
../pom.xml
diff --git a/example/example-app/example-app-query/pom.xml b/example/example-app/example-app-query/pom.xml
index d596ef7a6..f5ca6c06a 100644
--- a/example/example-app/example-app-query/pom.xml
+++ b/example/example-app/example-app-query/pom.xml
@@ -6,7 +6,7 @@
com.codingapi.springboot
example-app
- 3.4.36
+ 3.4.37
../pom.xml
diff --git a/example/example-app/pom.xml b/example/example-app/pom.xml
index bed9922e3..b7d1181a6 100644
--- a/example/example-app/pom.xml
+++ b/example/example-app/pom.xml
@@ -6,7 +6,7 @@
com.codingapi.springboot
springboot-example
- 3.4.36
+ 3.4.37
../pom.xml
pom
diff --git a/example/example-domain/example-domain-leave/pom.xml b/example/example-domain/example-domain-leave/pom.xml
index d3ccf773f..cae0a9607 100644
--- a/example/example-domain/example-domain-leave/pom.xml
+++ b/example/example-domain/example-domain-leave/pom.xml
@@ -6,7 +6,7 @@
com.codingapi.springboot
example-domain
- 3.4.36
+ 3.4.37
../pom.xml
diff --git a/example/example-domain/example-domain-user/pom.xml b/example/example-domain/example-domain-user/pom.xml
index 4b3414de8..7e92e8617 100644
--- a/example/example-domain/example-domain-user/pom.xml
+++ b/example/example-domain/example-domain-user/pom.xml
@@ -6,7 +6,7 @@
com.codingapi.springboot
example-domain
- 3.4.36
+ 3.4.37
../pom.xml
diff --git a/example/example-domain/pom.xml b/example/example-domain/pom.xml
index fb1b38e03..f5001cf2a 100644
--- a/example/example-domain/pom.xml
+++ b/example/example-domain/pom.xml
@@ -5,7 +5,7 @@
com.codingapi.springboot
springboot-example
- 3.4.36
+ 3.4.37
../pom.xml
4.0.0
diff --git a/example/example-infra/example-infra-flow/pom.xml b/example/example-infra/example-infra-flow/pom.xml
index b386702a1..07a76f1c6 100644
--- a/example/example-infra/example-infra-flow/pom.xml
+++ b/example/example-infra/example-infra-flow/pom.xml
@@ -5,7 +5,7 @@
com.codingapi.springboot
example-infra
- 3.4.36
+ 3.4.37
../pom.xml
diff --git a/example/example-infra/example-infra-jpa/pom.xml b/example/example-infra/example-infra-jpa/pom.xml
index 8767d8ec5..5b492faa7 100644
--- a/example/example-infra/example-infra-jpa/pom.xml
+++ b/example/example-infra/example-infra-jpa/pom.xml
@@ -5,7 +5,7 @@
com.codingapi.springboot
example-infra
- 3.4.36
+ 3.4.37
../pom.xml
diff --git a/example/example-infra/example-infra-security/pom.xml b/example/example-infra/example-infra-security/pom.xml
index 9acc8a4d1..8ec9fc28e 100644
--- a/example/example-infra/example-infra-security/pom.xml
+++ b/example/example-infra/example-infra-security/pom.xml
@@ -6,7 +6,7 @@
com.codingapi.springboot
example-infra
- 3.4.36
+ 3.4.37
../pom.xml
diff --git a/example/example-infra/pom.xml b/example/example-infra/pom.xml
index 0d5d9336f..d6fcdafc9 100644
--- a/example/example-infra/pom.xml
+++ b/example/example-infra/pom.xml
@@ -6,7 +6,7 @@
com.codingapi.springboot
springboot-example
- 3.4.36
+ 3.4.37
../pom.xml
pom
diff --git a/example/example-interface/pom.xml b/example/example-interface/pom.xml
index b642c6439..cae61cc4e 100644
--- a/example/example-interface/pom.xml
+++ b/example/example-interface/pom.xml
@@ -6,7 +6,7 @@
com.codingapi.springboot
springboot-example
- 3.4.36
+ 3.4.37
example-interface
diff --git a/example/example-server/pom.xml b/example/example-server/pom.xml
index 1951ce377..34d122887 100644
--- a/example/example-server/pom.xml
+++ b/example/example-server/pom.xml
@@ -5,7 +5,7 @@
springboot-example
com.codingapi.springboot
- 3.4.36
+ 3.4.37
4.0.0
diff --git a/example/pom.xml b/example/pom.xml
index 41268df03..2d14b7455 100644
--- a/example/pom.xml
+++ b/example/pom.xml
@@ -19,7 +19,7 @@
springboot-example
- 3.4.36
+ 3.4.37
springboot-example
springboot-example project for Spring Boot
diff --git a/pom.xml b/pom.xml
index e964864cc..42b5cb729 100644
--- a/pom.xml
+++ b/pom.xml
@@ -12,7 +12,7 @@
com.codingapi.springboot
springboot-parent
- 3.4.36
+ 3.4.37
https://github.com/codingapi/springboot-framewrok
springboot-parent
diff --git a/springboot-starter-data-authorization/pom.xml b/springboot-starter-data-authorization/pom.xml
index a4f9fc2d0..dc7ad3795 100644
--- a/springboot-starter-data-authorization/pom.xml
+++ b/springboot-starter-data-authorization/pom.xml
@@ -6,7 +6,7 @@
com.codingapi.springboot
springboot-parent
- 3.4.36
+ 3.4.37
springboot-starter-data-authorization
diff --git a/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/condition/IConditionSQL.java b/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/condition/IConditionSQL.java
new file mode 100644
index 000000000..af35e7e09
--- /dev/null
+++ b/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/condition/IConditionSQL.java
@@ -0,0 +1,6 @@
+package com.codingapi.springboot.authorization.condition;
+
+public interface IConditionSQL {
+
+
+}
diff --git a/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/condition/JoinConditionSQL.java b/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/condition/JoinConditionSQL.java
new file mode 100644
index 000000000..3c5a9b272
--- /dev/null
+++ b/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/condition/JoinConditionSQL.java
@@ -0,0 +1,41 @@
+package com.codingapi.springboot.authorization.condition;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+import net.sf.jsqlparser.expression.Alias;
+import net.sf.jsqlparser.schema.Table;
+import org.springframework.util.StringUtils;
+
+@Setter
+@Getter
+@AllArgsConstructor
+@NoArgsConstructor
+public class JoinConditionSQL implements IConditionSQL {
+
+ private Type type;
+ private String tableName;
+ private String tableAlias;
+ private String onCondition;
+
+
+
+ public static enum Type{
+ INNER,
+ LEFT,
+ RIGHT
+ }
+
+
+ public Table toJoinTable(){
+ if(StringUtils.hasText(tableAlias)){
+ Table table = new Table(this.tableName);
+ table.setAlias(new Alias(tableAlias));
+ return table;
+ }else {
+ return new Table(this.tableName);
+ }
+ }
+
+}
diff --git a/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/condition/WhereConditionSQL.java b/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/condition/WhereConditionSQL.java
new file mode 100644
index 000000000..d3d695dde
--- /dev/null
+++ b/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/condition/WhereConditionSQL.java
@@ -0,0 +1,18 @@
+package com.codingapi.springboot.authorization.condition;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class WhereConditionSQL implements IConditionSQL {
+
+ private String condition;
+
+ public WhereConditionSQL(String condition,Object... args){
+ this.condition = String.format(condition, args);
+ }
+
+}
diff --git a/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/DataPermissionSQLEnhancer.java b/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/DataPermissionSQLEnhancer.java
index f0908b7ef..095c8cb1d 100644
--- a/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/DataPermissionSQLEnhancer.java
+++ b/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/DataPermissionSQLEnhancer.java
@@ -1,27 +1,21 @@
package com.codingapi.springboot.authorization.enhancer;
+import com.codingapi.springboot.authorization.condition.IConditionSQL;
+import com.codingapi.springboot.authorization.enhancer.handler.ConditionSQLHandlerContext;
import com.codingapi.springboot.authorization.handler.Condition;
import com.codingapi.springboot.authorization.handler.RowHandler;
-import net.sf.jsqlparser.expression.BinaryExpression;
import net.sf.jsqlparser.expression.Expression;
-import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
-import net.sf.jsqlparser.expression.SignedExpression;
-import net.sf.jsqlparser.expression.operators.arithmetic.Subtraction;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
-import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.InExpression;
-import net.sf.jsqlparser.expression.operators.relational.LikeExpression;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.*;
-import net.sf.jsqlparser.util.TablesNamesFinder;
import java.sql.SQLException;
import java.util.List;
-import java.util.Set;
/**
* 数据权限 SQL 增强器
@@ -162,12 +156,9 @@ private void injectDataPermissionCondition(PlainSelect plainSelect, Table table,
String aliaName = table.getAlias() != null ? table.getAlias().getName() : tableName;
Condition condition = rowHandler.handler(plainSelect.toString(), tableName, aliaName);
if (condition != null) {
- // 添加自定义条件
- Expression customExpression = CCJSqlParserUtil.parseCondExpression(condition.getCondition());
- if (where != null) {
- plainSelect.setWhere(new AndExpression(customExpression, where));
- } else {
- plainSelect.setWhere(customExpression);
+ List conditionList = condition.getConditionList();
+ for (IConditionSQL conditionSQL:conditionList) {
+ ConditionSQLHandlerContext.getInstance().handler(conditionSQL,plainSelect,table,where);
}
}
}
diff --git a/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/handler/ConditionSQLHandlerContext.java b/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/handler/ConditionSQLHandlerContext.java
new file mode 100644
index 000000000..c89613084
--- /dev/null
+++ b/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/handler/ConditionSQLHandlerContext.java
@@ -0,0 +1,38 @@
+package com.codingapi.springboot.authorization.enhancer.handler;
+
+import com.codingapi.springboot.authorization.condition.IConditionSQL;
+import lombok.Getter;
+import net.sf.jsqlparser.expression.Expression;
+import net.sf.jsqlparser.schema.Table;
+import net.sf.jsqlparser.statement.select.PlainSelect;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class ConditionSQLHandlerContext {
+
+ private final List handlers;
+
+ @Getter
+ private final static ConditionSQLHandlerContext instance = new ConditionSQLHandlerContext();
+
+ private ConditionSQLHandlerContext() {
+ this.handlers = new ArrayList<>();
+ this.registerHandlers();
+ }
+
+
+ private void registerHandlers(){
+ this.handlers.add(new WhereConditionSQLHandler());
+ this.handlers.add(new JoinConditionSQLHandler());
+ }
+
+
+ public void handler(IConditionSQL dynamicSQL, PlainSelect plainSelect, Table table, Expression where) {
+ for (IConditionSQLHandler handler:handlers){
+ if (handler.support(dynamicSQL)){
+ handler.handler(dynamicSQL,plainSelect, table, where);
+ }
+ }
+ }
+}
diff --git a/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/handler/IConditionSQLHandler.java b/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/handler/IConditionSQLHandler.java
new file mode 100644
index 000000000..f99f3be9e
--- /dev/null
+++ b/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/handler/IConditionSQLHandler.java
@@ -0,0 +1,15 @@
+package com.codingapi.springboot.authorization.enhancer.handler;
+
+import com.codingapi.springboot.authorization.condition.IConditionSQL;
+import net.sf.jsqlparser.expression.Expression;
+import net.sf.jsqlparser.schema.Table;
+import net.sf.jsqlparser.statement.select.PlainSelect;
+
+public interface IConditionSQLHandler {
+
+ boolean support(IConditionSQL conditionSQL);
+
+ void handler(IConditionSQL conditionSQL, PlainSelect plainSelect, Table table, Expression where);
+
+
+}
diff --git a/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/handler/JoinConditionSQLHandler.java b/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/handler/JoinConditionSQLHandler.java
new file mode 100644
index 000000000..e5e5a41fb
--- /dev/null
+++ b/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/handler/JoinConditionSQLHandler.java
@@ -0,0 +1,52 @@
+package com.codingapi.springboot.authorization.enhancer.handler;
+
+import com.codingapi.springboot.authorization.condition.IConditionSQL;
+import com.codingapi.springboot.authorization.condition.JoinConditionSQL;
+import lombok.SneakyThrows;
+import net.sf.jsqlparser.expression.Expression;
+import net.sf.jsqlparser.parser.CCJSqlParserUtil;
+import net.sf.jsqlparser.schema.Table;
+import net.sf.jsqlparser.statement.select.Join;
+import net.sf.jsqlparser.statement.select.PlainSelect;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+public class JoinConditionSQLHandler implements IConditionSQLHandler {
+
+ @Override
+ public boolean support(IConditionSQL conditionSQL) {
+ return conditionSQL instanceof JoinConditionSQL;
+ }
+
+ @SneakyThrows
+ @Override
+ public void handler(IConditionSQL conditionSQL, PlainSelect plainSelect, Table table, Expression where) {
+ JoinConditionSQL joinConditionSQL = (JoinConditionSQL) conditionSQL;
+
+ if(plainSelect.getJoins()==null){
+ plainSelect.setJoins(new ArrayList<>());
+ }
+
+ Join join = new Join();
+ join.setFromItem(joinConditionSQL.toJoinTable());
+
+ if(joinConditionSQL.getType()== JoinConditionSQL.Type.INNER){
+ join.setInner(true);
+ }
+
+ if(joinConditionSQL.getType()== JoinConditionSQL.Type.RIGHT){
+ join.setRight(true);
+ }
+
+ if(joinConditionSQL.getType()== JoinConditionSQL.Type.LEFT){
+ join.setLeft(true);
+ }
+
+ Collection expressions = new ArrayList<>();
+ Expression expression = CCJSqlParserUtil.parseCondExpression(joinConditionSQL.getOnCondition());
+ expressions.add(expression);
+ join.setOnExpressions(expressions);
+ plainSelect.addJoins(join);
+ }
+}
diff --git a/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/handler/WhereConditionSQLHandler.java b/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/handler/WhereConditionSQLHandler.java
new file mode 100644
index 000000000..c55897609
--- /dev/null
+++ b/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/handler/WhereConditionSQLHandler.java
@@ -0,0 +1,31 @@
+package com.codingapi.springboot.authorization.enhancer.handler;
+
+import com.codingapi.springboot.authorization.condition.IConditionSQL;
+import com.codingapi.springboot.authorization.condition.WhereConditionSQL;
+import lombok.SneakyThrows;
+import net.sf.jsqlparser.expression.Expression;
+import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
+import net.sf.jsqlparser.parser.CCJSqlParserUtil;
+import net.sf.jsqlparser.schema.Table;
+import net.sf.jsqlparser.statement.select.PlainSelect;
+
+public class WhereConditionSQLHandler implements IConditionSQLHandler {
+
+ @Override
+ public boolean support(IConditionSQL conditionSQL) {
+ return conditionSQL instanceof WhereConditionSQL;
+ }
+
+ @SneakyThrows
+ @Override
+ public void handler(IConditionSQL conditionSQL, PlainSelect plainSelect, Table table, Expression where) {
+ String whereSQL = ((WhereConditionSQL) conditionSQL).getCondition();
+ // 添加自定义条件
+ Expression customExpression = CCJSqlParserUtil.parseCondExpression(whereSQL);
+ if (where != null) {
+ plainSelect.setWhere(new AndExpression(customExpression, where));
+ } else {
+ plainSelect.setWhere(customExpression);
+ }
+ }
+}
diff --git a/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/handler/Condition.java b/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/handler/Condition.java
index 3cb6d62e7..b4c39bd1d 100644
--- a/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/handler/Condition.java
+++ b/springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/handler/Condition.java
@@ -1,25 +1,36 @@
package com.codingapi.springboot.authorization.handler;
+import com.codingapi.springboot.authorization.condition.IConditionSQL;
+import com.codingapi.springboot.authorization.condition.WhereConditionSQL;
import lombok.Getter;
+import java.util.ArrayList;
+import java.util.List;
+
/**
* 查询条件
*/
@Getter
public class Condition {
- private final String condition;
+ private final List conditionList;
+
+ public Condition() {
+ this.conditionList = new ArrayList<>();
+ }
- private Condition(String condition) {
- this.condition = condition;
+ public void addDynamicSQL(IConditionSQL dynamicSQL){
+ this.conditionList.add(dynamicSQL);
}
public static Condition customCondition(String condition) {
- return new Condition(condition);
+ Condition dynamicSQLContent = new Condition();
+ dynamicSQLContent.addDynamicSQL(new WhereConditionSQL(condition));
+ return dynamicSQLContent;
}
public static Condition formatCondition(String condition, Object... args) {
- return new Condition(String.format(condition, args));
+ return customCondition(String.format(condition, args));
}
public static Condition emptyCondition() {
@@ -27,7 +38,7 @@ public static Condition emptyCondition() {
}
public static Condition defaultCondition() {
- return new Condition("1=1");
+ return customCondition("1=1");
}
}
diff --git a/springboot-starter-data-authorization/src/test/java/com/codingapi/springboot/authorization/DataAuthorizationContextTest.java b/springboot-starter-data-authorization/src/test/java/com/codingapi/springboot/authorization/DataAuthorizationContextTest.java
index 581836884..a455b439a 100644
--- a/springboot-starter-data-authorization/src/test/java/com/codingapi/springboot/authorization/DataAuthorizationContextTest.java
+++ b/springboot-starter-data-authorization/src/test/java/com/codingapi/springboot/authorization/DataAuthorizationContextTest.java
@@ -1,15 +1,14 @@
package com.codingapi.springboot.authorization;
import com.codingapi.springboot.authorization.current.CurrentUser;
-import com.codingapi.springboot.authorization.enhancer.DataPermissionSQLEnhancer;
+import com.codingapi.springboot.authorization.condition.JoinConditionSQL;
+import com.codingapi.springboot.authorization.condition.WhereConditionSQL;
import com.codingapi.springboot.authorization.entity.Depart;
import com.codingapi.springboot.authorization.entity.Unit;
import com.codingapi.springboot.authorization.entity.User;
-import com.codingapi.springboot.authorization.filter.DataAuthorizationFilter;
import com.codingapi.springboot.authorization.filter.DefaultDataAuthorizationFilter;
import com.codingapi.springboot.authorization.filter.SkipAuthorizationFilter;
import com.codingapi.springboot.authorization.handler.Condition;
-import com.codingapi.springboot.authorization.handler.RowHandler;
import com.codingapi.springboot.authorization.interceptor.SQLRunningContext;
import com.codingapi.springboot.authorization.mask.ColumnMaskContext;
import com.codingapi.springboot.authorization.mask.impl.BankCardMask;
@@ -390,4 +389,80 @@ public String filter(String sql) {
}
+
+ @Test
+ @Order(6)
+ void test6() throws Exception{
+
+ unitRepository.deleteAll();
+ departRepository.deleteAll();
+ userRepository.deleteAll();
+
+ Unit rootUnit = new Unit("Coding总公司");
+ unitRepository.save(rootUnit);
+
+ Unit sdUnit = new Unit("Coding山东分公司", rootUnit.getId());
+ unitRepository.save(sdUnit);
+
+ Depart jgbDepart = new Depart("Coding架构部", rootUnit.getId());
+ departRepository.save(jgbDepart);
+
+ Depart xmbDepart = new Depart("Coding项目部", sdUnit.getId());
+ departRepository.save(xmbDepart);
+
+ User lorne = new User("lorne", LocalDate.parse("1991-01-01"), "beijing", "110105199003078999", "13812345678", jgbDepart);
+ User bob = new User("bob", LocalDate.parse("1991-01-01"), "beijing", "110105199003078999", "13812345678", xmbDepart);
+ User tom = new User("tom", LocalDate.parse("1991-01-01"), "beijing", "110105199003078999", "13812345678", xmbDepart);
+
+ userRepository.save(lorne);
+ userRepository.save(bob);
+ userRepository.save(tom);
+
+ String sql = "select u.* from t_user u where u.phone like '%1%' and u.id > 1 and u.depart_id in (select d.id from t_depart d where d.id > 0) ";
+
+ DataAuthorizationContext.getInstance().clearDataAuthorizationFilters();
+ DataAuthorizationContext.getInstance().addDataAuthorizationFilter(new DefaultDataAuthorizationFilter() {
+ @Override
+ public Condition rowAuthorization(String tableName, String tableAlias) {
+ String conditionTemplate = "%s.id > -100 ";
+ Condition condition = new Condition();
+ condition.addDynamicSQL(new WhereConditionSQL(conditionTemplate, tableAlias));
+ condition.addDynamicSQL(new JoinConditionSQL(
+ JoinConditionSQL.Type.INNER,
+ "t_unit",
+ "u",
+ "u.id = "+tableAlias+".id"));
+ return condition;
+ }
+
+ @Override
+ public T columnAuthorization(String tableName, String columnName, T value) {
+ System.out.println("tableName:" + tableName + ",columnName:" + columnName + ",value:" + value);
+ return value;
+ }
+
+ @Override
+ public boolean supportColumnAuthorization(String tableName, String columnName, Object value) {
+ if ("t_depart".equalsIgnoreCase(tableName)) {
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public boolean supportRowAuthorization(String tableName, String tableAlias) {
+ if ("t_depart".equalsIgnoreCase(tableName)) {
+ return true;
+ }
+ return false;
+ }
+ });
+
+ List