ShieldContextHolder.java
3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package cn.com.glsx.auth.utils;
import cn.com.glsx.auth.model.Department;
import cn.com.glsx.auth.model.Role;
import cn.com.glsx.auth.model.SyntheticUser;
import cn.com.glsx.auth.model.Tenant;
import lombok.NoArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @author payu
*/
@NoArgsConstructor
public class ShieldContextHolder {
public static final Long ADMIN_ROLE_ID = 1L;
private static final ThreadLocal<SyntheticUser> USER_THREAD_LOCAL = new ThreadLocal<>();
// 获取当前用户
public static SyntheticUser getUser() {
SyntheticUser user = USER_THREAD_LOCAL.get();
Assert.notNull(user, "获取当前用户信息失败");
return user;
}
// 设置当前用户
public static void setUser(SyntheticUser user) {
USER_THREAD_LOCAL.set(user);
}
// 移除当前用户
public static void removeUser() {
USER_THREAD_LOCAL.remove();
}
public static boolean isSuperAdmin() {
return getUser().getIsSupperAdmin();
}
public static Long getUserId() {
return getUser().getUserId();
}
public static String getAccount() {
return getUser().getAccount();
}
public static String getUsername() {
return getUser().getUsername();
}
// public static UserGroup getUserGroup() {
// SyntheticUser user = getUser();
// return user.getUserGroup();
// }
public static Tenant getTenant() {
SyntheticUser user = getUser();
Tenant tenant = user.getTenant();
Assert.notNull(tenant, "获取当前用户租户信息失败");
return tenant;
}
public static Department getDepartment() {
SyntheticUser user = getUser();
Department dept = user.getDepartment();
Assert.notNull(dept, "获取当前用户部门信息失败");
return dept;
}
public static Long getTenantId() {
return getTenant().getTenantId();
}
public static Long getDepartmentId() {
return getDepartment().getDeptId();
}
public static List<Long> getRoleIds() {
List<Role> roleList = getUser().getRoles();
if (CollectionUtils.isNotEmpty(roleList)) {
return roleList.stream().map(Role::getRoleId).collect(Collectors.toList());
}
return new ArrayList<>();
}
/**
* 获取当前用户角色数据权限可见的租户id
*
* @return
*/
public static Set<Long> getVisibleTenantIds() {
return getUser().getVisibleTenantIds();
}
/**
* 获取当前用户角色数据权限可见的部门id
*
* @return
*/
public static Set<Long> getVisibleDeptIds() {
return getUser().getVisibleDeptIds();
}
/**
* 获取当前用户角色数据权限可见的创建人id
*
* @return
*/
public static Set<Long> getVisibleCreatorIds() {
return getUser().getVisibleCreatorIds();
}
public static Role getRole() {
Role role = getUser().getRoles().get(0);
Assert.notNull(role, "获取当前用户角色信息失败");
return role;
}
public static Long getRoleId() {
return getRole().getRoleId();
}
public static Integer getRoleVisibility() {
return getRole().getRoleVisibility();
}
public static Integer getRolePermissionType() {
return getRole().getRolePermissionType();
}
}