-
Notifications
You must be signed in to change notification settings - Fork 41
fix: improve user API message handling and fix detached entity bug #249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -155,14 +155,19 @@ public ResponseEntity<JSONResponse> updateUserAccount(@AuthenticationPrincipal D | |||||||||||||||||
| @Valid @RequestBody UserProfileUpdateDto profileUpdateDto, | ||||||||||||||||||
| HttpServletRequest request, Locale locale) { | ||||||||||||||||||
| validateAuthenticatedUser(userDetails); | ||||||||||||||||||
| User user = userDetails.getUser(); | ||||||||||||||||||
| // Re-fetch user from database to ensure we have an attached entity | ||||||||||||||||||
| User user = userService.findUserByEmail(userDetails.getUser().getEmail()); | ||||||||||||||||||
| if (user == null) { | ||||||||||||||||||
| log.error("User not found in database: {}", userDetails.getUser().getEmail()); | ||||||||||||||||||
| return buildErrorResponse(messages.getMessage("message.user.not-found", null, "User not found", locale), 1, HttpStatus.BAD_REQUEST); | ||||||||||||||||||
| } | ||||||||||||||||||
| user.setFirstName(profileUpdateDto.getFirstName()); | ||||||||||||||||||
| user.setLastName(profileUpdateDto.getLastName()); | ||||||||||||||||||
| userService.saveRegisteredUser(user); | ||||||||||||||||||
|
|
||||||||||||||||||
| logAuditEvent("ProfileUpdate", "Success", "User profile updated", user, request); | ||||||||||||||||||
|
|
||||||||||||||||||
| return buildSuccessResponse(messages.getMessage("message.update-user.success", null, locale), null); | ||||||||||||||||||
| return buildSuccessResponse(messages.getMessage("message.update-user.success", null, "Profile updated successfully", locale), null); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
|
|
@@ -206,7 +211,7 @@ public ResponseEntity<JSONResponse> savePassword(@Valid @RequestBody SavePasswor | |||||||||||||||||
| // are not a concern. Constant-time comparison is only needed when comparing | ||||||||||||||||||
| // against stored credentials, which is handled by Spring's PasswordEncoder. | ||||||||||||||||||
| if (!savePasswordDto.getNewPassword().equals(savePasswordDto.getConfirmPassword())) { | ||||||||||||||||||
| return buildErrorResponse(messages.getMessage("message.password.mismatch", null, locale), 1, | ||||||||||||||||||
| return buildErrorResponse(messages.getMessage("message.password.mismatch", null, "Passwords do not match", locale), 1, | ||||||||||||||||||
| HttpStatus.BAD_REQUEST); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -216,14 +221,14 @@ public ResponseEntity<JSONResponse> savePassword(@Valid @RequestBody SavePasswor | |||||||||||||||||
|
|
||||||||||||||||||
| if (tokenResult != UserService.TokenValidationResult.VALID) { | ||||||||||||||||||
| String messageKey = "auth.message." + tokenResult.getValue(); | ||||||||||||||||||
| return buildErrorResponse(messages.getMessage(messageKey, null, locale), 2, HttpStatus.BAD_REQUEST); | ||||||||||||||||||
| return buildErrorResponse(messages.getMessage(messageKey, null, "Invalid or expired token", locale), 2, HttpStatus.BAD_REQUEST); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // Get user by token | ||||||||||||||||||
| Optional<User> userOptional = userService.getUserByPasswordResetToken(savePasswordDto.getToken()); | ||||||||||||||||||
|
|
||||||||||||||||||
| if (userOptional.isEmpty()) { | ||||||||||||||||||
| return buildErrorResponse(messages.getMessage("auth.message.invalid", null, locale), 3, | ||||||||||||||||||
| return buildErrorResponse(messages.getMessage("auth.message.invalid", null, "Invalid token", locale), 3, | ||||||||||||||||||
| HttpStatus.BAD_REQUEST); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -246,7 +251,7 @@ public ResponseEntity<JSONResponse> savePassword(@Valid @RequestBody SavePasswor | |||||||||||||||||
|
|
||||||||||||||||||
| logAuditEvent("PasswordReset", "Success", "Password reset completed", user, request); | ||||||||||||||||||
|
|
||||||||||||||||||
| return buildSuccessResponse(messages.getMessage("message.reset-password.success", null, locale), | ||||||||||||||||||
| return buildSuccessResponse(messages.getMessage("message.reset-password.success", null, "Password has been reset successfully", locale), | ||||||||||||||||||
| "/user/login.html"); | ||||||||||||||||||
|
|
||||||||||||||||||
| } catch (Exception ex) { | ||||||||||||||||||
|
|
@@ -272,7 +277,12 @@ public ResponseEntity<JSONResponse> savePassword(@Valid @RequestBody SavePasswor | |||||||||||||||||
| public ResponseEntity<JSONResponse> updatePassword(@AuthenticationPrincipal DSUserDetails userDetails, | ||||||||||||||||||
| @Valid @RequestBody PasswordDto passwordDto, HttpServletRequest request, Locale locale) { | ||||||||||||||||||
| validateAuthenticatedUser(userDetails); | ||||||||||||||||||
| User user = userDetails.getUser(); | ||||||||||||||||||
| // Re-fetch user from database to ensure we have an attached entity | ||||||||||||||||||
| User user = userService.findUserByEmail(userDetails.getUser().getEmail()); | ||||||||||||||||||
| if (user == null) { | ||||||||||||||||||
| log.error("User not found in database: {}", userDetails.getUser().getEmail()); | ||||||||||||||||||
|
Comment on lines
+280
to
+283
|
||||||||||||||||||
| // Re-fetch user from database to ensure we have an attached entity | |
| User user = userService.findUserByEmail(userDetails.getUser().getEmail()); | |
| if (user == null) { | |
| log.error("User not found in database: {}", userDetails.getUser().getEmail()); | |
| // Use the User entity from the authenticated user details for consistency with other endpoints | |
| User user = userDetails.getUser(); | |
| if (user == null) { | |
| log.error("Authenticated user has no associated User entity"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The updatePassword method now calls userService.findUserByEmail(), but the existing unit tests don't mock this call. This will likely cause test failures. The tests should be updated to mock findUserByEmail to return the test user.