Skip to content

Commit

Permalink
Merge pull request #154 from kbss-cvut/fix/fix-fetching-user-referenc…
Browse files Browse the repository at this point in the history
…e-for-change-metadata

Fix fetching user reference for change metadata
  • Loading branch information
blcham authored Sep 13, 2024
2 parents 34526b4 + 40949f5 commit 28b4cf0
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 28b4cf0

Please sign in to comment.