8240dc664cb1fa43e7ea30eb73c9b006cfd750e8.svn-base
9.42 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
/**
* 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.HttpServletResponse;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
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.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.Collections3;
import com.thinkgem.jeesite.common.utils.StringUtils;
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-12-05
*/
@Controller
@RequestMapping(value = "${adminPath}/sys/role")
public class RoleController extends BaseController {
@Autowired
private SystemService systemService;
@Autowired
private OfficeService officeService;
@ModelAttribute("role")
public Role get(@RequestParam(required=false) String id) {
if (StringUtils.isNotBlank(id)){
return systemService.getRole(id);
}else{
return new Role();
}
}
@RequiresPermissions("sys:role:view")
@RequestMapping(value = {"list", ""})
public String list(Role role, Model model) {
List<Role> list = systemService.findAllRole();
model.addAttribute("list", list);
return "modules/sys/roleList";
}
@RequiresPermissions("sys:role:view")
@RequestMapping(value = "form")
public String form(Role role, Model model) {
if (role.getOffice()==null){
role.setOffice(UserUtils.getUser().getOffice());
}
model.addAttribute("role", role);
model.addAttribute("menuList", systemService.findAllMenu());
model.addAttribute("officeList", officeService.findAll());
return "modules/sys/roleForm";
}
@RequiresPermissions("sys:role:edit")
@RequestMapping(value = "save")
public String save(Role role, Model model, RedirectAttributes redirectAttributes) {
if(!UserUtils.getUser().isAdmin()&&role.getSysData().equals(Global.YES)){
addMessage(redirectAttributes, "越权操作,只有超级管理员才能修改此数据!");
return "redirect:" + adminPath + "/sys/role/?repage";
}
if(Global.isDemoMode()){
addMessage(redirectAttributes, "演示模式,不允许操作!");
return "redirect:" + adminPath + "/sys/role/?repage";
}
if (!beanValidator(model, role)){
return form(role, model);
}
if (!"true".equals(checkName(role.getOldName(), role.getName()))){
addMessage(model, "保存角色'" + role.getName() + "'失败, 角色名已存在");
return form(role, model);
}
if (!"true".equals(checkEnname(role.getOldEnname(), role.getEnname()))){
addMessage(model, "保存角色'" + role.getName() + "'失败, 英文名已存在");
return form(role, model);
}
systemService.saveRole(role);
addMessage(redirectAttributes, "保存角色'" + role.getName() + "'成功");
return "redirect:" + adminPath + "/sys/role/?repage";
}
@RequiresPermissions("sys:role:edit")
@RequestMapping(value = "delete")
public String delete(Role role, RedirectAttributes redirectAttributes) {
if(!UserUtils.getUser().isAdmin() && role.getSysData().equals(Global.YES)){
addMessage(redirectAttributes, "越权操作,只有超级管理员才能修改此数据!");
return "redirect:" + adminPath + "/sys/role/?repage";
}
if(Global.isDemoMode()){
addMessage(redirectAttributes, "演示模式,不允许操作!");
return "redirect:" + adminPath + "/sys/role/?repage";
}
// if (Role.isAdmin(id)){
// addMessage(redirectAttributes, "删除角色失败, 不允许内置角色或编号空");
//// }else if (UserUtils.getUser().getRoleIdList().contains(id)){
//// addMessage(redirectAttributes, "删除角色失败, 不能删除当前用户所在角色");
// }else{
systemService.deleteRole(role);
addMessage(redirectAttributes, "删除角色成功");
// }
return "redirect:" + adminPath + "/sys/role/?repage";
}
/**
* 角色分配页面
* @param role
* @param model
* @return
*/
@RequiresPermissions("sys:role:edit")
@RequestMapping(value = "assign")
public String assign(Role role, Model model) {
List<User> userList = systemService.findUser(new User(new Role(role.getId())));
model.addAttribute("userList", userList);
return "modules/sys/roleAssign";
}
/**
* 角色分配 -- 打开角色分配对话框
* @param role
* @param model
* @return
*/
@RequiresPermissions("sys:role:view")
@RequestMapping(value = "usertorole")
public String selectUserToRole(Role role, Model model) {
List<User> userList = systemService.findUser(new User(new Role(role.getId())));
model.addAttribute("role", role);
model.addAttribute("userList", userList);
model.addAttribute("selectIds", Collections3.extractToString(userList, "name", ","));
model.addAttribute("officeList", officeService.findAll());
return "modules/sys/selectUserToRole";
}
/**
* 角色分配 -- 根据部门编号获取用户列表
* @param officeId
* @param response
* @return
*/
@RequiresPermissions("sys:role:view")
@ResponseBody
@RequestMapping(value = "users")
public List<Map<String, Object>> users(String officeId, HttpServletResponse response) {
List<Map<String, Object>> mapList = Lists.newArrayList();
User user = new User();
user.setOffice(new Office(officeId));
Page<User> page = systemService.findUser(new Page<User>(1, -1), user);
for (User e : page.getList()) {
Map<String, Object> map = Maps.newHashMap();
map.put("id", e.getId());
map.put("pId", 0);
map.put("name", e.getName());
mapList.add(map);
}
return mapList;
}
/**
* 角色分配 -- 从角色中移除用户
* @param userId
* @param roleId
* @param redirectAttributes
* @return
*/
@RequiresPermissions("sys:role:edit")
@RequestMapping(value = "outrole")
public String outrole(String userId, String roleId, RedirectAttributes redirectAttributes) {
if(Global.isDemoMode()){
addMessage(redirectAttributes, "演示模式,不允许操作!");
return "redirect:" + adminPath + "/sys/role/assign?id="+roleId;
}
Role role = systemService.getRole(roleId);
User user = systemService.getUser(userId);
if (UserUtils.getUser().getId().equals(userId)) {
addMessage(redirectAttributes, "无法从角色【" + role.getName() + "】中移除用户【" + user.getName() + "】自己!");
}else {
if (user.getRoleList().size() <= 1){
addMessage(redirectAttributes, "用户【" + user.getName() + "】从角色【" + role.getName() + "】中移除失败!这已经是该用户的唯一角色,不能移除。");
}else{
Boolean flag = systemService.outUserInRole(role, user);
if (!flag) {
addMessage(redirectAttributes, "用户【" + user.getName() + "】从角色【" + role.getName() + "】中移除失败!");
}else {
addMessage(redirectAttributes, "用户【" + user.getName() + "】从角色【" + role.getName() + "】中移除成功!");
}
}
}
return "redirect:" + adminPath + "/sys/role/assign?id="+role.getId();
}
/**
* 角色分配
* @param role
* @param idsArr
* @param redirectAttributes
* @return
*/
@RequiresPermissions("sys:role:edit")
@RequestMapping(value = "assignrole")
public String assignRole(Role role, String[] idsArr, RedirectAttributes redirectAttributes) {
if(Global.isDemoMode()){
addMessage(redirectAttributes, "演示模式,不允许操作!");
return "redirect:" + adminPath + "/sys/role/assign?id="+role.getId();
}
StringBuilder msg = new StringBuilder();
int newNum = 0;
for (int i = 0; i < idsArr.length; i++) {
User user = systemService.assignUserToRole(role, systemService.getUser(idsArr[i]));
if (null != user) {
msg.append("<br/>新增用户【" + user.getName() + "】到角色【" + role.getName() + "】!");
newNum++;
}
}
addMessage(redirectAttributes, "已成功分配 "+newNum+" 个用户"+msg);
return "redirect:" + adminPath + "/sys/role/assign?id="+role.getId();
}
/**
* 验证角色名是否有效
* @param oldName
* @param name
* @return
*/
@RequiresPermissions("user")
@ResponseBody
@RequestMapping(value = "checkName")
public String checkName(String oldName, String name) {
if (name!=null && name.equals(oldName)) {
return "true";
} else if (name!=null && systemService.getRoleByName(name) == null) {
return "true";
}
return "false";
}
/**
* 验证角色英文名是否有效
* @param oldName
* @param name
* @return
*/
@RequiresPermissions("user")
@ResponseBody
@RequestMapping(value = "checkEnname")
public String checkEnname(String oldEnname, String enname) {
if (enname!=null && enname.equals(oldEnname)) {
return "true";
} else if (enname!=null && systemService.getRoleByEnname(enname) == null) {
return "true";
}
return "false";
}
}