kongzy
2024-09-14 f0f00e9ba8a755e4317e029d73b69a92ad9f9df1
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
package com.gkhy.exam.framework.web.service;
 
import cn.hutool.core.util.ObjectUtil;
import com.gkhy.exam.common.domain.entity.SysUser;
import com.gkhy.exam.common.domain.model.LoginUserDetails;
import com.gkhy.exam.common.enums.DeleteStatus;
import com.gkhy.exam.common.enums.UserStatus;
import com.gkhy.exam.common.enums.UserTypeEnum;
import com.gkhy.exam.common.exception.ApiException;
import com.gkhy.exam.system.domain.ExStudent;
import com.gkhy.exam.system.service.ExStudentService;
import com.gkhy.exam.system.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 用户验证处理
 */
@Slf4j
@Service
public class UserDetailServiceImpl implements UserDetailsService {
    @Autowired
    private SysUserService userService;
 
    @Autowired
    private ExStudentService studentService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser user=null;
        if(username.endsWith("_student")){
            username=username.replace("_student","");
            ExStudent student = studentService.selectStudentByPhone(username);
            if(student!=null) {
                user = new SysUser()
                        .setId(student.getId())
                        .setUserType(UserTypeEnum.STUDENT.getCode())
                        .setUsername(username)
                        .setCompanyId(student.getCompanyId())
                        .setName(student.getName())
                        .setStatus(student.getStatus())
                        .setDelFlag(student.getDelFlag())
                        .setPassword(student.getPassword());
            }
        }else{
            username=username.replace("_admin","");
            user=userService.selectUserByUsername(username);
        }
 
        if (ObjectUtil.isNull(user))
        {
            log.info("登录用户:{} 不存在.", username);
            throw new ApiException("登录用户不存在");
        }
        else if (DeleteStatus.DELETED.getCode().equals(user.getDelFlag()))
        {
            log.info("登录用户:{} 已被删除.", username);
            throw new ApiException("登录用户已被删除");
        }
        else if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
        {
            log.info("登录用户:{} 已被停用.", username);
            throw new ApiException("登录用户已被停用");
        }else if(!UserTypeEnum.SYSTEM_USER.getCode().equals(user.getUserType())&&user.getCompanyId()==null){
            log.info("登录用户:{} 公司信息缺失.", username);
            throw new ApiException("登录用户公司信息缺失");
        }
        List<GrantedAuthority> authorities=new ArrayList<>();
        if(UserTypeEnum.SYSTEM_USER.getCode().equals(user.getUserType())) {
            authorities.add(new SimpleGrantedAuthority("train:exam:system"));
        }else if(UserTypeEnum.COMPANY_USER.getCode().equals(user.getUserType())){
            authorities.add(new SimpleGrantedAuthority("train:exam:company"));
        }else if(UserTypeEnum.DEPART_USER.getCode().equals(user.getUserType())){
            authorities.add(new SimpleGrantedAuthority("train:exam:depart"));
        }else if(UserTypeEnum.WORKSHOP_USER.getCode().equals(user.getUserType())){
            authorities.add(new SimpleGrantedAuthority("train:exam:workshop"));
        }else if(UserTypeEnum.OTHER_USER.getCode().equals(user.getUserType())){
            authorities.add(new SimpleGrantedAuthority("train:exam:other"));
        }else if(UserTypeEnum.STUDENT.getCode().equals(user.getUserType())){
            authorities.add(new SimpleGrantedAuthority("train:exam:student"));
        }
        return new LoginUserDetails(user,authorities);
    }
}