inleft
2022-02-09 9bcb19959eeb9da9bde2561e7278f6d0a55eb151
commit | author | age
9bcb19 1 /*
I 2 Copyright [2020] [https://www.xiaonuo.vip]
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
17
18 1.请不要删除和修改根目录下的LICENSE文件。
19 2.请不要删除和修改Snowy源码头部的版权声明。
20 3.请保留源码和相关描述文件的项目出处,作者声明等。
21 4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy
22 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy
23 6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
24  */
25 package vip.xiaonuo.sys.modular.auth.context;
26
27 import cn.hutool.core.collection.CollectionUtil;
28 import cn.hutool.core.util.ObjectUtil;
29 import cn.hutool.core.util.StrUtil;
30 import org.springframework.security.authentication.AnonymousAuthenticationToken;
31 import org.springframework.security.core.Authentication;
32 import org.springframework.security.core.context.SecurityContextHolder;
33 import org.springframework.stereotype.Component;
34 import vip.xiaonuo.core.consts.CommonConstant;
35 import vip.xiaonuo.core.consts.SymbolConstant;
36 import vip.xiaonuo.core.context.login.LoginContext;
37 import vip.xiaonuo.core.exception.AuthException;
38 import vip.xiaonuo.core.exception.enums.AuthExceptionEnum;
39 import vip.xiaonuo.core.pojo.login.LoginEmpInfo;
40 import vip.xiaonuo.core.pojo.login.SysLoginUser;
41 import vip.xiaonuo.sys.core.enums.AdminTypeEnum;
42 import vip.xiaonuo.sys.modular.auth.service.AuthService;
43 import vip.xiaonuo.sys.modular.user.entity.SysUser;
44 import vip.xiaonuo.sys.modular.user.service.SysUserService;
45
46 import javax.annotation.Resource;
47 import java.util.List;
48
49 /**
50  * 登录用户上下文实现类
51  *
52  * @author xuyuxiang
53  * @date 2020/3/13 12:19
54  */
55 @Component
56 public class LoginContextSpringSecurityImpl implements LoginContext {
57
58     @Resource
59     private AuthService authService;
60
61     @Resource
62     private SysUserService sysUserService;
63
64     private LoginContextSpringSecurityImpl() {
65
66     }
67
68     /**
69      * 获取当前登录用户
70      *
71      * @author xuyuxiang
72      * @date 2020/3/13 14:42
73      */
74     @Override
75     public SysLoginUser getSysLoginUser() {
76         Authentication authentication = authService.getAuthentication();
77         if (ObjectUtil.isEmpty(authentication) || authentication.getPrincipal() instanceof String) {
78             throw new AuthException(AuthExceptionEnum.NO_LOGIN_USER);
79         } else {
80             return (SysLoginUser) authentication.getPrincipal();
81         }
82     }
83
84     /**
85      * 获取当前登录用户,如未登录,则返回null,不抛异常
86      *
87      * @author xuyuxiang
88      * @date 2020/3/13 14:42
89      */
90     @Override
91     public SysLoginUser getSysLoginUserWithoutException() {
92         Authentication authentication = authService.getAuthentication();
93         if (ObjectUtil.isEmpty(authentication) || authentication.getPrincipal() instanceof String) {
94             return null;
95         } else {
96             return (SysLoginUser) authentication.getPrincipal();
97         }
98     }
99
100     /**
101      * 获取当前登录用户的id
102      *
103      * @author xuyuxiang
104      * @date 2020/3/18 19:26
105      */
106     @Override
107     public Long getSysLoginUserId() {
108         return this.getSysLoginUser().getId();
109     }
110
111     /**
112      * 判断用户是否登录
113      *
114      * @author xuyuxiang
115      * @date 2020/3/18 19:23
116      */
117     @Override
118     public boolean hasLogin() {
119         Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
120         if (ObjectUtil.isEmpty(authentication) || authentication.getPrincipal() instanceof String) {
121             return false;
122         } else {
123             return !(authentication instanceof AnonymousAuthenticationToken);
124         }
125     }
126
127     /**
128      * 获取当前登录的用户账号
129      *
130      * @author xuyuxiang
131      * @date 2020/3/23 8:49
132      */
133     @Override
134     public String getSysLoginUserAccount() {
135         return this.getSysLoginUser().getAccount();
136     }
137
138     /**
139      * 判断当前登录用户是否有某资源的访问权限
140      *
141      * @author xuyuxiang
142      * @date 2020/3/23 8:49
143      */
144     @Override
145     public boolean hasPermission(String requestUri) {
146         String removePrefix = StrUtil.removePrefix(requestUri, SymbolConstant.LEFT_DIVIDE);
147         String requestPermission = removePrefix.replaceAll(SymbolConstant.LEFT_DIVIDE, SymbolConstant.COLON);
148         return this.getSysLoginUser().getPermissions().contains(requestPermission);
149     }
150
151     /**
152      * 判断当前登录用户是否包含某个角色
153      *
154      * @author xuyuxiang
155      * @date 2020/3/23 8:55
156      */
157     @Override
158     public boolean hasRole(String roleCode) {
159         List<String> roleCodeList = this.getLoginUserRoleCodeList();
160         return roleCodeList.contains(roleCode);
161     }
162
163     /**
164      * 判断当前登录用户是否包含任意一个角色
165      *
166      * @author xuyuxiang
167      * @date 2020/3/23 8:55
168      */
169     @Override
170     public boolean hasAnyRole(String roleCodes) {
171         boolean flag = false;
172         List<String> loginUserRoleCodeList = this.getLoginUserRoleCodeList();
173         String[] roleCodeArr = StrUtil.split(roleCodes, SymbolConstant.COMMA);
174         for (String roleCode : roleCodeArr) {
175             if (loginUserRoleCodeList.contains(roleCode)) {
176                 flag = true;
177                 break;
178             }
179         }
180         return flag;
181     }
182
183     /**
184      * 管理员类型(0超级管理员 1非管理员)
185      * 判断当前登录用户是否是超级管理员
186      *
187      * @author xuyuxiang
188      * @date 2020/3/23 17:51
189      */
190     @Override
191     public boolean isSuperAdmin() {
192         return this.isAdmin(AdminTypeEnum.SUPER_ADMIN.getCode());
193     }
194
195     /**
196      * 判断当前登录用户是否包含所有角色
197      *
198      * @author xuyuxiang
199      * @date 2020/4/5 10:28
200      */
201     @Override
202     public boolean hasAllRole(String roleCodes) {
203         boolean flag = true;
204         List<String> loginUserRoleCodeList = this.getLoginUserRoleCodeList();
205         String[] roleCodeArr = StrUtil.split(roleCodes, SymbolConstant.COMMA);
206         for (String roleCode : roleCodeArr) {
207             if (!loginUserRoleCodeList.contains(roleCode)) {
208                 flag = false;
209                 break;
210             }
211         }
212         return flag;
213     }
214
215     /**
216      * 判断当前登录用户是否是指定类型的管理员
217      *
218      * @author xuyuxiang
219      * @date 2020/4/5 11:43
220      */
221     private boolean isAdmin(Integer adminTypeCode) {
222         Integer adminType = this.getSysLoginUser().getAdminType();
223         boolean flag = false;
224         if (adminType.equals(adminTypeCode)) {
225             flag = true;
226         }
227         return flag;
228     }
229
230     /**
231      * 当前登录用户的数据范围(组织机构id集合)
232      *
233      * @author xuyuxiang
234      * @date 2020/4/5 17:20
235      */
236     @Override
237     public List<Long> getLoginUserDataScopeIdList() {
238         return this.getSysLoginUser().getDataScopes();
239     }
240
241     /**
242      * 获取当前登录用户的组织机构id集合
243      *
244      * @author xuyuxiang
245      * @date 2020/4/5 18:32
246      */
247     @Override
248     public Long getSysLoginUserOrgId() {
249         LoginEmpInfo loginEmpInfo = this.getSysLoginUser().getLoginEmpInfo();
250         if (ObjectUtil.isNotNull(loginEmpInfo)) {
251             if (ObjectUtil.isNotEmpty(loginEmpInfo.getOrgId())) {
252                 return loginEmpInfo.getOrgId();
253             }
254         }
255         return null;
256     }
257
258     /**
259      * 获取当前登录用户的角色id集合
260      *
261      * @author xuyuxiang
262      * @date 2020/4/20 16:04
263      */
264     @Override
265     public List<String> getLoginUserRoleIds() {
266         List<String> resultList = CollectionUtil.newArrayList();
267         this.getSysLoginUser().getRoles().forEach(dict -> resultList.add(dict.getStr(CommonConstant.ID)));
268         return resultList;
269     }
270
271     @Override
272     public SysLoginUser getSysLoginUserUpToDate() {
273         SysLoginUser sysLoginUser = this.getSysLoginUser();
274         Long loginUserId = sysLoginUser.getId();
275         SysUser sysUser = sysUserService.getById(loginUserId);
276         //构造SysLoginUser
277         return authService.genSysLoginUser(sysUser);
278     }
279
280     /**
281      * 获取当前用户的角色编码集合
282      *
283      * @author xuyuxiang
284      * @date 2020/3/23 8:58
285      */
286     private List<String> getLoginUserRoleCodeList() {
287         List<String> roleCodeList = CollectionUtil.newArrayList();
288         this.getSysLoginUser().getRoles().forEach(dict -> roleCodeList.add(dict.getStr(CommonConstant.CODE)));
289         return roleCodeList;
290     }
291 }