提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.datapermission.core.util; |
H |
2 |
|
|
3 |
import com.iailab.framework.datapermission.core.annotation.DataPermission; |
|
4 |
import com.iailab.framework.datapermission.core.aop.DataPermissionContextHolder; |
|
5 |
import lombok.SneakyThrows; |
|
6 |
|
|
7 |
import java.util.concurrent.Callable; |
|
8 |
|
|
9 |
/** |
|
10 |
* 数据权限 Util |
|
11 |
* |
|
12 |
* @author iailab |
|
13 |
*/ |
|
14 |
public class DataPermissionUtils { |
|
15 |
|
|
16 |
private static DataPermission DATA_PERMISSION_DISABLE; |
|
17 |
|
|
18 |
@DataPermission(enable = false) |
|
19 |
@SneakyThrows |
|
20 |
private static DataPermission getDisableDataPermissionDisable() { |
|
21 |
if (DATA_PERMISSION_DISABLE == null) { |
|
22 |
DATA_PERMISSION_DISABLE = DataPermissionUtils.class |
|
23 |
.getDeclaredMethod("getDisableDataPermissionDisable") |
|
24 |
.getAnnotation(DataPermission.class); |
|
25 |
} |
|
26 |
return DATA_PERMISSION_DISABLE; |
|
27 |
} |
|
28 |
|
|
29 |
/** |
|
30 |
* 忽略数据权限,执行对应的逻辑 |
|
31 |
* |
|
32 |
* @param runnable 逻辑 |
|
33 |
*/ |
|
34 |
public static void executeIgnore(Runnable runnable) { |
|
35 |
DataPermission dataPermission = getDisableDataPermissionDisable(); |
|
36 |
DataPermissionContextHolder.add(dataPermission); |
|
37 |
try { |
|
38 |
// 执行 runnable |
|
39 |
runnable.run(); |
|
40 |
} finally { |
|
41 |
DataPermissionContextHolder.remove(); |
|
42 |
} |
|
43 |
} |
|
44 |
|
|
45 |
/** |
|
46 |
* 忽略数据权限,执行对应的逻辑 |
|
47 |
* |
|
48 |
* @param callable 逻辑 |
|
49 |
* @return 执行结果 |
|
50 |
*/ |
|
51 |
@SneakyThrows |
|
52 |
public static <T> T executeIgnore(Callable<T> callable) { |
|
53 |
DataPermission dataPermission = getDisableDataPermissionDisable(); |
|
54 |
DataPermissionContextHolder.add(dataPermission); |
|
55 |
try { |
|
56 |
// 执行 callable |
|
57 |
return callable.call(); |
|
58 |
} finally { |
|
59 |
DataPermissionContextHolder.remove(); |
|
60 |
} |
|
61 |
} |
|
62 |
|
|
63 |
} |