|
30 | 30 | import javax.validation.Valid; |
31 | 31 | import java.net.URI; |
32 | 32 | import java.util.List; |
| 33 | +import java.util.NoSuchElementException; |
33 | 34 | import java.util.Optional; |
34 | 35 | import java.util.stream.Collectors; |
35 | 36 |
|
@@ -137,28 +138,49 @@ public ResponseEntity<UserDTO> updateUser(@Valid @RequestBody UserDTO userDTO) { |
137 | 138 | try { |
138 | 139 | Optional<User> existingUser = userRepository.findOneByEmailIgnoreCase(userDTO.getEmail()); |
139 | 140 | if (existingUser.isPresent() && (!existingUser.get() |
140 | | - .getId() |
141 | | - .equals(userDTO.getId()))) { |
| 141 | + .getId() |
| 142 | + .equals(userDTO.getId()))) { |
142 | 143 | throw new EmailAlreadyUsedException(); |
143 | 144 | } |
144 | 145 | existingUser = userRepository.findOneByLogin(userDTO.getLogin() |
145 | | - .toLowerCase()); |
| 146 | + .toLowerCase()); |
146 | 147 | if (existingUser.isPresent() && (!existingUser.get() |
147 | | - .getId() |
148 | | - .equals(userDTO.getId()))) { |
| 148 | + .getId() |
| 149 | + .equals(userDTO.getId()))) { |
149 | 150 | throw new LoginAlreadyUsedException(); |
150 | 151 | } |
| 152 | + |
| 153 | + User user = userRepository.findOneWithAuthoritiesById(userDTO.getId()) |
| 154 | + .orElseThrow(() -> new NoSuchElementException(String.format("User %1$s not found", userDTO.getId().toString()))); |
| 155 | + if (!userDTO.getAuthorities().contains("ROLE_ADMIN") && |
| 156 | + user.getAuthorities().stream().anyMatch(authority -> authority.getName().equals("ROLE_ADMIN")) && userRepository.countAdmins() == 1) { |
| 157 | + throw new BadRequestAlertException(ctx, "Cannot update roles for the last remaining admin user.", UserService.class.toString()); |
| 158 | + } |
| 159 | + |
151 | 160 | Optional<UserDTO> updatedUser = userService.updateUser(userDTO); |
152 | 161 |
|
153 | 162 | return ResponseUtil.wrapOrNotFound(updatedUser, |
154 | | - HeaderUtil.createAlert("A user is updated with identifier " + userDTO.getLogin(), |
155 | | - userDTO.getLogin())); |
| 163 | + HeaderUtil.createAlert("A user is updated with identifier " + userDTO.getLogin(), |
| 164 | + userDTO.getLogin())); |
| 165 | + |
| 166 | + } catch (NoSuchElementException e) { |
| 167 | + String msg = ctx + ": " + e.getMessage(); |
| 168 | + log.error(msg); |
| 169 | + applicationEventService.createEvent(msg, ApplicationEventType.ERROR); |
| 170 | + return ResponseEntity.status(HttpStatus.NOT_FOUND).headers( |
| 171 | + HeaderUtil.createFailureAlert("", "", msg)).body(null); |
| 172 | + } catch (BadRequestAlertException e) { |
| 173 | + String msg = ctx + ": " + e.getMessage(); |
| 174 | + log.error(msg); |
| 175 | + applicationEventService.createEvent(msg, ApplicationEventType.ERROR); |
| 176 | + return ResponseEntity.status(HttpStatus.BAD_REQUEST).headers( |
| 177 | + HeaderUtil.createFailureAlert("", "", msg)).body(null); |
156 | 178 | } catch (Exception e) { |
157 | 179 | String msg = ctx + ": " + e.getMessage(); |
158 | 180 | log.error(msg); |
159 | 181 | applicationEventService.createEvent(msg, ApplicationEventType.ERROR); |
160 | 182 | return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).headers( |
161 | | - HeaderUtil.createFailureAlert("", "", msg)).body(null); |
| 183 | + HeaderUtil.createFailureAlert("", "", msg)).body(null); |
162 | 184 | } |
163 | 185 | } |
164 | 186 |
|
@@ -239,7 +261,7 @@ public ResponseEntity<Void> deleteUser(@PathVariable String login) { |
239 | 261 | return ResponseEntity.ok() |
240 | 262 | .headers(HeaderUtil.createAlert("A user is deleted with identifier " + login, login)) |
241 | 263 | .build(); |
242 | | - } catch (NoSuchMethodException e) { |
| 264 | + } catch (NoSuchElementException e) { |
243 | 265 | String msg = ctx + ": " + e.getMessage(); |
244 | 266 | log.error(msg); |
245 | 267 | applicationEventService.createEvent(msg, ApplicationEventType.ERROR); |
|
0 commit comments