zj
2024-06-03 3603ecb207f7e712c635f19531e05fac4d19e53f
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
package project.web.admin.systemuser;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
 
import kernel.exception.BusinessException;
import kernel.util.JsonUtils;
import kernel.util.StringUtils;
import kernel.web.PageActionSupport;
import project.Constants;
import security.Role;
import security.RoleService;
import systemuser.AdminRoleAuthorityService;
import util.RegexUtil;
 
/**
 * 角色管理
 */
@RestController
public class AdminRoleAuthorityController extends PageActionSupport {
 
    private Logger logger=LoggerFactory.getLogger(AdminRoleAuthorityController.class);
 
    @Autowired
    private AdminRoleAuthorityService adminRoleAuthorityService;
    @Autowired
    private RoleService roleService;
    
    private final String action = "normal/adminRoleAuthorityAction!";
 
    /**
     * 获取角色列表
     */
    @RequestMapping(action + "list.action")
    public ModelAndView list(HttpServletRequest request) {
        String message = request.getParameter("message");
        String error = request.getParameter("error");
 
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("role_authority_manage_list");
        
        try {
            
            List<Map<String, Object>> datas = this.adminRoleAuthorityService.getAllRole();
 
            for (Map<String, Object> data : datas) {
                // 过滤假分核查
                if (!"root".equals(this.getUsername_login()) && data.get("names") != null) {
                    // 排在中间或结尾
                    data.put("names", data.get("names").toString().replace(", 假分核查", ""));
                    // 排在开头后面还有
                    data.put("names", data.get("names").toString().replace("假分核查 ,", ""));
                    // 单独一个
                    data.put("names", data.get("names").toString().replace("假分核查", ""));
                }
                
                String roleName = data.get("roleName").toString();
                
                data.put("roleName", Constants.ROLE_MAP.containsKey(roleName) ? Constants.ROLE_MAP.get(roleName) : roleName);
                
                if (Constants.ROLE_MAP.containsKey(roleName)) {
                    data.put("is_default_role", "1");
                } else {
                    data.put("is_default_role", "0");
                }                
            }
 
            modelAndView.addObject("datas", datas);
            
        } catch (BusinessException e) {
            modelAndView.addObject("error", e.getMessage());
            return modelAndView;
        } catch (Throwable t) {
            logger.error(" error ", t);
            modelAndView.addObject("error", "[ERROR] " + t.getMessage());
            return modelAndView;
        }
 
        modelAndView.addObject("message", message);
        modelAndView.addObject("error", error);
        return modelAndView;
    }
 
    /**
     * 新增角色 页面
     */
    @RequestMapping(action + "toAdd.action")
    public ModelAndView toAdd() {
        return new ModelAndView("role_authority_manage_add");
    }
 
    /**
     * 新增角色
     */
    @RequestMapping(action + "add.action")
    public ModelAndView add(HttpServletRequest request) {
        String roleName = request.getParameter("roleName");
 
        ModelAndView modelAndView = new ModelAndView();
 
        try {
 
            String error = this.votify(roleName);
            if (StringUtils.isNotEmpty(error)) {
                throw new BusinessException(error);
            }
            
            Role role = new Role();
            role.setId("SECURITY_ROLE_" + roleName.toUpperCase());
            role.setRoleName(roleName.toUpperCase());
            
            this.roleService.addRole(role, this.getUsername_login(), this.getIp());
 
        } catch (BusinessException e) {
            modelAndView.addObject("error", e.getMessage());
            modelAndView.addObject("roleName", roleName);
            modelAndView.setViewName("role_authority_manage_add");
            return modelAndView;
        } catch (Throwable t) {
            logger.error("add error ", t);
            modelAndView.addObject("error", "程序错误");
            modelAndView.addObject("roleName", roleName);
            modelAndView.setViewName("role_authority_manage_add");
            return modelAndView;
        }
        
        modelAndView.addObject("message", "操作成功");
        modelAndView.setViewName("redirect:/" + action + "list.action");
        return modelAndView;
    }
 
    /**
     * 更新角色
     */
    @RequestMapping(action + "update.action")
    public ModelAndView update(HttpServletRequest request) {
        String id = request.getParameter("id");
        String[] role_resource = request.getParameterValues("role_resource");
        String login_safeword = request.getParameter("login_safeword");
        String email_code = request.getParameter("email_code");
        String super_google_auth_code = request.getParameter("super_google_auth_code");
 
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/" + action + "list.action");
 
        try {
            
            String role_resource_str = "";
            if (null != role_resource && 0 != role_resource.length) {
                role_resource_str = String.join(",", role_resource);
            }
 
            this.adminRoleAuthorityService.updateRoleResource(id, role_resource_str, this.getUsername_login(),
                    login_safeword, email_code, this.getIp(), super_google_auth_code);
 
        } catch (BusinessException e) {
            modelAndView.addObject("error", e.getMessage());
            return modelAndView;
        } catch (Throwable t) {
            logger.error("update error ", t);
            this.error = "程序错误";
            modelAndView.addObject("error", "程序错误");
            return modelAndView;
        }
        
        modelAndView.addObject("message", "操作成功");
        return modelAndView;
    }
 
    /**
     * 删除角色
     */
    @RequestMapping(action + "delete.action")
    public ModelAndView delete(HttpServletRequest request) {
        String id = request.getParameter("id");
        String login_safeword = request.getParameter("login_safeword");
        String email_code = request.getParameter("email_code");
        String super_google_auth_code = request.getParameter("super_google_auth_code");
 
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/" + action + "list.action");
 
        try {
 
            this.adminRoleAuthorityService.delete(id, this.getUsername_login(), login_safeword, email_code,
                    this.getIp(), super_google_auth_code);
 
        } catch (BusinessException e) {
            modelAndView.addObject("error", e.getMessage());
            return modelAndView;
        } catch (Throwable t) {
            logger.error("update error ", t);
            modelAndView.addObject("error", "程序错误");
            return modelAndView;
        }
        
        modelAndView.addObject("message", "操作成功");
        return modelAndView;
    }
 
    /**
     * resources
     */
    @RequestMapping(action + "resources.action")
    public String resources(HttpServletRequest request) {
        String id = request.getParameter("id");
 
        Map<String, Object> resultMap = new HashMap<String, Object>();
 
        try {
 
            resultMap.put("code", 200);
 
            List<Map<String, Object>> allResources = this.adminRoleAuthorityService.getResourceName(null);
            List<Map<String, Object>> newAllResources = new ArrayList<Map<String, Object>>();
            
            for (Map<String, Object> data : allResources) {
                // 过滤假分核查
                if (!"root".equals(this.getUsername_login())
                        && "SECURITY_USER_RECORD".equals(data.get("set_id").toString())) {
//                    allResources.remove(data);
//                    break;
                    continue;
                }
                // 过滤空名称的
                if (data.get("name") == null || !StringUtils.isNotEmpty(data.get("name").toString())
                        || "null".equals(data.get("name").toString())) {
//                    allResources.remove(data);
                    continue;
                }
                newAllResources.add(data);
            }
 
            List<String> roleResourceMappingIdById = this.adminRoleAuthorityService.getRoleResourceMappingIdById(id);
            // 过滤假分核查
            if (!"root".equals(this.getUsername_login()) && !CollectionUtils.isEmpty(roleResourceMappingIdById)) {
                roleResourceMappingIdById.remove("SECURITY_USER_RECORD");
            }
            
            resultMap.put("all_resources", newAllResources);
            resultMap.put("checked_resources",
                    String.join(",", this.adminRoleAuthorityService.getRoleResourceMappingIdById(id)));
 
        } catch (BusinessException e) {
            resultMap.put("code", 500);
            resultMap.put("message", e.getMessage());
        } catch (Throwable t) {
            logger.error(" error ", t);
            resultMap.put("code", 500);
            resultMap.put("message", "程序错误");
        }        
 
        return JsonUtils.getJsonString(resultMap);
    }
 
    private String votify(String roleName) {
        if (!RegexUtil.isEnglish(roleName)) {
            return "[角色]请输入英文";
        }
        return null;
    }
 
}