Skip to content

Commit

Permalink
Merge pull request #225 from integratedmodelling/IM-562-UPPERCASE-nam…
Browse files Browse the repository at this point in the history
…e-in-mong-ldap-keycloak

IM-562 fix: Bug with uppercase/lowercase names
  • Loading branch information
kristinaBc3 authored Feb 11, 2025
2 parents e84496d + e47eae4 commit 4061b86
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 42 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
6 changes: 5 additions & 1 deletion klab.hub/src/main/resources/application.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ keycloak:
bearer-only: true
hub:
env:
app-base-url: http://localhost:8284/hub
# SPA MODE
#app-base-url: http://localhost:8284/hub/ui
#base-url: /hub
# DEVELOP MODE
app-base-url: http://localhost:8080/hub
base-url: http://localhost:8284/hub
environment: development
keycloak-url: http://localhost:8078
Expand Down

0 comments on commit 4061b86

Please sign in to comment.