郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package com.gkhy.safePlatform.account.service.impl;
 
import com.alibaba.fastjson.JSONArray;
import com.gkhy.safePlatform.account.entity.enterprise.DepartmentInfoDO;
import com.gkhy.safePlatform.account.entity.user.*;
import com.gkhy.safePlatform.account.enums.RoleStatusEnum;
import com.gkhy.safePlatform.account.enums.UserStatusEnum;
import com.gkhy.safePlatform.account.model.dto.req.AccountPwdChangeReqDTO;
import com.gkhy.safePlatform.account.service.AuthService;
import com.gkhy.safePlatform.account.service.RedisService;
import com.gkhy.safePlatform.account.service.baseService.*;
import com.gkhy.safePlatform.account.utils.TokenUtil;
import com.gkhy.safePlatform.account.utils.MenuUtil;
import com.gkhy.safePlatform.account.utils.PasswordUtil;
import com.gkhy.safePlatform.account.model.dto.req.LoginReqDTO;
import com.gkhy.safePlatform.account.model.dto.resp.MenuRespDTO;
import com.gkhy.safePlatform.account.model.dto.resp.UserLoginRespDTO;
import com.gkhy.safePlatform.commons.co.ContextCacheUser;
import com.gkhy.safePlatform.commons.enums.RedisKeyEnum;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.enums.UserTypeEnum;
import com.gkhy.safePlatform.commons.exception.BusinessException;
import com.gkhy.safePlatform.commons.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
@Service("authService")
public class AuthServiceImpl implements AuthService {
 
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private RoleInfoService roleInfoService;
    @Autowired
    private PermissionInfoService permissionInfoService;
    @Autowired
    private MenuInfoService menuInfoService;
    @Autowired
    private TokenUtil tokenConfig;
    @Autowired
    private RedisService redisService;
    @Autowired
    private DepartmentInfoService departmentInfoService;
    @Autowired
    private PositionInfoService positionInfoService;
 
 
 
    /**
     * @Description: 登录
     */
    @Override
    public UserLoginRespDTO authLogin(LoginReqDTO loginParam) {
 
        // todo 根据入参判断用什么登录
        String usernameParam = loginParam.getUsername();
        // 用户名不能为空
        if (StringUtils.isBlank(usernameParam)) {
            throw new BusinessException(ResultCodes.CLIENT_ACCOUNT_USERNAME_NULL);
        }
        String username = usernameParam.trim();
        UserInfo userDetail = userInfoService.getUserByUsername(username);
        // 用户不存在
        if (userDetail == null) {
            throw new BusinessException(ResultCodes.CLIENT_ACCOUNT_NOT_EXIST);
        }
        // 用户状态 以后可能会有离职、冻结等状态
        if (!userDetail.getStatus().equals(UserStatusEnum.VALID.getCode())) {
            throw new BusinessException(ResultCodes.CLIENT_ACCOUNT_NOT_EXIST);
        }
        // 密码不为空
        if (StringUtils.isBlank(loginParam.getPassword())) {
            throw new BusinessException(ResultCodes.CLIENT_PASSWORD_NULL);
        }
        String password = loginParam.getPassword().trim();
        // 密码匹配
        if (!PasswordUtil.match(password, userDetail.getSalt(), userDetail.getHash())) {
            throw new BusinessException(ResultCodes.CLIENT_PASSWORD_ERROR);
        }
        // todo 用户过期
 
 
        // 用户role 需要分配
        RoleInfoDO roleInfo = roleInfoService.getRoleInfoDOById(userDetail.getRoleId());
        if (roleInfo == null) {
            throw new BusinessException(ResultCodes.CLIENT_ACCOUNT_HAS_NO_ROLE);
        }
        // 用户dep
        DepartmentInfoDO department = new DepartmentInfoDO();
        if (userDetail.getDepId() != null) {
            department = departmentInfoService.getDepartmentInfoDOById(userDetail.getDepId());
        }
        // 用户岗位
        PositionInfoDO position = new PositionInfoDO();
        if (userDetail.getPositionId() != null) {
            position = positionInfoService.getPositionInfoById(userDetail.getPositionId());
        }
 
        // security对象中存入登陆者信息
        List<GrantedAuthority> authorities = new ArrayList<>();
        // role
        authorities.add(new SimpleGrantedAuthority("ROLE_"+ roleInfo.getCode()));
 
        // permission
        List<PermissionInfo> permissions = this.getUserPermissionByUserId(userDetail.getUid());
        for (PermissionInfo permission : permissions) {
            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(permission.getCode());
            authorities.add(simpleGrantedAuthority);
        }
 
        // 生成 accessToken
        String accessToken = TokenUtil.makeToken(username,userDetail.getUid());
 
        // 存入 redis
        String accessTokenKey = RedisKeyEnum.authKey(RedisKeyEnum.AUTH_TOKEN, userDetail.getUid());
        String authoritiesKey = RedisKeyEnum.authKey(RedisKeyEnum.AUTH_AUTHORITIES, userDetail.getUid());
 
        ContextCacheUser contextCacheUser = new ContextCacheUser(
                // 用户uid
                userDetail.getUid(),
                // 用户基本信息
                userDetail.getUsername(), userDetail.getRealName(), userDetail.getPhone(), userDetail.getEmail(),
                // 用户角色信息
                roleInfo.getId(), roleInfo.getCode(),
                // 用户部门信息
                department.getId(), department.getName(),
                // 用户岗位信息
                position.getId(), position.getCode(),
                // 用户类型
                userDetail.getType(),
                // token
                accessToken);
        // 1.通行认证:auth:token:access
        redisService.setCacheUserAndExpireTime(accessTokenKey, contextCacheUser, tokenConfig.getExpiration());
        // 2.角色权限:auth:authorities
        redisService.setCacheAuthorityAndExpireTime(authoritiesKey, authorities, tokenConfig.getExpiration());
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetail, accessToken,authorities);
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
 
        return new UserLoginRespDTO(
                userDetail.getUid(),
                userDetail.getUsername(),
                userDetail.getRealName(),
                userDetail.getType(),
                userDetail.getStatus(),
                userDetail.getPhone(),
                userDetail.getEmail(),
                department.getName(),
                position.getName(),
                Collections.singletonList(roleInfo.getCode()),
                accessToken,
                department.getId()
        );
    }
 
    @Override
    public List<PermissionInfo> getUserPermissionByUserId(Long userId) {
        if (userId == null) {
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        return permissionInfoService.getPermissionsByUserId(userId);
    }
 
    @Override
    public UserInfo getUserInfoByUserId(Long userId) {
        if (userId == null) {
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        return userInfoService.getUserByUserId(userId);
    }
 
 
    /**
     * @Description: 根据用户名登出用户
     */
    @Override
    public void authLogout(Long userId) {
        assert userId != null;
        String accessTokenKey = RedisKeyEnum.authKey(RedisKeyEnum.AUTH_TOKEN, userId);
        // 1.删除通行认证:auth:token:access
        redisService.cleanCacheUserByKey(accessTokenKey);
        // 2.删除用户权限
        String authoritiesKey = RedisKeyEnum.authKey(RedisKeyEnum.AUTH_AUTHORITIES, userId);
        redisService.cleanCacheAuthorityByKey(authoritiesKey);
 
    }
 
    @Override
    public UserInfo getUserByUserId(Long userId) {
        if (userId == null) {
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        return userInfoService.getUserByUserId(userId);
    }
 
 
    @Override
    public UserDetails getUserDetailsByUsername(String username) {
        return userInfoService.getUserByUsername(username);
    }
 
    @Override
    public void pwdChange(ContextCacheUser currentUser, AccountPwdChangeReqDTO reqDTO) {
        if (reqDTO.getUid() == null) {
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        if (StringUtils.isBlank(reqDTO.getNewPassword())) {
            throw new BusinessException(ResultCodes.CLIENT_PASSWORD_NULL);
        }
        String newPassword = reqDTO.getNewPassword().trim();
        // todo 密码正则
        UserInfo userInfo = userInfoService.getUserByUserId(reqDTO.getUid());
        if (userInfo == null) {
            throw new BusinessException(ResultCodes.CLIENT_ACCOUNT_NOT_EXIST);
        }
        if (!userInfo.getStatus().equals(UserStatusEnum.VALID.getCode())) {
            throw new BusinessException(ResultCodes.CLIENT_ACCOUNT_NOT_EXIST);
        }
 
        String salt = PasswordUtil.makeSalt();
        String hash = PasswordUtil.makePassword(newPassword, salt);
        userInfoService.updatePassword(reqDTO.getUid(), salt, hash);
 
    }
 
 
    @Override
    public RoleInfo getUserRoleByUserId(Long userId) {
        if (userId == null) {
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        RoleInfo roleInfo = roleInfoService.getRoleInfoByUserId(userId);
        if (roleInfo == null) {
            throw new BusinessException(ResultCodes.CLIENT_ROLE_NOT_EXIST);
        }
        assert roleInfo.getStatus() != null;
        RoleStatusEnum status = RoleStatusEnum.parse(roleInfo.getStatus());
        if (status != RoleStatusEnum.ENABLED) {
            throw new BusinessException(ResultCodes.CLIENT_ROLE_CODE_NOT_ON);
        }
        return roleInfo;
    }
 
 
    @Override
    public List<MenuRespDTO> getUserMenuTreeByUserIdAndProjectId(ContextCacheUser currentUser, Long projectId){
        if (currentUser.getUid() == null) {
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
 
//        RoleInfo roleInfo = roleInfoService.getRoleInfoByUserId(currentUser.getUid());
//        // 角色判断
//        if (roleInfo == null) {
//            throw new BusinessException(ResultCodes.CLIENT_ACCOUNT_HAS_NO_ROLE);
//        }
 
        List<MenuInfoDO> allMenu;
        // 1.redis获取所有菜单
        String oo = redisService.getCacheMenuByKey(RedisKeyEnum.AUTH_MENU.getKey());
        if (oo != null) {
            allMenu = JSONArray.parseArray(oo, MenuInfoDO.class);
        }else{
            // 2.redis没有则mysql获取
            allMenu = menuInfoService.getAllMenu();
            redisService.setCacheMenuAndExpireTime(RedisKeyEnum.AUTH_MENU.getKey(), allMenu, tokenConfig.getExpiration());
        }
 
 
//        // 如果project不为空 判断project是否为角色所拥有
//        if (projectId != null) {
//            RoleProjectInfo roleProject = roleProjectInfoService.getRoleProjectInfo(roleInfo.getId(), projectId);
//            if (roleProject == null) {
//                // 需要返回公共页面
//                return MenuUtil.generatePublicTree(allMenu);
//            }
//        }
        // 1.根据用户类型判断最高权限
        // 1.1 type 为 admin的拥有最高菜单可分配和可视权限 不受角色code影响
        List<MenuInfoDO> userMenu = allMenu;
        assert currentUser.getType() != null;
        if (!currentUser.getType().equals(UserTypeEnum.ADMIN.getCode())) {
            userMenu = MenuUtil.getUserMenuFromAllMenu(allMenu, currentUser.getRoleCode());
        }
 
        return MenuUtil.generateMenuTree(userMenu, projectId);
    }
 
 
 
}