This repository has been archived by the owner on May 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
RoleController.java
190 lines (177 loc) · 7.01 KB
/
RoleController.java
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
package com.learning.securedapp.web.controllers;
import com.learning.securedapp.domain.Permission;
import com.learning.securedapp.domain.Role;
import com.learning.securedapp.exception.SecuredAppException;
import com.learning.securedapp.security.SecurityUtil;
import com.learning.securedapp.web.services.SecurityService;
import com.learning.securedapp.web.validators.RoleValidator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
@Controller
@Slf4j
@Secured(SecurityUtil.MANAGE_ROLES)
@RequiredArgsConstructor
public class RoleController {
private static final String VIEWPREFIX = "roles/";
private final SecurityService securityService;
private final RoleValidator roleValidator;
/**
* permissionsList.
*
* @return a {@link java.util.List} object.
*/
@ModelAttribute("permissionsList")
public List<Permission> permissionsList() {
return securityService.getAllPermissions();
}
/**
* listRoles.
*
* @param model a {@link org.springframework.ui.Model} object.
* @return a {@link java.lang.String} object.
*/
@GetMapping(value = "/roles")
public String listRoles(Model model) {
List<Role> list = securityService.getAllRoles();
model.addAttribute("roles", list);
return VIEWPREFIX + "roles";
}
/**
* createRoleForm.
*
* @param model a {@link org.springframework.ui.Model} object.
* @return a {@link java.lang.String} object.
*/
@GetMapping(value = "/roles/new")
public String createRoleForm(Model model) {
Role role = Role.builder().build();
model.addAttribute("role", role);
// model.addAttribute("permissionsList",securityService.getAllPermissions());
return VIEWPREFIX + "create_role";
}
/**
* createRole.
*
* @param role a {@link com.learning.securedapp.domain.Role} object.
* @param result a {@link org.springframework.validation.BindingResult} object.
* @param model a {@link org.springframework.ui.Model} object.
* @param redirectAttributes a {@link
* org.springframework.web.servlet.mvc.support.RedirectAttributes} object.
* @return a {@link java.lang.String} object.
* @throws com.learning.securedapp.exception.SecuredAppException if any.
*/
@PostMapping(value = "/roles")
public String createRole(
@Valid @ModelAttribute("role") Role role,
BindingResult result,
Model model,
RedirectAttributes redirectAttributes)
throws SecuredAppException {
roleValidator.validate(role, result);
if (result.hasErrors()) {
return VIEWPREFIX + "create_role";
}
try {
Role persistedRole = securityService.createRole(role);
log.debug(
"Created new role with id : {} and name : {}",
persistedRole.getId(),
persistedRole.getRoleName());
redirectAttributes.addFlashAttribute(
"success", "Role " + persistedRole.getRoleName() + " created successfully");
} catch (SecuredAppException e) {
model.addAttribute("errorMessage", e.getMessage());
redirectAttributes.addFlashAttribute(
"error", "Role " + role.getRoleName() + " was not created");
throw new SecuredAppException("Role " + role.getRoleName() + " was not created", e);
}
return "redirect:/roles";
}
/**
* editRoleForm.
*
* @param id a {@link java.lang.String} object.
* @param model a {@link org.springframework.ui.Model} object.
* @return a {@link java.lang.String} object.
*/
@GetMapping(value = "/roles/{id}")
public String editRoleForm(@PathVariable String id, Model model) {
Role role = securityService.getRoleById(id);
Map<String, Permission> assignedPermissionMap = new HashMap<>();
List<Permission> permissions = role.getPermissions();
for (Permission permission : permissions) {
assignedPermissionMap.put(permission.getId(), permission);
}
List<Permission> rolePermissions = new ArrayList<>();
List<Permission> allPermissions = securityService.getAllPermissions();
for (Permission permission : allPermissions) {
if (assignedPermissionMap.containsKey(permission.getId())) {
rolePermissions.add(permission);
} else {
rolePermissions.add(null);
}
}
role.setPermissions(rolePermissions);
model.addAttribute("role", role);
// model.addAttribute("permissionsList",allPermissions);
return VIEWPREFIX + "edit_role";
}
/**
* updateRole.
*
* @param role a {@link com.learning.securedapp.domain.Role} object.
* @param result a {@link org.springframework.validation.BindingResult} object.
* @param model a {@link org.springframework.ui.Model} object.
* @param redirectAttributes a {@link
* org.springframework.web.servlet.mvc.support.RedirectAttributes} object.
* @return a {@link java.lang.String} object.
* @throws com.learning.securedapp.exception.SecuredAppException if any.
*/
@PostMapping(value = "/roles/{id}")
public String updateRole(
@ModelAttribute("role") Role role,
BindingResult result,
Model model,
RedirectAttributes redirectAttributes)
throws SecuredAppException {
try {
Role persistedRole = securityService.updateRole(role, role.getId());
log.debug(
"Updated role with id : {} and name : {}",
persistedRole.getId(),
persistedRole.getRoleName());
redirectAttributes.addFlashAttribute(
"success", "Role updated successfully for " + persistedRole.getRoleName());
} catch (SecuredAppException e) {
model.addAttribute("errorMessage", e.getMessage());
throw new SecuredAppException(e.getMessage());
}
return "redirect:/roles";
}
// Delete
/**
* delete.
*
* @param id a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
*/
@GetMapping("/roles/delete/{id}")
public String delete(@PathVariable String id) {
securityService.deleteRole(id);
return "redirect:/roles";
}
}