懒羊羊
2023-08-30 71e81ed1d12e4d69f53c8ad9e066650ad4186293
提交 | 用户 | 时间
71e81e 1 package cn.stylefeng.guns.sys.core.auth;
2
3 import cn.stylefeng.guns.base.auth.context.LoginContext;
4 import cn.stylefeng.guns.base.auth.exception.AuthException;
5 import cn.stylefeng.guns.base.auth.exception.enums.AuthExceptionEnum;
6 import cn.stylefeng.guns.base.auth.model.LoginUser;
7 import cn.stylefeng.guns.base.consts.ConstantsContext;
8 import cn.stylefeng.guns.sys.core.auth.util.TokenUtil;
9 import cn.stylefeng.guns.sys.core.constant.Const;
10 import cn.stylefeng.guns.sys.core.constant.factory.ConstantFactory;
11 import org.springframework.security.authentication.AnonymousAuthenticationToken;
12 import org.springframework.security.core.Authentication;
13 import org.springframework.security.core.context.SecurityContextHolder;
14 import org.springframework.stereotype.Component;
15
16 import java.util.List;
17
18 /**
19  * 用户登录上下文
20  *
21  * @author fengshuonan
22  * @Date 2019/7/18 22:27
23  */
24 @Component
25 public class LoginContextSpringSecutiryImpl implements LoginContext {
26
27     @Override
28     public LoginUser getUser() {
29         if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof String) {
30             throw new AuthException(AuthExceptionEnum.NOT_LOGIN_ERROR);
31         } else {
32             return (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
33         }
34     }
35
36     @Override
37     public String getToken() {
38         return TokenUtil.getToken();
39     }
40
41     @Override
42     public boolean hasLogin() {
43         Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
44         if (authentication == null) {
45             return false;
46         } else {
47             if (authentication instanceof AnonymousAuthenticationToken) {
48                 return false;
49             } else {
50                 return true;
51             }
52         }
53     }
54
55     @Override
56     public Long getUserId() {
57         return getUser().getId();
58     }
59
60     @Override
61     public boolean hasRole(String roleName) {
62         return getUser().getRoleTips().contains(roleName);
63     }
64
65     @Override
66     public boolean hasAnyRoles(String roleNames) {
67         boolean hasAnyRole = false;
68         if (this.hasLogin() && roleNames != null && roleNames.length() > 0) {
69             for (String role : roleNames.split(",")) {
70                 if (hasRole(role.trim())) {
71                     hasAnyRole = true;
72                     break;
73                 }
74             }
75         }
76         return hasAnyRole;
77     }
78
79     @Override
80     public boolean hasPermission(String permission) {
81         return getUser().getPermissions().contains(permission);
82     }
83
84     @Override
85     public boolean isAdmin() {
86         List<Long> roleList = getUser().getRoleList();
87         if(roleList != null){
88             for (Long integer : roleList) {
89                 String singleRoleTip = ConstantFactory.me().getSingleRoleTip(integer);
90                 if (singleRoleTip.equals(Const.ADMIN_NAME)) {
91                     return true;
92                 }
93             }
94         }
95         return false;
96     }
97
98     @Override
99     public boolean oauth2Flag() {
100         String account = getUser().getAccount();
101         if (account.startsWith(ConstantsContext.getOAuth2UserPrefix())) {
102             return true;
103         } else {
104             return false;
105         }
106     }
107
108     @Override
109     public List<Long> getDeptDataScope() {
110         Long deptId = getUser().getDeptId();
111         List<Long> subDeptIds = ConstantFactory.me().getSubDeptId(deptId);
112         subDeptIds.add(deptId);
113         return subDeptIds;
114     }
115 }