zj
2024-06-03 3603ecb207f7e712c635f19531e05fac4d19e53f
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
package project.user.internal;
 
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
 
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.providers.encoding.PasswordEncoder;
 
import kernel.exception.BusinessException;
import kernel.util.StringUtils;
import kernel.web.ApplicationUtil;
import project.Constants;
import project.party.PartyService;
import project.party.model.Party;
import project.syspara.Syspara;
import project.syspara.SysparaService;
import project.user.UserService;
import project.user.idcode.IdentifyingCodeTimeWindowService;
import security.Role;
import security.SecUser;
import security.internal.SecUserService;
 
public class UserServiceImpl implements UserService {
    
    private PartyService partyService;
    
    private SecUserService secUserService;
    
    private PasswordEncoder passwordEncoder;
    
    private OnlineUserService onlineUserService;
    
    private IdentifyingCodeTimeWindowService identifyingCodeTimeWindowService;
    
    /**
     * 图片验证key,保证前后一致性
     */
    private Map<String, String> imageCodeCache = new ConcurrentHashMap<String, String>();
 
    @Override
    public SecUser login(String username, String password) {
        SecUser user = secUserService.findUserByLoginName(username);
        SysparaService sysparaService = ApplicationUtil.getBean(SysparaService.class);
        Syspara syspara = sysparaService.find("bind_email_phone_login");
        if(ObjectUtils.isNotEmpty(syspara)&&"1".equals(syspara.getValue())) {
            user = secUserService.findUserByLoginName2(username);
        }
        if (user == null) {
            throw new BusinessException("用户不存在");
        }
        Party party = partyService.cachePartyBy(user.getPartyId(), false);
        String[] rolesArrty = new String[] { Constants.SECURITY_ROLE_GUEST, Constants.SECURITY_ROLE_MEMBER, Constants.SECURITY_ROLE_TEST };
        if (party == null || !party.getLogin_authority()) {
            throw new BusinessException("登录失败");
        }
        Set<Role> roles = user.getRoles();
        boolean find = false;
        for (Iterator<Role> iterator = roles.iterator(); iterator.hasNext();) {
            Role role = (Role) iterator.next();
            for (int i = 0; i < rolesArrty.length; i++) {
                if (role.getRoleName().equals(rolesArrty[i])) {
                    find = true;
                }
            }
        }
 
        if (!find) {
            throw new BusinessException("登录失败");
        }
        String password_encoder = passwordEncoder.encodePassword(password, user.getUsername());
 
        if (!password_encoder.equals(user.getPassword())) {
            throw new BusinessException("密码不正确");
        }
 
        party.setLast_loginTime(new Date());
        partyService.update(party);
 
        return user;
 
    }
    
    /**
     * 验证码登录
     */
    @Override
    public SecUser login_idcode(String username, String verifcode) {
        SecUser user = secUserService.findUserByLoginName(username);
 
        if (user == null) {
            throw new BusinessException("用户名不存在");
        }
        Party party = partyService.cachePartyBy(user.getPartyId(), false);
        String[] rolesArrty = new String[] {};
        if (party == null || !party.getLogin_authority()) {
            throw new BusinessException("登录失败");
        }
        Set<Role> roles = user.getRoles();
        boolean find = false;
        for (Iterator<Role> iterator = roles.iterator(); iterator.hasNext();) {
            Role role = (Role) iterator.next();
            for (int i = 0; i < rolesArrty.length; i++) {
                if (role.getRoleName().equals(rolesArrty[i])) {
                    find = true;
                }
            }
        }
 
        if (!find) {
            throw new BusinessException("登录失败");
        }
 
        String authcode = this.identifyingCodeTimeWindowService.getAuthCode(username);
 
        if ((authcode == null) || (!authcode.equals(verifcode))) {
            throw new BusinessException("登录失败");
        }
        this.identifyingCodeTimeWindowService.delAuthCode(username);
        return user;
    }
    
    @Override
    public void online(String partyId) {
        if (StringUtils.isNullOrEmpty(partyId)) {
            return;
        }
        onlineUserService.put(partyId, new Date());
    }
    
    @Override
    public void logout(String partyId) {
        if (StringUtils.isNullOrEmpty(partyId)) {
            return;
        }
        onlineUserService.del(partyId);
    }
    
    @Override
    public boolean isOnline(String partyId) {
        Object object = onlineUserService.get(partyId);
        if (object != null) {
            return true;
        }
        return false;
    }
 
    public void setSecUserService(SecUserService secUserService) {
        this.secUserService = secUserService;
    }
 
    public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }
 
    public void setPartyService(PartyService partyService) {
        this.partyService = partyService;
    }
 
    public Map<String, String> getImageCodeCache() {
        return imageCodeCache;
    }
 
    public void putImageCode(String key, String value) {
        imageCodeCache.put(key, value);
    }
 
    public String cacheImageCode(String key) {
        return imageCodeCache.get(key);
    }
 
    public void cacheRemoveImageCode(String key) {
        imageCodeCache.remove(key);
    }
 
    public void putRandKey(String key, String value) {
        imageCodeCache.put(key, value);
    }
 
    public IdentifyingCodeTimeWindowService getIdentifyingCodeTimeWindowService() {
        return identifyingCodeTimeWindowService;
    }
 
    public void setIdentifyingCodeTimeWindowService(IdentifyingCodeTimeWindowService identifyingCodeTimeWindowService) {
        this.identifyingCodeTimeWindowService = identifyingCodeTimeWindowService;
    }
 
    public void setOnlineUserService(OnlineUserService onlineUserService) {
        this.onlineUserService = onlineUserService;
    }
}