|
15 | 15 | import org.springframework.web.bind.annotation.RequestMapping;
|
16 | 16 | import org.springframework.web.bind.annotation.RestController;
|
17 | 17 | import com.digitalsanctuary.spring.user.audit.AuditEvent;
|
| 18 | +import com.digitalsanctuary.spring.user.dto.PasswordDto; |
18 | 19 | import com.digitalsanctuary.spring.user.dto.UserDto;
|
19 | 20 | import com.digitalsanctuary.spring.user.event.OnRegistrationCompleteEvent;
|
| 21 | +import com.digitalsanctuary.spring.user.exceptions.InvalidOldPasswordException; |
20 | 22 | import com.digitalsanctuary.spring.user.exceptions.UserAlreadyExistException;
|
21 | 23 | import com.digitalsanctuary.spring.user.persistence.model.User;
|
22 | 24 | import com.digitalsanctuary.spring.user.service.DSUserDetails;
|
@@ -146,6 +148,40 @@ public ResponseEntity<JSONResponse> resetPassword(@Valid @RequestBody UserDto us
|
146 | 148 | return buildSuccessResponse("If account exists, password reset email has been sent!", forgotPasswordPendingURI);
|
147 | 149 | }
|
148 | 150 |
|
| 151 | + /** |
| 152 | + * Updates the user's password. This is used when the user is logged in and wants to change their password. |
| 153 | + * |
| 154 | + * @param userDetails the authenticated user details |
| 155 | + * @param passwordDto the password data transfer object containing the old and new passwords |
| 156 | + * @param request the HTTP servlet request |
| 157 | + * @param locale the locale |
| 158 | + * @return a ResponseEntity containing a JSONResponse with the password update result |
| 159 | + */ |
| 160 | + @PostMapping("/updatePassword") |
| 161 | + public ResponseEntity<JSONResponse> updatePassword(@AuthenticationPrincipal DSUserDetails userDetails, |
| 162 | + @Valid @RequestBody PasswordDto passwordDto, HttpServletRequest request, Locale locale) { |
| 163 | + validateAuthenticatedUser(userDetails); |
| 164 | + User user = userDetails.getUser(); |
| 165 | + |
| 166 | + try { |
| 167 | + if (!userService.checkIfValidOldPassword(user, passwordDto.getOldPassword())) { |
| 168 | + throw new InvalidOldPasswordException("Invalid old password"); |
| 169 | + } |
| 170 | + |
| 171 | + userService.changeUserPassword(user, passwordDto.getNewPassword()); |
| 172 | + logAuditEvent("PasswordUpdate", "Success", "User password updated", user, request); |
| 173 | + |
| 174 | + return buildSuccessResponse(messages.getMessage("message.update-password.success", null, locale), null); |
| 175 | + } catch (InvalidOldPasswordException ex) { |
| 176 | + logAuditEvent("PasswordUpdate", "Failure", "Invalid old password", user, request); |
| 177 | + return buildErrorResponse(messages.getMessage("message.update-password.invalid-old", null, locale), 1, HttpStatus.BAD_REQUEST); |
| 178 | + } catch (Exception ex) { |
| 179 | + log.error("Unexpected error during password update.", ex); |
| 180 | + logAuditEvent("PasswordUpdate", "Failure", ex.getMessage(), user, request); |
| 181 | + return buildErrorResponse("System Error!", 5, HttpStatus.INTERNAL_SERVER_ERROR); |
| 182 | + } |
| 183 | + } |
| 184 | + |
149 | 185 | /**
|
150 | 186 | * Deletes the user's account. This is used when the user wants to delete their account. This will either delete the account or disable it based
|
151 | 187 | * on the configuration of the actuallyDeleteAccount property. After the account is disabled or deleted, the user will be logged out.
|
|
0 commit comments