SysUserServiceImpl.java
7.5 KB
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
package com.bsth.service.sys.impl;
import com.bsth.common.ResponseCode;
import com.bsth.controller.sys.util.RSAUtils;
import com.bsth.email.entity.EmailBean;
import com.bsth.entity.sys.Role;
import com.bsth.entity.sys.SysUser;
import com.bsth.repository.sys.SysUserRepository;
import com.bsth.security.util.SecurityUtils;
import com.bsth.service.impl.BaseServiceImpl;
import com.bsth.service.sys.RoleService;
import com.bsth.service.sys.SysUserService;
import com.bsth.util.IpUtils;
import com.bsth.util.MailUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUser, Integer> implements SysUserService{
@Autowired
SysUserRepository sysUserRepository;
@Autowired
RoleService roleService;
// 发送邮件
@Autowired
private MailUtils mailUtils;
Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public SysUser findByUserName(String name) {
return sysUserRepository.findByUserName(name);
}
@Override
public Map<String, Object> save(SysUser t) {
//
if(t.getPassword() == null || t.getPassword().trim().equals("")){
SysUser user = sysUserRepository.findOne(t.getId());
t.setPassword(user.getPassword());
}else{
t.setPassword(new BCryptPasswordEncoder(4).encode(t.getPassword()));
}
return super.save(t);
}
@Override
public int changeEnabled(int id, int enabled) {
sysUserRepository.changeEnabled(id,enabled);
return 0;
}
@Override
public int changePWD(int id,String newPWD) {
return sysUserRepository.changePWD(id,new BCryptPasswordEncoder(4).encode(newPWD));
}
@Override
public boolean validPWDExpired(String userName) {
SysUser sysUser = this.sysUserRepository.findByUserName(userName);
if (sysUser == null) {
throw new RuntimeException("用户[" + userName + "]不存在!");
}
if (sysUser.getPwdValidPeriod() == null || sysUser.getLastPwdDate() == null) {
// 如果没有设定密码过期时间,判定为不过期
return true;
}
DateTime now = new DateTime();
DateTime lastPwdDate = new DateTime(sysUser.getLastPwdDate());
Integer now_period_days = Days.daysBetween(lastPwdDate, now).getDays();
Integer expiredTipDays = 3; // 密码过期提前提示天数
if (now_period_days < (sysUser.getPwdValidPeriod() - expiredTipDays)) {
return true;
} else if (now_period_days >= (sysUser.getPwdValidPeriod() - expiredTipDays) &&
now_period_days < sysUser.getPwdValidPeriod()) {
// 快过期前提示
throw new RuntimeException("当前用户密码还有[" + (sysUser.getPwdValidPeriod() - now_period_days) + "]天过期!");
} else {
throw new RuntimeException("当前用户密码已过期!");
}
}
@Override
public Map<String, Object> register(SysUser u) {
Map<String, Object> rs = new HashMap();
boolean isLegality = false;
Iterator<Role> itRole = u.getRoles().iterator();
Role ro = new Role();
while(itRole.hasNext()){//判断是否有下一个
ro = itRole.next();
if(roleService.checkOperationLegality(ro.getId())){
isLegality = true;
} else {
rs.put("status", ResponseCode.ERROR);
rs.put("msg", "用户权限不够,请联系管理员!");
return rs;
}
}
if(isLegality){
try{
//解密RSA
try{
u.setUserName(RSAUtils.decryptBase64(u.getUserName()));
u.setPassword(RSAUtils.decryptBase64(u.getPassword()));
}catch (RuntimeException e) {
rs.put("msg", "网络延迟,解密失败,请重新添加!");
}
//检查用户名是否存在
if(findByUserName(u.getUserName()) != null){
rs.put("status", ResponseCode.ERROR);
rs.put("msg", "用户名" + u.getUserName() + "已存在!");
}
else{
u.setPassword(new BCryptPasswordEncoder(4).encode(u.getPassword()));
rs = super.save(u);
}
}catch (Exception e){
logger.error("", e);
rs.put("status", ResponseCode.ERROR);
rs.put("msg", e.getMessage());
}
}else {
rs.put("status", ResponseCode.ERROR);
rs.put("msg", "用户权限不够,请联系管理员!");
}
return rs;
}
@Override
public List<SysUser> findAll_distinct() {
Map<String, Object> map = roleService.findSubordinate();
Object object = roleService.findSubordinate().get("list");
List<SysUser> rsList = new ArrayList<>();
// 有权限查看的角色
// List<Role> roleList = JSONArray.parseArray(map.get("list").toString(), Role.class);
try{
Gson gson = new Gson();
// 有权限查看的角色
List<Role> roleList = gson.fromJson(map.get("list").toString(), new TypeToken<List<Role>>(){}.getType());
if(roleList.size() != 0 && !roleList.isEmpty()){
// 遍历有权限查看的角色
Map<Integer,Role> roleMap = new HashMap<>();
for (Role role: roleList) {
roleMap.put(role.getId(),role);
}
List<SysUser> list = new ArrayList<>();
list = sysUserRepository.findAll_distinct();
for (SysUser sysUsers:list) {
Iterator<Role> itUser = sysUsers.getRoles().iterator();
Role roleUser = new Role();
while(itUser.hasNext()){//判断是否有下一个
roleUser = itUser.next();
}
// 添加权限内的用户
if(roleMap.get(roleUser.getId()) != null){
rsList.add(sysUsers);
}
}
}
} catch (Exception e){
logger.error("error", e);
}
return rsList;
}
@Override
@Transactional
public Map<String, Object> resetPassword(Integer id, Integer pwdValidPeriod){
Map<String, Object> rs = new HashMap();
try{
// 获取当前用户
SysUser user = SecurityUtils.getCurrentUser();
Iterator<Role> itRole = user.getRoles().iterator();
Role ro = new Role();
boolean Legality = false;
while(itRole.hasNext()){//判断是否有下一个
ro = itRole.next();
if(ro.getLevel() == 1)
Legality = true;
}
if(Legality){
String pwd = PwdGenerator.randomPassword(16);
user = sysUserRepository.findOne(id);
user.setPwdValidPeriod(pwdValidPeriod);
sysUserRepository.save(user);
sysUserRepository.changePWD(id, new BCryptPasswordEncoder(4).encode(pwd));
//发送邮件
EmailBean mail = new EmailBean();
mail.setSubject(IpUtils.getLocalIpAddress() +":密码重置");
mail.setContent(pwd);
mailUtils.sendMail(mail);
logger.info("setLD-sendMail:邮件发送成功!");
rs.put("status", ResponseCode.SUCCESS);
rs.put("msg", "密码重置成功!");
}else {
rs.put("status", ResponseCode.ERROR);
rs.put("msg", "您不是管理员无权限重置其他用户密码");
}
}catch (Exception e){
logger.error("", e);
rs.put("status", ResponseCode.ERROR);
rs.put("msg", e.getMessage());
}
return rs;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void recordLoginDate(String userName) {
sysUserRepository.recordLoginDate(userName);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void realName(String jobCode, String realName, int id) {
sysUserRepository.realName(jobCode, realName, id);
}
}