@Test @WithMockUser(USERNAME_USER) void updateCurrentUser_wrongUser() { User updatedUser = mock(User.class); when(updatedUser.getUsername()).thenReturn("wrong-username"); assertThrows( RuntimeException.class, () -> userAccountServiceImpl.updateCurrentUser(updatedUser)); }
@Override @PreAuthorize("hasAnyRole('ROLE_SU', 'ROLE_USER')") @Transactional public void updateCurrentUser(User updatedCurrentUser) { String currentUsername = SecurityUtils.getCurrentUsername(); if (!Objects.equals(currentUsername, updatedCurrentUser.getUsername())) { throw new RuntimeException("Updated user differs from the current user"); } User currentUser = userService.getUser(currentUsername); if (currentUser == null) { throw new RuntimeException("User does not exist [" + currentUsername + "]"); } userService.update(updatedCurrentUser); }
UserAccountServiceImpl implements UserAccountService { @Override @PreAuthorize("hasAnyRole('ROLE_SU', 'ROLE_USER')") @Transactional public void updateCurrentUser(User updatedCurrentUser) { String currentUsername = SecurityUtils.getCurrentUsername(); if (!Objects.equals(currentUsername, updatedCurrentUser.getUsername())) { throw new RuntimeException("Updated user differs from the current user"); } User currentUser = userService.getUser(currentUsername); if (currentUser == null) { throw new RuntimeException("User does not exist [" + currentUsername + "]"); } userService.update(updatedCurrentUser); } }
UserAccountServiceImpl implements UserAccountService { @Override @PreAuthorize("hasAnyRole('ROLE_SU', 'ROLE_USER')") @Transactional public void updateCurrentUser(User updatedCurrentUser) { String currentUsername = SecurityUtils.getCurrentUsername(); if (!Objects.equals(currentUsername, updatedCurrentUser.getUsername())) { throw new RuntimeException("Updated user differs from the current user"); } User currentUser = userService.getUser(currentUsername); if (currentUser == null) { throw new RuntimeException("User does not exist [" + currentUsername + "]"); } userService.update(updatedCurrentUser); }  }
UserAccountServiceImpl implements UserAccountService { @Override @PreAuthorize("hasAnyRole('ROLE_SU', 'ROLE_USER')") @Transactional public void updateCurrentUser(User updatedCurrentUser) { String currentUsername = SecurityUtils.getCurrentUsername(); if (!Objects.equals(currentUsername, updatedCurrentUser.getUsername())) { throw new RuntimeException("Updated user differs from the current user"); } User currentUser = userService.getUser(currentUsername); if (currentUser == null) { throw new RuntimeException("User does not exist [" + currentUsername + "]"); } userService.update(updatedCurrentUser); }  @Override @Transactional(readOnly = true) User getCurrentUser(); @Override @PreAuthorize("hasAnyRole('ROLE_SU', 'ROLE_USER')") @Transactional void updateCurrentUser(User updatedCurrentUser); @Override @PreAuthorize("hasAnyRole('ROLE_SU', 'ROLE_USER')") @Transactional boolean validateCurrentUserPassword(String password); void setPasswordEncoder(PasswordEncoder passwordEncoder); }
UserAccountServiceImpl implements UserAccountService { @Override @PreAuthorize("hasAnyRole('ROLE_SU', 'ROLE_USER')") @Transactional public void updateCurrentUser(User updatedCurrentUser) { String currentUsername = SecurityUtils.getCurrentUsername(); if (!Objects.equals(currentUsername, updatedCurrentUser.getUsername())) { throw new RuntimeException("Updated user differs from the current user"); } User currentUser = userService.getUser(currentUsername); if (currentUser == null) { throw new RuntimeException("User does not exist [" + currentUsername + "]"); } userService.update(updatedCurrentUser); }  @Override @Transactional(readOnly = true) User getCurrentUser(); @Override @PreAuthorize("hasAnyRole('ROLE_SU', 'ROLE_USER')") @Transactional void updateCurrentUser(User updatedCurrentUser); @Override @PreAuthorize("hasAnyRole('ROLE_SU', 'ROLE_USER')") @Transactional boolean validateCurrentUserPassword(String password); void setPasswordEncoder(PasswordEncoder passwordEncoder);  }