Skip to content

Commit

Permalink
Fix fetching user reference for change metadata
Browse files Browse the repository at this point in the history
- avoid exception when fetching user by username
  • Loading branch information
kostobog authored and blcham committed Sep 13, 2024
1 parent 34526b4 commit 40949f5
Showing 1 changed file with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public SecurityUtils(UserDao userDao, SecurityConf config) {
* If the user is impersonating another user, the impersonated user is returned.
* Otherwise, the currently authenticated user is returned.
*
* @return
* @return the instance of the User class representing the currently authenticated user
*/
public User getCurrentUser() {
final SecurityContext context = SecurityContextHolder.getContext();
Expand All @@ -53,9 +53,21 @@ public User getCurrentUser() {
}
}

public String getCurrentUsername(){
final SecurityContext context = SecurityContextHolder.getContext();
assert context != null;
final Object principal = context.getAuthentication().getPrincipal();
if (principal instanceof Jwt) {
final OidcUserInfo userInfo = new OidcUserInfo(((Jwt)principal).getClaims());
return userInfo.getPreferredUsername();
} else {
return context.getAuthentication().getName();
}
}

public UserReference getCurrentUserReference() {
User user = getCurrentUser();
return new UserReference(user);
String username = getCurrentUsername();
return userDao.findUserReferenceByUsername(username);
}

// TODO map role, but I am not sure which changes in the model when be required if I add addRole method to User
Expand Down

0 comments on commit 40949f5

Please sign in to comment.