Skip to content

Commit

Permalink
fix: handle missing security info
Browse files Browse the repository at this point in the history
Prevents failures if CurrentIdentityAssociation bean is not present.

Fixes #963
  • Loading branch information
mcollovati committed Sep 24, 2024
1 parent c5fa10f commit 22438a0
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,26 @@ public static Class<?> classUtils_getUserClass(Class<?> clazz) {
}

public static Principal authenticationUtil_getSecurityHolderAuthentication() {
SecurityIdentity identity = CurrentIdentityAssociation.current();
SecurityIdentity identity = currentIdentity();
if (identity != null && !identity.isAnonymous()) {
return identity.getPrincipal();
}
return null;
}

public static Function<String, Boolean> authenticationUtil_getSecurityHolderRoleChecker() {
SecurityIdentity identity = CurrentIdentityAssociation.current();
SecurityIdentity identity = currentIdentity();
if (identity == null || identity.isAnonymous()) {
return role -> false;
}
return role -> role != null && identity.hasRole(role);
}

private static SecurityIdentity currentIdentity() {
try {
return CurrentIdentityAssociation.current();
} catch (Exception e) {
return null;
}
}
}

0 comments on commit 22438a0

Please sign in to comment.