Skip to content
Snippets Groups Projects
Commit 52355689 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-3928 adjust currentUserId; fix order/modifierAccessor

parent c384bdaf
No related branches found
No related tags found
No related merge requests found
......@@ -34,6 +34,7 @@ import org.springframework.security.authentication.AuthenticationTrustResolverIm
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.jwt.Jwt;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
......@@ -41,20 +42,13 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class CurrentUserHelper {
static final String ROLE_PREFIX = "ROLE_";
private static final String SUB_CLAIM_KEY = "sub";
public static final Predicate<String> HAS_ROLE = CurrentUserHelper::hasRole;
private static final AuthenticationTrustResolver TRUST_RESOLVER = new AuthenticationTrustResolverImpl();
private static final Predicate<Authentication> TRUSTED = auth -> !TRUST_RESOLVER.isAnonymous(auth);
public static Authentication getAuthentication() {
return findAuthentication().orElseThrow(() -> new IllegalStateException("No authenticated User found"));
}
public static Optional<Authentication> findAuthentication() {
return Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication()).filter(TRUSTED);
}
public static boolean hasRole(String role) {
var auth = getAuthentication();
......@@ -82,7 +76,15 @@ public class CurrentUserHelper {
return containsRole(roleToCheck, authorities);
}
public static String prepareRoleForCheck(String role) {
static boolean containsRole(String role, Collection<? extends GrantedAuthority> authorities) {
return authorities.stream().anyMatch(a -> isAuthorityEquals(role, a.getAuthority()));
}
private static boolean isAuthorityEquals(String role, String authority) {
return StringUtils.equalsIgnoreCase(role, authority) || StringUtils.equalsIgnoreCase(prepareRoleForCheck(role), authority);
}
static String prepareRoleForCheck(String role) {
if ((Objects.nonNull(role)) && (!role.startsWith(ROLE_PREFIX))) {
return ROLE_PREFIX + role;
} else {
......@@ -90,17 +92,19 @@ public class CurrentUserHelper {
}
}
public static boolean containsRole(String role, Collection<? extends GrantedAuthority> authorities) {
return authorities.stream().anyMatch(a -> isAuthorityEquals(role, a.getAuthority()));
public static UserId getCurrentUserId() {
return UserId.from(getSubClaim());
}
private static boolean isAuthorityEquals(String role, String authority) {
String roleToCheck = prepareRoleForCheck(role);
return StringUtils.equalsIgnoreCase(role, authority) || StringUtils.equalsIgnoreCase(roleToCheck, authority);
private static String getSubClaim() {
return ((Jwt) getAuthentication().getPrincipal()).getClaim(SUB_CLAIM_KEY);
}
static UserId getCurrentUserId() {
return UserId.from(getAuthentication().getName());
public static Authentication getAuthentication() {
return findAuthentication().orElseThrow(() -> new IllegalStateException("No authenticated User found"));
}
private static Optional<Authentication> findAuthentication() {
return Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication()).filter(TRUSTED);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment