Skip to content

Commit

Permalink
IM-562 fix: Bug with uppercase/lowercase names
Browse files Browse the repository at this point in the history
Use findUserNameignoreCase instead findUserName and change keycloakSecurityAdapter to use check loweracse username
  • Loading branch information
kristinaBc3 committed Feb 7, 2025
1 parent 07a89f6 commit abbb9d7
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public List<Task> build(TaskParameters parameters) {
} else {
throw new ClassCastException();
}
User user = userRepository.findByName(param.username).orElseThrow();
User user = userRepository.findByNameIgnoreCase(param.username).orElseThrow();
ArrayList<Task> ret = new ArrayList<Task>(2);

Set<GroupEntry> optIn = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private void checkUserName(EmailTemplate emailTemplate) {
if (emailTemplate.getAuthorUsername() == null) {
emailTemplate.setAuthorUsername(profileService.getCurrentUserProfile(false).getUsername());
} else {
Optional<User> user = userRepository.findByName(emailTemplate.getAuthorUsername());
Optional<User> user = userRepository.findByNameIgnoreCase(emailTemplate.getAuthorUsername());
if (!user.isPresent()) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
public interface UserRepository extends ResourceRepository<User, String>{

Optional<User> findById(String id);

Optional<User> findByName(String username); // need exactly the username

default Optional<User> findByName(String username) {
return findByNameIgnoreCase(username);
}; // need exactly the username

Optional<User> findByNameIgnoreCase(String username);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public boolean isUser(String userName) {

String preferredUsername = principal.getKeycloakSecurityContext().getToken().getPreferredUsername();
if (preferredUsername != null) {
return preferredUsername.equals(userName);
return preferredUsername.equals(userName.toLowerCase());
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,41 +182,6 @@ public class UserProfileController {

}

// TODO keycloak
// @PutMapping(value = API.HUB.USER_BASE_ID, params = API.HUB.PARAMETERS.USER_SET_EMAIL)
// public ResponseEntity< ? > updateUserEmail(@PathVariable String id,
// @RequestParam(API.HUB.PARAMETERS.USER_SET_EMAIL) String setPassword,
// @RequestBody UpdateEmailRequest updateEmailRequest) {
//
// /* Check user and password are correct */
// try {
// userAuthService.getAuthResponse(updateEmailRequest.getUsername(), updateEmailRequest.getPassword(),
// updateEmailRequest.isRemote());
// } catch (Exception e) {
// return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Incorrect password.");
// }
//
// /* Check token is correct */
// TokenVerifyEmailClickback token = (TokenVerifyEmailClickback) tokenService.getAndVerifyToken(id,
// updateEmailRequest.getToken(), TokenType.verifyEmail);
// if (token == null) {
// throw new ActivationTokenFailedException("User Verification token failed");
// }
//
// /* Update user email */
// try {
// userService.updateUserEmail(id, updateEmailRequest.getEmail());
//
// tokenService.deleteToken(token.getTokenString());
//
// } catch (Exception e) {
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
// }
//
// JSONObject resp = new JSONObject();
// return new ResponseEntity<JSONObject>(resp, HttpStatus.CREATED);
// }

@GetMapping(value = API.HUB.USER_BASE_ID, params = "remote-login")
@PreAuthorize("@securityService.isUser(#id)")
public ResponseEntity< ? > getFullUserProfile(@PathVariable String id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class UserTagServiceImpl implements UserTagService {
TagNotificationRepository notificationRepository;

private User findUserByName(String username) {
return userRepository.findByName(username).orElseThrow(() -> new BadRequestException("User is not present."));
return userRepository.findByNameIgnoreCase(username).orElseThrow(() -> new BadRequestException("User is not present."));
}

private boolean doesTagExistInTheDatabase(MongoTag tag) {
Expand Down

0 comments on commit abbb9d7

Please sign in to comment.