潘志宝
5 天以前 7fce3006ecd0b670e33c2d3ba123778e79e2e943
提交 | 用户 | 时间
e7c126 1 package com.xxl.job.admin.controller;
H 2
3 import com.xxl.job.admin.controller.annotation.PermissionLimit;
4 import com.xxl.job.admin.core.model.XxlJobGroup;
5 import com.xxl.job.admin.core.model.XxlJobUser;
6 import com.xxl.job.admin.core.util.I18nUtil;
7 import com.xxl.job.admin.dao.XxlJobGroupDao;
8 import com.xxl.job.admin.dao.XxlJobUserDao;
9 import com.xxl.job.admin.service.LoginService;
10 import com.xxl.job.core.biz.model.ReturnT;
11 import org.springframework.stereotype.Controller;
12 import org.springframework.ui.Model;
13 import org.springframework.util.DigestUtils;
14 import org.springframework.util.StringUtils;
15 import org.springframework.web.bind.annotation.RequestMapping;
16 import org.springframework.web.bind.annotation.RequestParam;
17 import org.springframework.web.bind.annotation.ResponseBody;
18
19 import javax.annotation.Resource;
20 import javax.servlet.http.HttpServletRequest;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 /**
26  * @author xuxueli 2019-05-04 16:39:50
27  */
28 @Controller
29 @RequestMapping("/user")
30 public class UserController {
31
32     @Resource
33     private XxlJobUserDao xxlJobUserDao;
34     @Resource
35     private XxlJobGroupDao xxlJobGroupDao;
36
37     @RequestMapping
38     @PermissionLimit(adminuser = true)
39     public String index(Model model) {
40
41         // 执行器列表
42         List<XxlJobGroup> groupList = xxlJobGroupDao.findAll();
43         model.addAttribute("groupList", groupList);
44
45         return "user/user.index";
46     }
47
48     @RequestMapping("/pageList")
49     @ResponseBody
50     @PermissionLimit(adminuser = true)
51     public Map<String, Object> pageList(@RequestParam(required = false, defaultValue = "0") int start,
52                                         @RequestParam(required = false, defaultValue = "10") int length,
53                                         String username, int role) {
54
55         // page list
56         List<XxlJobUser> list = xxlJobUserDao.pageList(start, length, username, role);
57         int list_count = xxlJobUserDao.pageListCount(start, length, username, role);
58
59         // filter
60         if (list!=null && list.size()>0) {
61             for (XxlJobUser item: list) {
62                 item.setPassword(null);
63             }
64         }
65
66         // package result
67         Map<String, Object> maps = new HashMap<String, Object>();
68         maps.put("recordsTotal", list_count);        // 总记录数
69         maps.put("recordsFiltered", list_count);    // 过滤后的总记录数
70         maps.put("data", list);                      // 分页列表
71         return maps;
72     }
73
74     @RequestMapping("/add")
75     @ResponseBody
76     @PermissionLimit(adminuser = true)
77     public ReturnT<String> add(XxlJobUser xxlJobUser) {
78
79         // valid username
80         if (!StringUtils.hasText(xxlJobUser.getUsername())) {
81             return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("system_please_input")+I18nUtil.getString("user_username") );
82         }
83         xxlJobUser.setUsername(xxlJobUser.getUsername().trim());
84         if (!(xxlJobUser.getUsername().length()>=4 && xxlJobUser.getUsername().length()<=20)) {
85             return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("system_lengh_limit")+"[4-20]" );
86         }
87         // valid password
88         if (!StringUtils.hasText(xxlJobUser.getPassword())) {
89             return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("system_please_input")+I18nUtil.getString("user_password") );
90         }
91         xxlJobUser.setPassword(xxlJobUser.getPassword().trim());
92         if (!(xxlJobUser.getPassword().length()>=4 && xxlJobUser.getPassword().length()<=20)) {
93             return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("system_lengh_limit")+"[4-20]" );
94         }
95         // md5 password
96         xxlJobUser.setPassword(DigestUtils.md5DigestAsHex(xxlJobUser.getPassword().getBytes()));
97
98         // check repeat
99         XxlJobUser existUser = xxlJobUserDao.loadByUserName(xxlJobUser.getUsername());
100         if (existUser != null) {
101             return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("user_username_repeat") );
102         }
103
104         // write
105         xxlJobUserDao.save(xxlJobUser);
106         return ReturnT.SUCCESS;
107     }
108
109     @RequestMapping("/update")
110     @ResponseBody
111     @PermissionLimit(adminuser = true)
112     public ReturnT<String> update(HttpServletRequest request, XxlJobUser xxlJobUser) {
113
114         // avoid opt login seft
115         XxlJobUser loginUser = (XxlJobUser) request.getAttribute(LoginService.LOGIN_IDENTITY_KEY);
116         if (loginUser.getUsername().equals(xxlJobUser.getUsername())) {
117             return new ReturnT<String>(ReturnT.FAIL.getCode(), I18nUtil.getString("user_update_loginuser_limit"));
118         }
119
120         // valid password
121         if (StringUtils.hasText(xxlJobUser.getPassword())) {
122             xxlJobUser.setPassword(xxlJobUser.getPassword().trim());
123             if (!(xxlJobUser.getPassword().length()>=4 && xxlJobUser.getPassword().length()<=20)) {
124                 return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("system_lengh_limit")+"[4-20]" );
125             }
126             // md5 password
127             xxlJobUser.setPassword(DigestUtils.md5DigestAsHex(xxlJobUser.getPassword().getBytes()));
128         } else {
129             xxlJobUser.setPassword(null);
130         }
131
132         // write
133         xxlJobUserDao.update(xxlJobUser);
134         return ReturnT.SUCCESS;
135     }
136
137     @RequestMapping("/remove")
138     @ResponseBody
139     @PermissionLimit(adminuser = true)
140     public ReturnT<String> remove(HttpServletRequest request, int id) {
141
142         // avoid opt login seft
143         XxlJobUser loginUser = (XxlJobUser) request.getAttribute(LoginService.LOGIN_IDENTITY_KEY);
144         if (loginUser.getId() == id) {
145             return new ReturnT<String>(ReturnT.FAIL.getCode(), I18nUtil.getString("user_update_loginuser_limit"));
146         }
147
148         xxlJobUserDao.delete(id);
149         return ReturnT.SUCCESS;
150     }
151
152     @RequestMapping("/updatePwd")
153     @ResponseBody
154     public ReturnT<String> updatePwd(HttpServletRequest request, String password){
155
156         // valid password
157         if (password==null || password.trim().length()==0){
158             return new ReturnT<String>(ReturnT.FAIL.getCode(), "密码不可为空");
159         }
160         password = password.trim();
161         if (!(password.length()>=4 && password.length()<=20)) {
162             return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("system_lengh_limit")+"[4-20]" );
163         }
164
165         // md5 password
166         String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
167
168         // update pwd
169         XxlJobUser loginUser = (XxlJobUser) request.getAttribute(LoginService.LOGIN_IDENTITY_KEY);
170
171         // do write
172         XxlJobUser existUser = xxlJobUserDao.loadByUserName(loginUser.getUsername());
173         existUser.setPassword(md5Password);
174         xxlJobUserDao.update(existUser);
175
176         return ReturnT.SUCCESS;
177     }
178
179 }