Skip to content

Commit

Permalink
IM-182 feat:Update user with admin or system user. Update email if is…
Browse files Browse the repository at this point in the history
… changed.
  • Loading branch information
kristinaBc3 committed Feb 27, 2024
1 parent 536d899 commit 4162d9b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public ResponseEntity<?> getCurrentUserProfile(@RequestParam(required = false) b
}

@PutMapping(API.HUB.USER_BASE_ID)
@PreAuthorize("authentication.getPrincipal() == #id")
@PreAuthorize("authentication.getPrincipal() == #id or hasRole('ROLE_ADMINISTRATOR') or hasRole('ROLE_SYSTEM')")
public ResponseEntity<?> updateUserProfile(@PathVariable String id, @RequestBody UpdateUserRequest updateRequest) {
ProfileResource profile = userService.updateUserByProfile(updateRequest.getProfile());
return new ResponseEntity<>(profile, HttpStatus.ACCEPTED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public UserProfileServiceImpl(UserRepository userRepository, ObjectMapper object
@Override
public ProfileResource updateUserByProfile(ProfileResource profile) {
User user = updateUserFromProfileResource(profile);
if (!user.getEmail().equals(profile.getEmail())) {
if (userRepository.existsByEmailIgnoreCase(profile.getEmail())) {
throw new KlabException("Duplicated key. Email is already exists");
}
user.setEmail(profile.getEmail());
}
User updatedUser = new UpdateUser(user, userRepository).execute();
ProfileResource updatedProfile = objectMapper.convertValue(updatedUser, ProfileResource.class);
return updatedProfile.getSafeProfile();
Expand Down

0 comments on commit 4162d9b

Please sign in to comment.