提交 | 用户 | 时间
|
1ac2bc
|
1 |
package cn.stylefeng.guns.sys.modular.rest.service; |
懒 |
2 |
|
|
3 |
import cn.hutool.core.collection.CollectionUtil; |
|
4 |
import cn.stylefeng.guns.base.auth.context.LoginContextHolder; |
|
5 |
import cn.stylefeng.guns.base.auth.model.LoginUser; |
|
6 |
import cn.stylefeng.guns.base.oauth2.entity.OauthUserInfo; |
|
7 |
import cn.stylefeng.guns.base.oauth2.service.OauthUserInfoService; |
|
8 |
import cn.stylefeng.guns.base.pojo.node.MenuNode; |
|
9 |
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory; |
|
10 |
import cn.stylefeng.guns.sys.core.constant.Const; |
|
11 |
import cn.stylefeng.guns.sys.core.constant.factory.ConstantFactory; |
|
12 |
import cn.stylefeng.guns.sys.core.constant.state.ManagerStatus; |
|
13 |
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum; |
|
14 |
import cn.stylefeng.guns.sys.core.util.SaltUtil; |
|
15 |
import cn.stylefeng.guns.sys.modular.rest.entity.RestUser; |
|
16 |
import cn.stylefeng.guns.sys.modular.rest.entity.RestUserPos; |
|
17 |
import cn.stylefeng.guns.sys.modular.rest.factory.RestUserFactory; |
|
18 |
import cn.stylefeng.guns.sys.modular.rest.mapper.RestUserMapper; |
|
19 |
import cn.stylefeng.guns.sys.modular.system.model.UserDto; |
|
20 |
import cn.stylefeng.roses.core.datascope.DataScope; |
|
21 |
import cn.stylefeng.roses.core.util.SpringContextHolder; |
|
22 |
import cn.stylefeng.roses.core.util.ToolUtil; |
|
23 |
import cn.stylefeng.roses.kernel.model.exception.ServiceException; |
|
24 |
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
|
25 |
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
26 |
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
27 |
import org.springframework.stereotype.Service; |
|
28 |
import org.springframework.transaction.annotation.Transactional; |
|
29 |
|
|
30 |
import javax.annotation.Resource; |
|
31 |
import java.util.ArrayList; |
|
32 |
import java.util.HashMap; |
|
33 |
import java.util.List; |
|
34 |
import java.util.Map; |
|
35 |
|
|
36 |
/** |
|
37 |
* <p> |
|
38 |
* 管理员表 服务实现类 |
|
39 |
* </p> |
|
40 |
* |
|
41 |
* @author stylefeng |
|
42 |
* @since 2018-12-07 |
|
43 |
*/ |
|
44 |
@Service |
|
45 |
public class RestUserService extends ServiceImpl<RestUserMapper, RestUser> { |
|
46 |
|
|
47 |
@Resource |
|
48 |
private RestMenuService restMenuService; |
|
49 |
|
|
50 |
@Resource |
|
51 |
private RestUserPosService restUserPosService; |
|
52 |
|
|
53 |
/** |
|
54 |
* 添加用戶 |
|
55 |
* |
|
56 |
* @author fengshuonan |
|
57 |
* @Date 2018/12/24 22:51 |
|
58 |
*/ |
|
59 |
@Transactional(rollbackFor = Exception.class) |
|
60 |
public void addUser(UserDto user) { |
|
61 |
|
|
62 |
// 判断账号是否重复 |
|
63 |
RestUser theUser = this.getByAccount(user.getAccount()); |
|
64 |
if (theUser != null) { |
|
65 |
throw new ServiceException(BizExceptionEnum.USER_ALREADY_REG); |
|
66 |
} |
|
67 |
|
|
68 |
// 完善账号信息 |
|
69 |
String salt = SaltUtil.getRandomSalt(); |
|
70 |
String password = SaltUtil.md5Encrypt(user.getPassword(), salt); |
|
71 |
|
|
72 |
RestUser newUser = RestUserFactory.createRestUser(user, password, salt); |
|
73 |
this.save(newUser); |
|
74 |
|
|
75 |
//添加职位关联 |
|
76 |
addPosition(user.getPosition(), newUser.getUserId()); |
|
77 |
} |
|
78 |
|
|
79 |
/** |
|
80 |
* 修改用户 |
|
81 |
* |
|
82 |
* @author fengshuonan |
|
83 |
* @Date 2018/12/24 22:53 |
|
84 |
*/ |
|
85 |
@Transactional(rollbackFor = Exception.class) |
|
86 |
public void editUser(UserDto user) { |
|
87 |
RestUser oldUser = this.getById(user.getUserId()); |
|
88 |
|
|
89 |
if (oldUser == null) { |
|
90 |
throw new ServiceException(BizExceptionEnum.NO_THIS_USER); |
|
91 |
} |
|
92 |
|
|
93 |
if (LoginContextHolder.getContext().hasRole(Const.ADMIN_NAME)) { |
|
94 |
this.updateById(RestUserFactory.editRestUser(user, oldUser)); |
|
95 |
} else { |
|
96 |
this.assertAuth(user.getUserId()); |
|
97 |
LoginUser shiroUser = LoginContextHolder.getContext().getUser(); |
|
98 |
if (shiroUser.getId().equals(user.getUserId())) { |
|
99 |
this.updateById(RestUserFactory.editRestUser(user, oldUser)); |
|
100 |
} else { |
|
101 |
throw new ServiceException(BizExceptionEnum.NO_PERMITION); |
|
102 |
} |
|
103 |
} |
|
104 |
|
|
105 |
//删除职位关联 |
|
106 |
restUserPosService.remove(new QueryWrapper<RestUserPos>().eq("user_id", user.getUserId())); |
|
107 |
|
|
108 |
//添加职位关联 |
|
109 |
addPosition(user.getPosition(), user.getUserId()); |
|
110 |
} |
|
111 |
|
|
112 |
/** |
|
113 |
* 删除用户 |
|
114 |
* |
|
115 |
* @author fengshuonan |
|
116 |
* @Date 2018/12/24 22:54 |
|
117 |
*/ |
|
118 |
@Transactional(rollbackFor = Exception.class) |
|
119 |
public void deleteUser(Long userId) { |
|
120 |
|
|
121 |
//不能删除超级管理员 |
|
122 |
if (userId.equals(Const.ADMIN_ID)) { |
|
123 |
throw new ServiceException(BizExceptionEnum.CANT_DELETE_ADMIN); |
|
124 |
} |
|
125 |
|
|
126 |
//不是超级管理员校验权限 |
|
127 |
if (!LoginContextHolder.getContext().isAdmin()) { |
|
128 |
this.assertAuth(userId); |
|
129 |
} |
|
130 |
this.setStatus(userId, ManagerStatus.DELETED.getCode()); |
|
131 |
|
|
132 |
//删除对应的oauth2绑定表 |
|
133 |
OauthUserInfoService oauthUserInfoService = null; |
|
134 |
try { |
|
135 |
oauthUserInfoService = SpringContextHolder.getBean(OauthUserInfoService.class); |
|
136 |
oauthUserInfoService.remove(new QueryWrapper<OauthUserInfo>().eq("user_id", userId)); |
|
137 |
} catch (Exception e) { |
|
138 |
//没有集成oauth2模块,不操作 |
|
139 |
} |
|
140 |
|
|
141 |
//删除职位关联 |
|
142 |
restUserPosService.remove(new QueryWrapper<RestUserPos>().eq("user_id", userId)); |
|
143 |
} |
|
144 |
|
|
145 |
/** |
|
146 |
* 修改用户状态 |
|
147 |
* |
|
148 |
* @author fengshuonan |
|
149 |
* @Date 2018/12/24 22:45 |
|
150 |
*/ |
|
151 |
public int setStatus(Long userId, String status) { |
|
152 |
return this.baseMapper.setStatus(userId, status); |
|
153 |
} |
|
154 |
|
|
155 |
/** |
|
156 |
* 修改密码 |
|
157 |
* |
|
158 |
* @author fengshuonan |
|
159 |
* @Date 2018/12/24 22:45 |
|
160 |
*/ |
|
161 |
public void changePwd(String oldPassword, String newPassword) { |
|
162 |
Long userId = LoginContextHolder.getContext().getUser().getId(); |
|
163 |
RestUser user = this.getById(userId); |
|
164 |
|
|
165 |
String oldMd5 = SaltUtil.md5Encrypt(oldPassword, user.getSalt()); |
|
166 |
|
|
167 |
if (user.getPassword().equals(oldMd5)) { |
|
168 |
String newMd5 = SaltUtil.md5Encrypt(newPassword, user.getSalt()); |
|
169 |
user.setPassword(newMd5); |
|
170 |
this.updateById(user); |
|
171 |
} else { |
|
172 |
throw new ServiceException(BizExceptionEnum.OLD_PWD_NOT_RIGHT); |
|
173 |
} |
|
174 |
} |
|
175 |
|
|
176 |
/** |
|
177 |
* 根据条件查询用户列表 |
|
178 |
* |
|
179 |
* @author fengshuonan |
|
180 |
* @Date 2018/12/24 22:45 |
|
181 |
*/ |
|
182 |
public Page<Map<String, Object>> selectUsers(DataScope dataScope, String name, String beginTime, String endTime, Long deptId) { |
|
183 |
Page page = LayuiPageFactory.defaultPage(); |
|
184 |
return this.baseMapper.selectUsers(page, dataScope, name, beginTime, endTime, deptId); |
|
185 |
} |
|
186 |
|
|
187 |
/** |
|
188 |
* 设置用户的角色 |
|
189 |
* |
|
190 |
* @author fengshuonan |
|
191 |
* @Date 2018/12/24 22:45 |
|
192 |
*/ |
|
193 |
public int setRoles(Long userId, String roleIds) { |
|
194 |
return this.baseMapper.setRoles(userId, roleIds); |
|
195 |
} |
|
196 |
|
|
197 |
/** |
|
198 |
* 通过账号获取用户 |
|
199 |
* |
|
200 |
* @author fengshuonan |
|
201 |
* @Date 2018/12/24 22:46 |
|
202 |
*/ |
|
203 |
public RestUser getByAccount(String account) { |
|
204 |
return this.baseMapper.getByAccount(account); |
|
205 |
} |
|
206 |
|
|
207 |
/** |
|
208 |
* 获取用户菜单列表 |
|
209 |
* |
|
210 |
* @author fengshuonan |
|
211 |
* @Date 2018/12/24 22:46 |
|
212 |
*/ |
|
213 |
public List<Map<String, Object>> getUserMenuNodes(List<Long> roleList) { |
|
214 |
if (roleList == null || roleList.size() == 0) { |
|
215 |
return new ArrayList<>(); |
|
216 |
} else { |
|
217 |
List<MenuNode> menus = restMenuService.getMenusByRoleIds(roleList); |
|
218 |
|
|
219 |
//定义不同系统分类的菜单集合 |
|
220 |
ArrayList<Map<String, Object>> lists = new ArrayList<>(); |
|
221 |
|
|
222 |
//根据当前用户包含的系统类型,分类出不同的菜单 |
|
223 |
List<Map<String, Object>> systemTypes = LoginContextHolder.getContext().getUser().getSystemTypes(); |
|
224 |
for (Map<String, Object> systemType : systemTypes) { |
|
225 |
|
|
226 |
//当前遍历系统分类code |
|
227 |
String systemCode = (String) systemType.get("code"); |
|
228 |
|
|
229 |
//获取当前系统分类下菜单集合 |
|
230 |
ArrayList<MenuNode> originSystemTypeMenus = new ArrayList<>(); |
|
231 |
for (MenuNode menu : menus) { |
|
232 |
if (menu.getSystemType().equals(systemCode)) { |
|
233 |
originSystemTypeMenus.add(menu); |
|
234 |
} |
|
235 |
} |
|
236 |
|
|
237 |
//拼接存放key为系统分类编码,value为该分类下菜单集合的map |
|
238 |
HashMap<String, Object> map = new HashMap<>(); |
|
239 |
List<MenuNode> treeSystemTypeMenus = MenuNode.buildTitle(originSystemTypeMenus); |
|
240 |
|
|
241 |
//渲染系统类型id |
|
242 |
map.put("id", systemCode); |
|
243 |
|
|
244 |
//系统类型的名称 |
|
245 |
map.put("name", ConstantFactory.me().getDictNameByCode(systemCode)); |
|
246 |
|
|
247 |
//各个系统地子菜单 |
|
248 |
map.put("subMenus", treeSystemTypeMenus); |
|
249 |
|
|
250 |
lists.add(map); |
|
251 |
} |
|
252 |
|
|
253 |
return lists; |
|
254 |
} |
|
255 |
|
|
256 |
} |
|
257 |
|
|
258 |
/** |
|
259 |
* 判断当前登录的用户是否有操作这个用户的权限 |
|
260 |
* |
|
261 |
* @author fengshuonan |
|
262 |
* @Date 2018/12/24 22:44 |
|
263 |
*/ |
|
264 |
public void assertAuth(Long userId) { |
|
265 |
if (LoginContextHolder.getContext().isAdmin()) { |
|
266 |
return; |
|
267 |
} |
|
268 |
List<Long> deptDataScope = LoginContextHolder.getContext().getDeptDataScope(); |
|
269 |
RestUser user = this.getById(userId); |
|
270 |
Long deptId = user.getDeptId(); |
|
271 |
if (deptDataScope.contains(deptId)) { |
|
272 |
return; |
|
273 |
} else { |
|
274 |
throw new ServiceException(BizExceptionEnum.NO_PERMITION); |
|
275 |
} |
|
276 |
|
|
277 |
} |
|
278 |
|
|
279 |
/** |
|
280 |
* 刷新当前登录用户的信息 |
|
281 |
* |
|
282 |
* @author fengshuonan |
|
283 |
* @Date 2019/1/19 5:59 PM |
|
284 |
*/ |
|
285 |
public void refreshCurrentUser() { |
|
286 |
LoginUser user = LoginContextHolder.getContext().getUser(); |
|
287 |
Long id = user.getId(); |
|
288 |
RestUser currentUser = this.getById(id); |
|
289 |
|
|
290 |
//TODO 刷新 |
|
291 |
// LoginUser shiroUser = userAuthService.shiroUser(currentUser); |
|
292 |
// LoginUser lastUser = LoginContextHolder.getContext().getUser(); |
|
293 |
// BeanUtil.copyProperties(shiroUser, lCastUser); |
|
294 |
} |
|
295 |
|
|
296 |
/** |
|
297 |
* 获取用户的基本信息 |
|
298 |
* |
|
299 |
* @author fengshuonan |
|
300 |
* @Date 2019-05-04 17:12 |
|
301 |
*/ |
|
302 |
public Map<String, Object> getUserInfo(Long userId) { |
|
303 |
RestUser user = this.getById(userId); |
|
304 |
Map<String, Object> map = RestUserFactory.removeUnSafeFieldsRest(user); |
|
305 |
|
|
306 |
HashMap<String, Object> hashMap = CollectionUtil.newHashMap(); |
|
307 |
hashMap.putAll(map); |
|
308 |
hashMap.put("roleName", ConstantFactory.me().getRoleName(user.getRoleId())); |
|
309 |
hashMap.put("deptName", ConstantFactory.me().getDeptName(user.getDeptId())); |
|
310 |
|
|
311 |
hashMap.put("positionIds", ConstantFactory.me().getPositionIds(userId)); |
|
312 |
hashMap.put("positionNames", ConstantFactory.me().getPositionName(userId)); |
|
313 |
|
|
314 |
return hashMap; |
|
315 |
} |
|
316 |
|
|
317 |
|
|
318 |
/** |
|
319 |
* 添加职位关联 |
|
320 |
* |
|
321 |
* @author fengshuonan |
|
322 |
* @Date 2019-06-28 13:35 |
|
323 |
*/ |
|
324 |
private void addPosition(String positions, Long userId) { |
|
325 |
if (ToolUtil.isNotEmpty(positions)) { |
|
326 |
String[] position = positions.split(","); |
|
327 |
for (String item : position) { |
|
328 |
|
|
329 |
RestUserPos entity = new RestUserPos(); |
|
330 |
entity.setUserId(userId); |
|
331 |
entity.setPosId(Long.valueOf(item)); |
|
332 |
|
|
333 |
restUserPosService.save(entity); |
|
334 |
|
|
335 |
} |
|
336 |
} |
|
337 |
} |
|
338 |
} |