Skip to content

Commit c9524ce

Browse files
committed
update 2.9.23
data authorization support union all sql
1 parent ab0d896 commit c9524ce

File tree

10 files changed

+68
-16
lines changed

10 files changed

+68
-16
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
<groupId>com.codingapi.springboot</groupId>
1717
<artifactId>springboot-parent</artifactId>
18-
<version>2.9.22</version>
18+
<version>2.9.23</version>
1919

2020
<url>https://github.com/codingapi/springboot-framewrok</url>
2121
<name>springboot-parent</name>

springboot-starter-data-authorization/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>springboot-parent</artifactId>
88
<groupId>com.codingapi.springboot</groupId>
9-
<version>2.9.22</version>
9+
<version>2.9.23</version>
1010
</parent>
1111

1212
<artifactId>springboot-starter-data-authorization</artifactId>

springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/DataPermissionSQLEnhancer.java

+18-6
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
99
import net.sf.jsqlparser.schema.Table;
1010
import net.sf.jsqlparser.statement.Statement;
11-
import net.sf.jsqlparser.statement.select.FromItem;
12-
import net.sf.jsqlparser.statement.select.Join;
13-
import net.sf.jsqlparser.statement.select.PlainSelect;
14-
import net.sf.jsqlparser.statement.select.Select;
11+
import net.sf.jsqlparser.statement.select.*;
1512

1613
import java.sql.SQLException;
14+
import java.util.List;
1715

1816
/**
1917
* 数据权限 SQL 增强器
@@ -45,8 +43,7 @@ public String getNewSQL() throws SQLException {
4543
if (statement instanceof Select) {
4644
tableColumnAliasHolder.holderAlias();
4745
Select select = (Select) statement;
48-
PlainSelect plainSelect = select.getPlainSelect();
49-
this.enhanceDataPermissionInSelect(plainSelect);
46+
this.deepMatch(select);
5047
return statement.toString();
5148
}
5249
} catch (Exception e) {
@@ -55,6 +52,21 @@ public String getNewSQL() throws SQLException {
5552
return sql;
5653
}
5754

55+
56+
private void deepMatch(Select select) throws Exception {
57+
if (select instanceof PlainSelect) {
58+
PlainSelect plainSelect = select.getPlainSelect();
59+
this.enhanceDataPermissionInSelect(plainSelect);
60+
}
61+
if (select instanceof SetOperationList) {
62+
SetOperationList setOperationList = select.getSetOperationList();
63+
List<Select> selectList = setOperationList.getSelects();
64+
for (Select selectItem : selectList) {
65+
this.deepMatch(selectItem.getPlainSelect());
66+
}
67+
}
68+
}
69+
5870
public TableColumnAliasContext getTableAlias() {
5971
return tableColumnAliasHolder.getAliasContext();
6072
}

springboot-starter-data-authorization/src/main/java/com/codingapi/springboot/authorization/enhancer/TableColumnAliasHolder.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,22 @@ public TableColumnAliasHolder(Statement statement) {
2929
*/
3030
public void holderAlias() {
3131
Select select = (Select) statement;
32-
PlainSelect plainSelect = select.getPlainSelect();
33-
this.searchSubSelect(null, plainSelect);
34-
aliasContext.columnKeyToMap();
32+
this.deepSearch(select);
33+
}
34+
35+
36+
private void deepSearch(Select select) {
37+
if (select instanceof PlainSelect) {
38+
PlainSelect plainSelect = select.getPlainSelect();
39+
this.searchSubSelect(null, plainSelect);
40+
aliasContext.columnKeyToMap();
41+
} else if (select instanceof SetOperationList) {
42+
SetOperationList setOperationList = select.getSetOperationList();
43+
List<Select> selectList = setOperationList.getSelects();
44+
for (Select selectItem : selectList) {
45+
this.deepSearch(selectItem);
46+
}
47+
}
3548
}
3649

3750

springboot-starter-data-authorization/src/test/java/com/codingapi/springboot/authorization/analyzer/SelectSQLAnalyzerTest.java

+27
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,31 @@ void test7() throws Exception{
195195
System.out.println(builder.getNewSQL());
196196
System.out.println(builder.getTableAlias());;
197197
}
198+
199+
200+
@Test
201+
@Order(8)
202+
void test8() throws Exception{
203+
String sql = "select ade1_0.id,ade1_0.ba_dept_name,ade1_0.ba_org_shortname,ade1_0.ba_dept_code,ade1_0.ba_code,ade1_0.ba_dept_property_code,ade1_0.ba_parent_type,ade1_0.ba_real_super_org_id,ade1_0.ba_org_is_avoidance_dept,ade1_0.ba_real_super_org_id,ade1_0.ba_super_org_name " +
204+
"from ba04_administrative_department ade1_0 where ade1_0.ba_parent_type=0" +
205+
" union all select ade2_0.id,ade2_0.ba_dept_name,ade2_0.ba_org_shortname,ade2_0.ba_dept_code,ade2_0.ba_code,ade2_0.ba_dept_property_code,ade2_0.ba_parent_type,ade2_0.ba_real_super_org_id,ade2_0.ba_org_is_avoidance_dept,ade3_0.ba_real_super_org_id,ade3_0.ba_super_org_name" +
206+
" from ba04_administrative_department ade2_0 left join ba04_administrative_department ade3_0 on ade2_0.ba_real_super_org_id=ade3_0.id " +
207+
"where ade2_0.ba_real_super_org_id=1";
208+
209+
210+
211+
RowHandler rowHandler = (subSql, tableName, tableAlias) -> {
212+
if (tableName.equalsIgnoreCase("ba04_administrative_department")) {
213+
String conditionTemplate = "%s.id < 100 ";
214+
return Condition.formatCondition(conditionTemplate, tableAlias);
215+
}
216+
return null;
217+
};
218+
DataPermissionSQLEnhancer builder = new DataPermissionSQLEnhancer(sql, rowHandler);
219+
String newSQL = builder.getNewSQL();
220+
System.out.println(newSQL);
221+
System.out.println(builder.getTableAlias());
222+
assertEquals("SELECT ade1_0.id, ade1_0.ba_dept_name, ade1_0.ba_org_shortname, ade1_0.ba_dept_code, ade1_0.ba_code, ade1_0.ba_dept_property_code, ade1_0.ba_parent_type, ade1_0.ba_real_super_org_id, ade1_0.ba_org_is_avoidance_dept, ade1_0.ba_real_super_org_id, ade1_0.ba_super_org_name FROM ba04_administrative_department ade1_0 WHERE ade1_0.id < 100 AND ade1_0.ba_parent_type = 0 " +
223+
"UNION ALL SELECT ade2_0.id, ade2_0.ba_dept_name, ade2_0.ba_org_shortname, ade2_0.ba_dept_code, ade2_0.ba_code, ade2_0.ba_dept_property_code, ade2_0.ba_parent_type, ade2_0.ba_real_super_org_id, ade2_0.ba_org_is_avoidance_dept, ade3_0.ba_real_super_org_id, ade3_0.ba_super_org_name FROM ba04_administrative_department ade2_0 LEFT JOIN ba04_administrative_department ade3_0 ON ade2_0.ba_real_super_org_id = ade3_0.id WHERE ade3_0.id < 100 AND ade2_0.id < 100 AND ade2_0.ba_real_super_org_id = 1", newSQL);
224+
}
198225
}

springboot-starter-data-fast/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-parent</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>2.9.22</version>
8+
<version>2.9.23</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

springboot-starter-flow/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>springboot-parent</artifactId>
88
<groupId>com.codingapi.springboot</groupId>
9-
<version>2.9.22</version>
9+
<version>2.9.23</version>
1010
</parent>
1111

1212
<name>springboot-starter-flow</name>

springboot-starter-security/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>springboot-parent</artifactId>
88
<groupId>com.codingapi.springboot</groupId>
9-
<version>2.9.22</version>
9+
<version>2.9.23</version>
1010
</parent>
1111

1212
<artifactId>springboot-starter-security</artifactId>

springboot-starter/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.codingapi.springboot</groupId>
77
<artifactId>springboot-parent</artifactId>
8-
<version>2.9.22</version>
8+
<version>2.9.23</version>
99
</parent>
1010
<artifactId>springboot-starter</artifactId>
1111

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
------------------------------------------------------
2-
CodingApi SpringBoot-Starter 2.9.22
2+
CodingApi SpringBoot-Starter 2.9.23
33
springboot version (${spring-boot.version})
44
------------------------------------------------------

0 commit comments

Comments
 (0)