diff --git a/src/main/java/com/psg/dlsysinfo/dl_sysinfo_server/controller/AdminController.java b/src/main/java/com/psg/dlsysinfo/dl_sysinfo_server/controller/AdminController.java index 0e97e57..5863378 100644 --- a/src/main/java/com/psg/dlsysinfo/dl_sysinfo_server/controller/AdminController.java +++ b/src/main/java/com/psg/dlsysinfo/dl_sysinfo_server/controller/AdminController.java @@ -68,6 +68,16 @@ public class AdminController { return ResponseEntity.ok(userService.getAllDecryptedUsers()); } + @PutMapping("/users/{userId}") + public ResponseEntity 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 setUserEnabled( @PathVariable Long userId, diff --git a/src/main/java/com/psg/dlsysinfo/dl_sysinfo_server/service/UserService.java b/src/main/java/com/psg/dlsysinfo/dl_sysinfo_server/service/UserService.java index 46fc846..5aa4213 100644 --- a/src/main/java/com/psg/dlsysinfo/dl_sysinfo_server/service/UserService.java +++ b/src/main/java/com/psg/dlsysinfo/dl_sysinfo_server/service/UserService.java @@ -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); + } + } + }