adjusted PUT methods for updating backend users.
All checks were successful
Build & Deploy Backend / build (push) Successful in 51s
Build & Deploy Backend / deploy (push) Successful in 1s

This commit is contained in:
2025-10-29 11:55:07 +08:00
parent 988c5ad527
commit b0e1928ca3
2 changed files with 65 additions and 0 deletions

View File

@@ -68,6 +68,16 @@ public class AdminController {
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")
public ResponseEntity<Void> setUserEnabled(
@PathVariable Long userId,

View File

@@ -169,4 +169,59 @@ public class UserService implements CustomUserDetailsService {
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);
}
}
}