提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.datapermission.core.rule; |
H |
2 |
|
|
3 |
import cn.hutool.core.collection.CollUtil; |
|
4 |
import cn.hutool.core.util.ArrayUtil; |
|
5 |
import com.iailab.framework.datapermission.core.annotation.DataPermission; |
|
6 |
import com.iailab.framework.datapermission.core.aop.DataPermissionContextHolder; |
|
7 |
import lombok.RequiredArgsConstructor; |
|
8 |
|
|
9 |
import java.util.Collections; |
|
10 |
import java.util.List; |
|
11 |
import java.util.stream.Collectors; |
|
12 |
|
|
13 |
/** |
|
14 |
* 默认的 DataPermissionRuleFactoryImpl 实现类 |
|
15 |
* 支持通过 {@link DataPermissionContextHolder} 过滤数据权限 |
|
16 |
* |
|
17 |
* @author iailab |
|
18 |
*/ |
|
19 |
@RequiredArgsConstructor |
|
20 |
public class DataPermissionRuleFactoryImpl implements DataPermissionRuleFactory { |
|
21 |
|
|
22 |
/** |
|
23 |
* 数据权限规则数组 |
|
24 |
*/ |
|
25 |
private final List<DataPermissionRule> rules; |
|
26 |
|
|
27 |
@Override |
|
28 |
public List<DataPermissionRule> getDataPermissionRules() { |
|
29 |
return rules; |
|
30 |
} |
|
31 |
|
|
32 |
@Override // mappedStatementId 参数,暂时没有用。以后,可以基于 mappedStatementId + DataPermission 进行缓存 |
|
33 |
public List<DataPermissionRule> getDataPermissionRule(String mappedStatementId) { |
|
34 |
// 1. 无数据权限 |
|
35 |
if (CollUtil.isEmpty(rules)) { |
|
36 |
return Collections.emptyList(); |
|
37 |
} |
|
38 |
// 2. 未配置,则默认开启 |
|
39 |
DataPermission dataPermission = DataPermissionContextHolder.get(); |
|
40 |
if (dataPermission == null) { |
|
41 |
return rules; |
|
42 |
} |
|
43 |
// 3. 已配置,但禁用 |
|
44 |
if (!dataPermission.enable()) { |
|
45 |
return Collections.emptyList(); |
|
46 |
} |
|
47 |
|
|
48 |
// 4. 已配置,只选择部分规则 |
|
49 |
if (ArrayUtil.isNotEmpty(dataPermission.includeRules())) { |
|
50 |
return rules.stream().filter(rule -> ArrayUtil.contains(dataPermission.includeRules(), rule.getClass())) |
|
51 |
.collect(Collectors.toList()); // 一般规则不会太多,所以不采用 HashSet 查询 |
|
52 |
} |
|
53 |
// 5. 已配置,只排除部分规则 |
|
54 |
if (ArrayUtil.isNotEmpty(dataPermission.excludeRules())) { |
|
55 |
return rules.stream().filter(rule -> !ArrayUtil.contains(dataPermission.excludeRules(), rule.getClass())) |
|
56 |
.collect(Collectors.toList()); // 一般规则不会太多,所以不采用 HashSet 查询 |
|
57 |
} |
|
58 |
// 6. 已配置,全部规则 |
|
59 |
return rules; |
|
60 |
} |
|
61 |
|
|
62 |
} |