292d7fb0c8554dd63a6a0be7df0f3e2410eea697.svn-base
13.3 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
* Copyright © 2015-2018 ODM All rights reserved.
*/
package com.thinkgem.jeesite.modules.sys.web;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.beanvalidator.BeanValidators;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.common.utils.excel.ImportExcel;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.Role;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
/**
* 用户Controller
* @author ThinkGem
* @version 2013-8-29
*/
@Controller
@RequestMapping(value = "${adminPath}/sys/user")
public class UserController extends BaseController {
@Autowired
private SystemService systemService;
@Autowired
private OfficeService officeService;
@ModelAttribute
public User get(@RequestParam(required=false) String id) {
if (StringUtils.isNotBlank(id)){
return systemService.getUser(id);
}else{
return new User();
}
}
@RequiresPermissions("sys:user:view")
@RequestMapping(value = {"index"})
public String index(User user, Model model) {
return "modules/sys/userIndex";
}
@RequiresPermissions("sys:user:view")
@RequestMapping(value = {"list", ""})
public String list(User user, HttpServletRequest request, HttpServletResponse response, Model model) {
Page<User> page = systemService.findUser(new Page<User>(request, response), user);
model.addAttribute("page", page);
return "modules/sys/userList";
}
@RequiresPermissions("sys:user:view")
@RequestMapping(value = "form")
public String form(User user, Model model) {
if (user.getCompany()==null || user.getCompany().getId()==null){
user.setCompany(UserUtils.getUser().getCompany());
}
if (user.getOffice()==null || user.getOffice().getId()==null){
user.setOffice(UserUtils.getUser().getOffice());
}
model.addAttribute("user", user);
model.addAttribute("allRoles", systemService.findAllRole());
return "modules/sys/userForm";
}
@RequiresPermissions("sys:user:edit")
@RequestMapping(value = "save")
public String save(User user, HttpServletRequest request, Model model, RedirectAttributes redirectAttributes) {
if(Global.isDemoMode()){
addMessage(redirectAttributes, "演示模式,不允许操作!");
return "redirect:" + adminPath + "/sys/user/list?repage";
}
// 修正引用赋值问题,不知道为何,Company和Office引用的一个实例地址,修改了一个,另外一个跟着修改。
user.setCompany(new Office(request.getParameter("company.id")));
user.setOffice(new Office(request.getParameter("office.id")));
// 如果新密码为空,则不更换密码
if (StringUtils.isNotBlank(user.getNewPassword())) {
user.setPassword(SystemService.entryptPassword(user.getNewPassword()));
}
if (!beanValidator(model, user)){
return form(user, model);
}
if (!"true".equals(checkLoginName(user.getOldLoginName(), user.getLoginName()))){
addMessage(model, "保存用户'" + user.getLoginName() + "'失败,登录名已存在");
return form(user, model);
}
// 角色数据有效性验证,过滤不在授权内的角色
List<Role> roleList = Lists.newArrayList();
List<String> roleIdList = user.getRoleIdList();
for (Role r : systemService.findAllRole()){
if (roleIdList.contains(r.getId())){
roleList.add(r);
}
}
user.setRoleList(roleList);
// 保存用户信息
systemService.saveUser(user);
// 清除当前用户缓存
if (user.getLoginName().equals(UserUtils.getUser().getLoginName())){
UserUtils.clearCache();
//UserUtils.getCacheMap().clear();
}
addMessage(redirectAttributes, "保存用户'" + user.getLoginName() + "'成功");
return "redirect:" + adminPath + "/sys/user/list?repage";
}
@RequiresPermissions("sys:user:edit")
@RequestMapping(value = "delete")
public String delete(User user, RedirectAttributes redirectAttributes) {
if(Global.isDemoMode()){
addMessage(redirectAttributes, "演示模式,不允许操作!");
return "redirect:" + adminPath + "/sys/user/list?repage";
}
if (UserUtils.getUser().getId().equals(user.getId())){
addMessage(redirectAttributes, "删除用户失败, 不允许删除当前用户");
}else if (User.isAdmin(user.getId())){
addMessage(redirectAttributes, "删除用户失败, 不允许删除超级管理员用户");
}else{
systemService.deleteUser(user);
addMessage(redirectAttributes, "删除用户成功");
}
return "redirect:" + adminPath + "/sys/user/list?repage";
}
/**
* 导出用户数据
* @param user
* @param request
* @param response
* @param redirectAttributes
* @return
*/
@RequiresPermissions("sys:user:view")
@RequestMapping(value = "export", method=RequestMethod.POST)
public String exportFile(User user, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
try {
String fileName = "用户数据"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
Page<User> page = systemService.findUser(new Page<User>(request, response, -1), user);
new ExportExcel("用户数据", User.class).setDataList(page.getList()).write(response, fileName).dispose();
return null;
} catch (Exception e) {
addMessage(redirectAttributes, "导出用户失败!失败信息:"+e.getMessage());
}
return "redirect:" + adminPath + "/sys/user/list?repage";
}
/**
* 导入用户数据
* @param file
* @param redirectAttributes
* @return
*/
@RequiresPermissions("sys:user:edit")
@RequestMapping(value = "import", method=RequestMethod.POST)
public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
if(Global.isDemoMode()){
addMessage(redirectAttributes, "演示模式,不允许操作!");
return "redirect:" + adminPath + "/sys/user/list?repage";
}
try {
int successNum = 0;
int failureNum = 0;
StringBuilder failureMsg = new StringBuilder();
ImportExcel ei = new ImportExcel(file, 1, 0);
List<User> list = ei.getDataList(User.class);
for (User user : list){
try{
if ("true".equals(checkLoginName("", user.getLoginName()))){
user.setPassword(SystemService.entryptPassword("123456"));
BeanValidators.validateWithException(validator, user);
systemService.saveUser(user);
successNum++;
}else{
failureMsg.append("<br/>登录名 "+user.getLoginName()+" 已存在; ");
failureNum++;
}
}catch(ConstraintViolationException ex){
failureMsg.append("<br/>登录名 "+user.getLoginName()+" 导入失败:");
List<String> messageList = BeanValidators.extractPropertyAndMessageAsList(ex, ": ");
for (String message : messageList){
failureMsg.append(message+"; ");
failureNum++;
}
}catch (Exception ex) {
failureMsg.append("<br/>登录名 "+user.getLoginName()+" 导入失败:"+ex.getMessage());
}
}
if (failureNum>0){
failureMsg.insert(0, ",失败 "+failureNum+" 条用户,导入信息如下:");
}
addMessage(redirectAttributes, "已成功导入 "+successNum+" 条用户"+failureMsg);
} catch (Exception e) {
addMessage(redirectAttributes, "导入用户失败!失败信息:"+e.getMessage());
}
return "redirect:" + adminPath + "/sys/user/list?repage";
}
/**
* 下载导入用户数据模板
* @param response
* @param redirectAttributes
* @return
*/
@RequiresPermissions("sys:user:view")
@RequestMapping(value = "import/template")
public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
try {
String fileName = "用户数据导入模板.xlsx";
List<User> list = Lists.newArrayList(); list.add(UserUtils.getUser());
new ExportExcel("用户数据", User.class, 2).setDataList(list).write(response, fileName).dispose();
return null;
} catch (Exception e) {
addMessage(redirectAttributes, "导入模板下载失败!失败信息:"+e.getMessage());
}
return "redirect:" + adminPath + "/sys/user/list?repage";
}
/**
* 验证登录名是否有效
* @param oldLoginName
* @param loginName
* @return
*/
@ResponseBody
@RequiresPermissions("sys:user:edit")
@RequestMapping(value = "checkLoginName")
public String checkLoginName(String oldLoginName, String loginName) {
if (loginName !=null && loginName.equals(oldLoginName)) {
return "true";
} else if (loginName !=null && systemService.getUserByLoginName(loginName) == null) {
return "true";
}
return "false";
}
/**
* 用户信息显示及保存
* @param user
* @param model
* @return
*/
@RequiresPermissions("user")
@RequestMapping(value = "info")
public String info(User user, HttpServletResponse response, Model model) {
User currentUser = UserUtils.getUser();
if (StringUtils.isNotBlank(user.getName())){
if(Global.isDemoMode()){
model.addAttribute("message", "演示模式,不允许操作!");
return "modules/sys/userInfo";
}
currentUser.setPcode(user.getPcode());
currentUser.setEmail(user.getEmail());
currentUser.setPhone(user.getPhone());
currentUser.setMobile(user.getMobile());
currentUser.setRemarks(user.getRemarks());
currentUser.setPhoto(user.getPhoto());
currentUser.setCkbh(user.getCkbh());
systemService.updateUserInfo(currentUser);
model.addAttribute("message", "保存用户信息成功");
}
model.addAttribute("user", currentUser);
Office office = officeService.get(currentUser.getCurrentUser().getOffice().getId());
model.addAttribute("officeCode", office.getCode());
model.addAttribute("Global", new Global());
return "modules/sys/userInfo";
}
/**
* 返回用户信息
* @return
*/
@RequiresPermissions("user")
@ResponseBody
@RequestMapping(value = "infoData")
public User infoData() {
return UserUtils.getUser();
}
/**
* 修改个人用户密码
* @param oldPassword
* @param newPassword
* @param model
* @return
*/
@RequiresPermissions("user")
@RequestMapping(value = "modifyPwd")
public String modifyPwd(String oldPassword, String newPassword, Model model, HttpServletRequest request) {
User user = UserUtils.getUser();
if (StringUtils.isNotBlank(oldPassword) && StringUtils.isNotBlank(newPassword)){
if(Global.isDemoMode()){
model.addAttribute("message", "演示模式,不允许操作!");
return "modules/sys/userModifyPwd";
}
if (SystemService.validatePassword(oldPassword, user.getPassword())){
systemService.updatePasswordById(user.getId(), user.getLoginName(), newPassword);
model.addAttribute("message", "修改密码成功");
}else{
model.addAttribute("message", "修改密码失败,旧密码错误");
}
}
model.addAttribute("user", user);
String forcedModify = request.getParameter("forcedModify");
if(StringUtils.isNotBlank(forcedModify) && "1".equals(forcedModify)){
return "modules/sys/sysIndex";
}else{
return "modules/sys/userModifyPwd";
}
}
@RequiresPermissions("user")
@ResponseBody
@RequestMapping(value = "treeData")
public List<Map<String, Object>> treeData(@RequestParam(required=false) String officeId, HttpServletResponse response) {
List<Map<String, Object>> mapList = Lists.newArrayList();
List<User> list = systemService.findUserByOfficeId(officeId);
for (int i=0; i<list.size(); i++){
User e = list.get(i);
Map<String, Object> map = Maps.newHashMap();
map.put("id", "u_"+e.getId());
map.put("pId", officeId);
map.put("name", StringUtils.replace(e.getName(), " ", ""));
mapList.add(map);
}
return mapList;
}
// @InitBinder
// public void initBinder(WebDataBinder b) {
// b.registerCustomEditor(List.class, "roleList", new PropertyEditorSupport(){
// @Autowired
// private SystemService systemService;
// @Override
// public void setAsText(String text) throws IllegalArgumentException {
// String[] ids = StringUtils.split(text, ",");
// List<Role> roles = new ArrayList<Role>();
// for (String id : ids) {
// Role role = systemService.getRole(Long.valueOf(id));
// roles.add(role);
// }
// setValue(roles);
// }
// @Override
// public String getAsText() {
// return Collections3.extractToString((List) getValue(), "id", ",");
// }
// });
// }
}