提交 | 用户 | 时间
|
a6de49
|
1 |
package com.iailab.framework.datapermission.core.db; |
H |
2 |
|
|
3 |
import cn.hutool.core.collection.CollUtil; |
|
4 |
import com.baomidou.mybatisplus.extension.plugins.handler.MultiDataPermissionHandler; |
|
5 |
import com.iailab.framework.datapermission.core.rule.DataPermissionRule; |
|
6 |
import com.iailab.framework.datapermission.core.rule.DataPermissionRuleFactory; |
|
7 |
import com.iailab.framework.mybatis.core.util.MyBatisUtils; |
|
8 |
import lombok.RequiredArgsConstructor; |
|
9 |
import net.sf.jsqlparser.expression.Expression; |
|
10 |
import net.sf.jsqlparser.expression.operators.conditional.AndExpression; |
|
11 |
import net.sf.jsqlparser.schema.Table; |
|
12 |
|
|
13 |
import java.util.List; |
|
14 |
|
|
15 |
/** |
|
16 |
* 基于 {@link DataPermissionRule} 的数据权限处理器 |
|
17 |
* |
|
18 |
* 它的底层,是基于 MyBatis Plus 的 <a href="https://baomidou.com/plugins/data-permission/">数据权限插件</a> |
|
19 |
* 核心原理:它会在 SQL 执行前拦截 SQL 语句,并根据用户权限动态添加权限相关的 SQL 片段。这样,只有用户有权限访问的数据才会被查询出来 |
|
20 |
* |
4d4165
|
21 |
* @author iailab |
a6de49
|
22 |
*/ |
H |
23 |
@RequiredArgsConstructor |
|
24 |
public class DataPermissionRuleHandler implements MultiDataPermissionHandler { |
|
25 |
|
|
26 |
private final DataPermissionRuleFactory ruleFactory; |
|
27 |
|
|
28 |
@Override |
|
29 |
public Expression getSqlSegment(Table table, Expression where, String mappedStatementId) { |
|
30 |
// 获得 Mapper 对应的数据权限的规则 |
|
31 |
List<DataPermissionRule> rules = ruleFactory.getDataPermissionRule(mappedStatementId); |
|
32 |
if (CollUtil.isEmpty(rules)) { |
|
33 |
return null; |
|
34 |
} |
|
35 |
|
|
36 |
// 生成条件 |
|
37 |
Expression allExpression = null; |
|
38 |
for (DataPermissionRule rule : rules) { |
|
39 |
// 判断表名是否匹配 |
|
40 |
String tableName = MyBatisUtils.getTableName(table); |
|
41 |
if (!rule.getTableNames().contains(tableName)) { |
|
42 |
continue; |
|
43 |
} |
|
44 |
|
|
45 |
// 单条规则的条件 |
|
46 |
Expression oneExpress = rule.getExpression(tableName, table.getAlias()); |
|
47 |
if (oneExpress == null) { |
|
48 |
continue; |
|
49 |
} |
|
50 |
// 拼接到 allExpression 中 |
|
51 |
allExpression = allExpression == null ? oneExpress |
|
52 |
: new AndExpression(allExpression, oneExpress); |
|
53 |
} |
|
54 |
return allExpression; |
|
55 |
} |
|
56 |
|
|
57 |
} |