-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserController.java
More file actions
240 lines (215 loc) · 11.4 KB
/
UserController.java
File metadata and controls
240 lines (215 loc) · 11.4 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
package io.github.patternatlas.api.rest.controller;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import io.github.patternatlas.api.entities.user.UserEntity;
import io.github.patternatlas.api.entities.user.role.Privilege;
import io.github.patternatlas.api.entities.user.role.Role;
import io.github.patternatlas.api.rest.model.user.PrivilegeModel;
import io.github.patternatlas.api.rest.model.user.RoleModel;
import io.github.patternatlas.api.rest.model.user.RoleModelRequest;
import io.github.patternatlas.api.rest.model.user.UserModel;
import io.github.patternatlas.api.rest.model.user.UserModelRequest;
import io.github.patternatlas.api.service.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/users", produces = { "application/hal+json", "application/json" })
public class UserController {
Logger logger = LoggerFactory.getLogger(UserController.class);
private final UserService userService;
private final PasswordEncoder passwordEncoder;
private ObjectMapper objectMapper;
public UserController(
UserService userService,
PasswordEncoder passwordEncoder,
ObjectMapper objectMapper
) {
this.userService = userService;
this.passwordEncoder = passwordEncoder;
this.objectMapper = objectMapper;
}
/**
* GET Methods
*/
// Exposes whole activity of each user (including created issues, candidates...). This should only be
// allowed for USER_READ_ALL, or USER_READ if it is for user themselves
@Operation(operationId = "getAllUsers", responses = {@ApiResponse(responseCode = "200")}, description = "Retrieve all users")
@GetMapping(value = "")
@PostFilter(value = "hasGlobalPermission(@PC.USER_READ_ALL) or " +
"(hasGlobalPermission(@PC.USER_READ) and filterObject.getContent().id.equals(loggedInUUID()))")
CollectionModel<EntityModel<UserModel>> getAll() {
List<EntityModel<UserModel>> users = this.userService.getAllUsers()
.stream()
.map(user -> new EntityModel<>(new UserModel(user)))
.collect(Collectors.toList());
// and )
return CollectionModel.of(users);
}
@GetMapping(value = "/{userId}")
ResponseEntity<EntityModel<UserModel>> getUserById(@PathVariable UUID userId) {
return ResponseEntity.ok(EntityModel.of(new UserModel(this.userService.getUserById(userId))));
}
/**
* Extended userinfo endpoint replaces endpoint in auth server.
* @param principal
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "/userinfo")
@ResponseBody
@PreAuthorize(value = "isLoggedIn()") // must be checked here since user needs to be logged in to get info
public Map<String, Object> user(Principal principal) {
if (principal != null) {
UUID id = UUID.fromString(principal.getName());
UserEntity user = this.userService.getUserById(id);
Map<String, Object> model = new HashMap<String, Object>();
model.put("name", user.getName());
model.put("id", user.getId());
model.put("role", user.getRoles().stream()
.map(Role::getName)
.collect(Collectors.toList()));
model.put("privileges", user.getRoles().stream()
.flatMap(role -> role.getPrivileges().stream())
.map(Privilege::getName)
.collect(Collectors.toList()));
return model;
}
return null;
}
@GetMapping(value = "/roles")
CollectionModel<EntityModel<RoleModel>> getAllRoles() {
List<EntityModel<RoleModel>> roles = this.userService.getAllRoles()
.stream()
.map(role -> EntityModel.of(new RoleModel(role)))
.collect(Collectors.toList());
return CollectionModel.of(roles);
}
@GetMapping(value = "/roles/platform")
CollectionModel<EntityModel<RoleModel>> getAllPlatformRoles() {
List<EntityModel<RoleModel>> roles = this.userService.getAllPlatformRoles()
.stream()
.map(role -> EntityModel.of(new RoleModel(role)))
.collect(Collectors.toList());
return CollectionModel.of(roles);
}
@GetMapping(value = "/roles/authors")
CollectionModel<EntityModel<RoleModel>> getAllAuthorRoles() {
List<EntityModel<RoleModel>> roles = this.userService.getAllAuthorRoles()
.stream()
.map(role -> EntityModel.of(new RoleModel(role)))
.collect(Collectors.toList());
return CollectionModel.of(roles);
}
@GetMapping(value = "/roles/{entityId}")
CollectionModel<EntityModel<RoleModel>> getAllRolesFromEntity(@PathVariable UUID entityId) {
List<EntityModel<RoleModel>> roles = this.userService.getAllRolesFromEntity(entityId)
.stream()
.map(role -> EntityModel.of(new RoleModel(role)))
.collect(Collectors.toList());
return CollectionModel.of(roles);
}
@GetMapping(value = "/roles/privileges")
CollectionModel<EntityModel<PrivilegeModel>> getAllPlatformPrivileges() {
List<EntityModel<PrivilegeModel>> privileges = this.userService.getAllPlatformPrivileges()
.stream()
.map(privilege -> EntityModel.of(new PrivilegeModel(privilege)))
.collect(Collectors.toList());
return CollectionModel.of(privileges);
}
@GetMapping(value = "/roles/default_author_privileges")
CollectionModel<EntityModel<PrivilegeModel>> getAllDefaultAuthorPrivileges() {
List<EntityModel<PrivilegeModel>> privileges = this.userService.getAllDefaultAuthorPrivileges()
.stream()
.map(privilege -> EntityModel.of(new PrivilegeModel(privilege)))
.collect(Collectors.toList());
return CollectionModel.of(privileges);
}
@GetMapping(value = "/roles/privileges/{entityId}")
CollectionModel<EntityModel<PrivilegeModel>> getAllPrivilegesFromEntity(@PathVariable UUID entityId) {
List<EntityModel<PrivilegeModel>> privileges = this.userService.getAllPrivilegesFromEntity(entityId)
.stream()
.map(privilege -> EntityModel.of(new PrivilegeModel(privilege)))
.collect(Collectors.toList());
return CollectionModel.of(privileges);
}
/**
* CREATE Methods
*/
@Operation(operationId = "createUser", responses = {@ApiResponse(responseCode = "200")}, description = "Create a user")
@PostMapping(value = "")
@ResponseStatus(HttpStatus.CREATED)
ResponseEntity<EntityModel<UserModel>> newUser(@RequestBody UserModelRequest userModelRequest) {
return ResponseEntity.ok(EntityModel.of(new UserModel(this.userService.createUser(userModelRequest))));
}
/**
* UPDATE Methods
*/
@Operation(operationId = "updateUser", responses = {@ApiResponse(responseCode = "200"), @ApiResponse(responseCode = "404", content = @Content)}, description = "Update a user")
@PutMapping(value = "/{userId}")
@ResponseStatus(HttpStatus.ACCEPTED)
ResponseEntity<EntityModel<UserModel>> updateUser(@PathVariable UUID userId, @RequestBody UserModelRequest userModelRequest) {
return ResponseEntity.ok(EntityModel.of(new UserModel(this.userService.updateUser(userId, userModelRequest))));
}
@Operation(operationId = "updatePlatformRole", responses = {@ApiResponse(responseCode = "200"), @ApiResponse(responseCode = "404", content = @Content)}, description = "Update platform wide roles for user")
@PutMapping(value = "/{userId}/role")
@ResponseStatus(HttpStatus.ACCEPTED)
ResponseEntity<EntityModel<UserModel>> updatePlatformRole(@PathVariable UUID userId, @RequestBody UserModelRequest userModelRequest) {
return ResponseEntity.ok(EntityModel.of(new UserModel(this.userService.updatePlatformRole(userId, userModelRequest))));
}
@PutMapping(value = "/roles/{roleId}/privileges/{privilegeId}")
@ResponseStatus(HttpStatus.ACCEPTED)
ResponseEntity<EntityModel<RoleModel>> updateUserRole(@PathVariable UUID roleId, @PathVariable UUID privilegeId, @RequestBody RoleModelRequest roleModelRequest) {
return ResponseEntity.ok(EntityModel.of(new RoleModel(this.userService.updateRole(roleId, privilegeId, roleModelRequest))));
}
/**
* This endpoint updates all resource specific roles that are related to the specified author role (HELPER, MAINTAINER or OWNER) according to the value of the specified default author privilege.
* e.g. the role is 'HELPER', the default author privilege is 'PATTERN_CANDIDATE_EDIT' and the checkboxValue of roleModelRequest is false. This
* means the privilege 'PATTERN_CANDIDATE_EDIT' should be removed from all 'HELPER' roles from all resources. If a resources has
* the ID 123, privilege 'PATTERN_CANDIDATE_EDIT_123' will be removed from the role 'HELPER_PATTERN_CANDIDATE_123'
*
* @param authorRoleId
* @param defaultAuthorPrivilegeId
* @param roleModelRequest
* @return
*/
@Operation(description = "Update all resource specific roles according to the value of the specified default author privilege")
@PutMapping(value = "/roles/{authorRoleId}/privileges/{defaultAuthorPrivilegeId}/all_resource_specific")
@ResponseStatus(HttpStatus.ACCEPTED)
void updateAllResourceSpecificAuthorRoles(@PathVariable UUID authorRoleId, @PathVariable UUID defaultAuthorPrivilegeId, @RequestBody RoleModelRequest roleModelRequest) {
this.userService.updateAllResourceSpecificRoles(authorRoleId, defaultAuthorPrivilegeId, roleModelRequest);
}
/**
* DELETE Methods
*/
@Operation(operationId = "deleteUser", responses = {@ApiResponse(responseCode = "200"), @ApiResponse(responseCode = "404", content = @Content)}, description = "Delete a user")
@DeleteMapping(value = "/{userId}")
ResponseEntity<?> deleteUser(@PathVariable UUID userId) {
this.userService.deleteUser(userId);
return ResponseEntity.noContent().build();
}
}