adjusted PUT methods for updating backend users.
This commit is contained in:
@@ -68,6 +68,16 @@ public class AdminController {
|
|||||||
return ResponseEntity.ok(userService.getAllDecryptedUsers());
|
return ResponseEntity.ok(userService.getAllDecryptedUsers());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PutMapping("/users/{userId}")
|
||||||
|
public ResponseEntity<UserDTO> updateUser(
|
||||||
|
@PathVariable Long userId,
|
||||||
|
@RequestBody UserDTO userDto,
|
||||||
|
@AuthenticationPrincipal CurrentUser user
|
||||||
|
) {
|
||||||
|
UserDTO updatedUser = userService.updateUser(userId, userDto);
|
||||||
|
return ResponseEntity.ok(updatedUser);
|
||||||
|
}
|
||||||
|
|
||||||
@PutMapping("/users/{userId}/enabled")
|
@PutMapping("/users/{userId}/enabled")
|
||||||
public ResponseEntity<Void> setUserEnabled(
|
public ResponseEntity<Void> setUserEnabled(
|
||||||
@PathVariable Long userId,
|
@PathVariable Long userId,
|
||||||
|
|||||||
@@ -169,4 +169,59 @@ public class UserService implements CustomUserDetailsService {
|
|||||||
userAuthRepository.save(user);
|
userAuthRepository.save(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public UserDTO updateUser(Long userId, UserDTO userDto) {
|
||||||
|
UserAuth user = userAuthRepository.findById(userId)
|
||||||
|
.orElseThrow(() -> new UsernameNotFoundException("User not found with id: " + userId));
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Update encrypted fields
|
||||||
|
if (userDto.getDisplayName() != null) {
|
||||||
|
user.setDisplayNameHash(encryptionService.encryptData(userDto.getDisplayName()));
|
||||||
|
}
|
||||||
|
if (userDto.getFirstName() != null) {
|
||||||
|
user.setFirstNameHash(encryptionService.encryptData(userDto.getFirstName()));
|
||||||
|
}
|
||||||
|
if (userDto.getLastName() != null) {
|
||||||
|
user.setLastNameHash(encryptionService.encryptData(userDto.getLastName()));
|
||||||
|
}
|
||||||
|
if (userDto.getEmail() != null) {
|
||||||
|
user.setEmailHash(encryptionService.encryptData(userDto.getEmail()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update role
|
||||||
|
if (userDto.getRole() != null) {
|
||||||
|
user.setRole(userDto.getRole());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update enabled state
|
||||||
|
user.setEnabled(userDto.isEnabled());
|
||||||
|
|
||||||
|
// Update client if provided
|
||||||
|
if (userDto.getClientIdentifier() != null) {
|
||||||
|
Client client = clientRepository.findByClientIdentifier(userDto.getClientIdentifier())
|
||||||
|
.orElseThrow(() -> new IllegalArgumentException("Client not found with identifier: " + userDto.getClientIdentifier()));
|
||||||
|
user.setClient(client);
|
||||||
|
}
|
||||||
|
|
||||||
|
UserAuth savedUser = userAuthRepository.save(user);
|
||||||
|
|
||||||
|
// Return updated DTO
|
||||||
|
return new UserDTO(
|
||||||
|
savedUser.getId(),
|
||||||
|
savedUser.getUsername(),
|
||||||
|
encryptionService.decryptData(savedUser.getDisplayNameHash()),
|
||||||
|
encryptionService.decryptData(savedUser.getFirstNameHash()),
|
||||||
|
encryptionService.decryptData(savedUser.getLastNameHash()),
|
||||||
|
encryptionService.decryptData(savedUser.getEmailHash()),
|
||||||
|
savedUser.getRole(),
|
||||||
|
savedUser.getClient().getClientIdentifier(),
|
||||||
|
encryptionService.decryptData(savedUser.getClient().getClientNameEncrypted()),
|
||||||
|
savedUser.isEnabled()
|
||||||
|
);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("Failed to update user: " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user