Skip to content

Commit 155c10b

Browse files
committed
1 parent 22d7a08 commit 155c10b

File tree

10 files changed

+93
-83
lines changed

10 files changed

+93
-83
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
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.10.18</version>
18+
<version>2.10.19</version>
1919

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

springboot-starter-data-authorization/pom.xml

Lines changed: 1 addition & 1 deletion
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.10.18</version>
9+
<version>2.10.19</version>
1010
</parent>
1111

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

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.codingapi.springboot.authorization.handler.RowHandler;
66
import net.sf.jsqlparser.expression.Expression;
77
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
8+
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
9+
import net.sf.jsqlparser.expression.operators.relational.InExpression;
810
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
911
import net.sf.jsqlparser.schema.Table;
1012
import net.sf.jsqlparser.statement.Statement;
@@ -100,6 +102,49 @@ private void enhanceDataPermissionInSelect(PlainSelect plainSelect) throws Excep
100102
}
101103
}
102104
}
105+
106+
Expression expression = plainSelect.getWhere();
107+
this.handlerSubSelect(expression);
108+
}
109+
110+
private void handlerSubSelect(Expression expression) throws Exception {
111+
if(expression!=null){
112+
if(expression instanceof AndExpression){
113+
AndExpression andExpression = (AndExpression) expression;
114+
Expression leftExpression = andExpression.getLeftExpression();
115+
Expression rightExpression = andExpression.getRightExpression();
116+
117+
this.handlerSubSelect(leftExpression);
118+
this.handlerSubSelect(rightExpression);
119+
120+
}
121+
if(expression instanceof OrExpression){
122+
OrExpression orExpression = (OrExpression) expression;
123+
Expression leftExpression = orExpression.getLeftExpression();
124+
Expression rightExpression = orExpression.getRightExpression();
125+
126+
this.handlerSubSelect(leftExpression);
127+
this.handlerSubSelect(rightExpression);
128+
}
129+
130+
if(expression instanceof InExpression){
131+
InExpression inExpression = (InExpression) expression;
132+
Expression leftExpression = inExpression.getLeftExpression();
133+
Expression rightExpression = inExpression.getRightExpression();
134+
135+
this.handlerSubSelect(leftExpression);
136+
this.handlerSubSelect(rightExpression);
137+
}
138+
139+
if(expression instanceof ParenthesedSelect){
140+
ParenthesedSelect parenthesedSelect = (ParenthesedSelect) expression;
141+
this.enhanceDataPermissionInSelect(parenthesedSelect.getPlainSelect());
142+
}
143+
144+
if(expression instanceof PlainSelect){
145+
this.enhanceDataPermissionInSelect((PlainSelect) expression);
146+
}
147+
}
103148
}
104149

105150

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

Lines changed: 39 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package com.codingapi.springboot.authorization;
22

33
import com.codingapi.springboot.authorization.current.CurrentUser;
4-
import com.codingapi.springboot.authorization.enhancer.DataPermissionSQLEnhancer;
54
import com.codingapi.springboot.authorization.entity.Depart;
65
import com.codingapi.springboot.authorization.entity.Unit;
76
import com.codingapi.springboot.authorization.entity.User;
87
import com.codingapi.springboot.authorization.filter.DefaultDataAuthorizationFilter;
98
import com.codingapi.springboot.authorization.handler.Condition;
10-
import com.codingapi.springboot.authorization.handler.RowHandler;
119
import com.codingapi.springboot.authorization.interceptor.SQLRunningContext;
1210
import com.codingapi.springboot.authorization.mask.ColumnMaskContext;
1311
import com.codingapi.springboot.authorization.mask.impl.BankCardMask;
@@ -17,14 +15,6 @@
1715
import com.codingapi.springboot.authorization.repository.UnitRepository;
1816
import com.codingapi.springboot.authorization.repository.UserRepository;
1917
import lombok.extern.slf4j.Slf4j;
20-
import net.sf.jsqlparser.expression.Expression;
21-
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
22-
import net.sf.jsqlparser.schema.Column;
23-
import net.sf.jsqlparser.statement.Statement;
24-
import net.sf.jsqlparser.statement.select.PlainSelect;
25-
import net.sf.jsqlparser.statement.select.Select;
26-
import net.sf.jsqlparser.statement.select.SelectItem;
27-
import net.sf.jsqlparser.statement.select.SelectItemVisitor;
2818
import org.junit.jupiter.api.MethodOrderer;
2919
import org.junit.jupiter.api.Order;
3020
import org.junit.jupiter.api.Test;
@@ -37,7 +27,6 @@
3727
import org.springframework.test.annotation.Rollback;
3828

3929
import java.time.LocalDate;
40-
import java.util.HashMap;
4130
import java.util.List;
4231
import java.util.Map;
4332

@@ -280,74 +269,43 @@ public boolean supportColumnAuthorization(String tableName, String columnName, O
280269

281270
}
282271

283-
284-
// @Test
272+
@Test
285273
@Order(4)
286-
void test4() throws Exception{
287-
String sql = "SELECT\n" +
288-
"\tt.* \n" +
289-
"FROM\n" +
290-
"\t(\n" +
291-
"\t\tSELECT\n" +
292-
"\t\t\tUNYiV.id AS '历史工作经历编号',\n" +
293-
"\t\t\tUNYiV.company_name AS '历史工作单位',\n" +
294-
"\t\t\tUNYiV.depart_name AS '历史工作部门',\n" +
295-
"\t\t\tUNYiV.post_name AS '历史工作岗位',\n" +
296-
"\t\t\tUNYiV.start_date AS '开始时间',\n" +
297-
"\t\t\tUNYiV.end_date AS '结束时间',\n" +
298-
"\t\t\towasH.员工编号 AS '员工编号',\n" +
299-
"\t\t\towasH.员工姓名 AS '员工姓名',\n" +
300-
"\t\t\towasH.员工生日 AS '员工生日',\n" +
301-
"\t\t\towasH.员工地址 AS '员工地址',\n" +
302-
"\t\t\towasH.身份证号码 AS '身份证号码',\n" +
303-
"\t\t\towasH.手机号 AS '手机号',\n" +
304-
"\t\t\towasH.部门编号 AS '部门编号',\n" +
305-
"\t\t\towasH.岗位编号 AS '岗位编号',\n" +
306-
"\t\t\towasH.任现职编号 AS '任现职编号',\n" +
307-
"\t\t\towasH.社团编号 AS '社团编号',\n" +
308-
"\t\t\towasH.社团名称 AS '社团名称',\n" +
309-
"\t\t\towasH.创建时间 AS '创建时间' \n" +
310-
"\t\tFROM\n" +
311-
"\t\t\tt_work AS pehMS,\n" +
312-
"\t\t\tt_employee AS OGwG7,\n" +
313-
"\t\t\tt_work_history AS UNYiV,\n" +
314-
"\t\t\t(\n" +
315-
"\t\t\t\tSELECT\n" +
316-
"\t\t\t\t\tWXJj8.id AS '员工编号',\n" +
317-
"\t\t\t\t\tWXJj8.NAME AS '员工姓名',\n" +
318-
"\t\t\t\t\tWXJj8.birth_date AS '员工生日',\n" +
319-
"\t\t\t\t\tWXJj8.address AS '员工地址',\n" +
320-
"\t\t\t\t\tWXJj8.id_card AS '身份证号码',\n" +
321-
"\t\t\t\t\tWXJj8.phone AS '手机号',\n" +
322-
"\t\t\t\t\tWXJj8.depart_id AS '部门编号',\n" +
323-
"\t\t\t\t\tWXJj8.post_id AS '岗位编号',\n" +
324-
"\t\t\t\t\tWXJj8.work_id AS '任现职编号',\n" +
325-
"\t\t\t\t\trnGD4.id AS '社团编号',\n" +
326-
"\t\t\t\t\trnGD4.NAME AS '社团名称',\n" +
327-
"\t\t\t\t\trnGD4.create_date AS '创建时间' \n" +
328-
"\t\t\t\tFROM\n" +
329-
"\t\t\t\t\tt_employee AS WXJj8,\n" +
330-
"\t\t\t\t\tt_league_employee AS dEj96,\n" +
331-
"\t\t\t\t\tt_league AS rnGD4 \n" +
332-
"\t\t\t\tWHERE\n" +
333-
"\t\t\t\t\trnGD4.id < 100 \n" +
334-
"\t\t\t\t\tAND dEj96.employee_id = WXJj8.id \n" +
335-
"\t\t\t\t\tAND dEj96.league_id = rnGD4.id \n" +
336-
"\t\t\t\t\tAND 1 = 1 \n" +
337-
"\t\t\t) AS owasH \n" +
338-
"\t\tWHERE\n" +
339-
"\t\t\tUNYiV.employee_id = OGwG7.id \n" +
340-
"\t\t\tAND OGwG7.work_id = pehMS.id \n" +
341-
"\t\t\tAND owasH.任现职编号 = pehMS.id \n" +
342-
"\t\t\tAND 1 = 1 \n" +
343-
"\t) AS t , t_employee AS e where t.员工编号 = e.id and e.id = 1";
274+
void test4() throws Exception {
275+
276+
unitRepository.deleteAll();
277+
departRepository.deleteAll();
278+
userRepository.deleteAll();
279+
280+
Unit rootUnit = new Unit("Coding总公司");
281+
unitRepository.save(rootUnit);
282+
283+
Unit sdUnit = new Unit("Coding山东分公司", rootUnit.getId());
284+
unitRepository.save(sdUnit);
285+
286+
Depart jgbDepart = new Depart("Coding架构部", rootUnit.getId());
287+
departRepository.save(jgbDepart);
288+
289+
Depart xmbDepart = new Depart("Coding项目部", sdUnit.getId());
290+
departRepository.save(xmbDepart);
291+
292+
User lorne = new User("lorne", LocalDate.parse("1991-01-01"), "beijing", "110105199003078999", "13812345678", jgbDepart);
293+
User bob = new User("bob", LocalDate.parse("1991-01-01"), "beijing", "110105199003078999", "13812345678", xmbDepart);
294+
User tom = new User("tom", LocalDate.parse("1991-01-01"), "beijing", "110105199003078999", "13812345678", xmbDepart);
295+
296+
userRepository.save(lorne);
297+
userRepository.save(bob);
298+
userRepository.save(tom);
299+
300+
String sql = "select * from t_user where phone like '%1%' and id > 1 and depart_id in (select id from t_depart where id > 0)";
344301

345302

346303
DataAuthorizationContext.getInstance().clearDataAuthorizationFilters();
347304
DataAuthorizationContext.getInstance().addDataAuthorizationFilter(new DefaultDataAuthorizationFilter() {
348305
@Override
349306
public Condition rowAuthorization(String tableName, String tableAlias) {
350-
return super.rowAuthorization(tableName, tableAlias);
307+
String conditionTemplate = "%s.id > -100 ";
308+
return Condition.formatCondition(conditionTemplate, tableAlias);
351309
}
352310

353311
@Override
@@ -358,18 +316,23 @@ public <T> T columnAuthorization(String tableName, String columnName, T value) {
358316

359317
@Override
360318
public boolean supportColumnAuthorization(String tableName, String columnName, Object value) {
361-
return true;
319+
if ("t_depart".equalsIgnoreCase(tableName)) {
320+
return true;
321+
}
322+
return false;
362323
}
363324

364325
@Override
365326
public boolean supportRowAuthorization(String tableName, String tableAlias) {
366-
return true;
327+
if ("t_depart".equalsIgnoreCase(tableName)) {
328+
return true;
329+
}
330+
return false;
367331
}
368332
});
369333

370-
371334
List<Map<String, Object>> data = jdbcTemplate.queryForList(sql);
372-
// System.out.println(data);
335+
System.out.println(data);
373336
}
374337

375338

springboot-starter-data-authorization/src/test/resources/application.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ spring.jpa.show-sql=true
1111
#spring.datasource.password=lorne4j#2024
1212

1313
logging.level.com.codingapi.springboot.authorization=debug
14+
15+
codingapi.data-authorization.show-sql=true

springboot-starter-data-fast/pom.xml

Lines changed: 1 addition & 1 deletion
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.10.18</version>
8+
<version>2.10.19</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

springboot-starter-flow/pom.xml

Lines changed: 1 addition & 1 deletion
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.10.18</version>
9+
<version>2.10.19</version>
1010
</parent>
1111

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

springboot-starter-security/pom.xml

Lines changed: 1 addition & 1 deletion
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.10.18</version>
9+
<version>2.10.19</version>
1010
</parent>
1111

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

springboot-starter/pom.xml

Lines changed: 1 addition & 1 deletion
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.10.18</version>
8+
<version>2.10.19</version>
99
</parent>
1010
<artifactId>springboot-starter</artifactId>
1111

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
------------------------------------------------------
2-
CodingApi SpringBoot-Starter 2.10.18
2+
CodingApi SpringBoot-Starter 2.10.19
33
springboot version (${spring-boot.version})
44
------------------------------------------------------

0 commit comments

Comments
 (0)