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

OZG-5176 Refactored CurrentUserHelper

parent 3fccb1f9
No related branches found
No related tags found
No related merge requests found
......@@ -23,8 +23,11 @@
package de.ozgcloud.admin.common.user;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.authentication.AuthenticationTrustResolver;
import org.springframework.security.authentication.AuthenticationTrustResolverImpl;
......@@ -32,10 +35,8 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class CurrentUserHelper {
......@@ -44,8 +45,7 @@ public class CurrentUserHelper {
private static final AuthenticationTrustResolver TRUST_RESOLVER = new AuthenticationTrustResolverImpl();
private static final Predicate<Authentication> IS_TRUSTED = auth -> !TRUST_RESOLVER.isAnonymous(auth);
static boolean hasRole(String role) {
public static boolean hasRole(String role) {
var auth = getAuthentication();
if ((Objects.isNull(auth)) || (Objects.isNull(auth.getPrincipal()))) {
......@@ -59,18 +59,10 @@ public class CurrentUserHelper {
if (Objects.isNull(authorities)) {
return false;
}
return containsRole(prepareRoleForCheck(role), authorities);
}
private 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);
return authorities.stream().anyMatch(a -> StringUtils.equalsIgnoreCase(addRolePrefixIfMissing(role), a.getAuthority()));
}
static String prepareRoleForCheck(String roleToCheck) {
static String addRolePrefixIfMissing(String roleToCheck) {
return Optional.ofNullable(roleToCheck)
.filter(IS_ROLE_PREFIX_MISSING)
.map(role -> String.format("%s%s", ROLE_PREFIX, role))
......
......@@ -23,6 +23,11 @@
package de.ozgcloud.admin.common.user;
import static org.assertj.core.api.Assertions.*;
import java.util.Collection;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
......@@ -36,11 +41,6 @@ import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import java.util.Collection;
import java.util.List;
import static org.assertj.core.api.Assertions.*;
class CurrentUserHelperTest {
@DisplayName("Has role")
@Nested
......@@ -54,8 +54,7 @@ class CurrentUserHelperTest {
void shouldNotHaveRoleIfNull() {
try (MockedStatic<CurrentUserHelper> mockUserHelper = Mockito.mockStatic(
CurrentUserHelper.class,
Mockito.CALLS_REAL_METHODS)
) {
Mockito.CALLS_REAL_METHODS)) {
mockUserHelper.when(CurrentUserHelper::getAuthentication).thenReturn(null);
boolean hasRole = CurrentUserHelper.hasRole(UserRole.ADMIN_USER);
......@@ -69,8 +68,7 @@ class CurrentUserHelperTest {
Mockito.when(mockAuthentication.getPrincipal()).thenReturn(null);
try (MockedStatic<CurrentUserHelper> mockUserHelper = Mockito.mockStatic(
CurrentUserHelper.class,
Mockito.CALLS_REAL_METHODS)
) {
Mockito.CALLS_REAL_METHODS)) {
mockUserHelper.when(CurrentUserHelper::getAuthentication).thenReturn(mockAuthentication);
boolean hasRole = CurrentUserHelper.hasRole(UserRole.ADMIN_USER);
......@@ -87,8 +85,7 @@ class CurrentUserHelperTest {
try (MockedStatic<CurrentUserHelper> mockUserHelper = Mockito.mockStatic(
CurrentUserHelper.class,
Mockito.CALLS_REAL_METHODS)
){
Mockito.CALLS_REAL_METHODS)) {
mockUserHelper.when(CurrentUserHelper::getAuthentication).thenReturn(mockAuthentication);
mockUserHelper.when(() -> CurrentUserHelper.containsRole(Mockito.anyList(), Mockito.anyString()))
.thenReturn(false);
......@@ -107,8 +104,7 @@ class CurrentUserHelperTest {
try (MockedStatic<CurrentUserHelper> mockUserHelper = Mockito.mockStatic(
CurrentUserHelper.class,
Mockito.CALLS_REAL_METHODS)
){
Mockito.CALLS_REAL_METHODS)) {
mockUserHelper.when(CurrentUserHelper::getAuthentication).thenReturn(mockAuthentication);
mockUserHelper.when(() -> CurrentUserHelper.containsRole(Mockito.anyList(), Mockito.anyString()))
.thenReturn(true);
......@@ -133,8 +129,7 @@ class CurrentUserHelperTest {
@Test
void shouldNotContainRole() {
List<GrantedAuthority> authorities = List.of(
new SimpleGrantedAuthority(CurrentUserHelper.ROLE_PREFIX + "OTHER")
);
new SimpleGrantedAuthority(CurrentUserHelper.ROLE_PREFIX + "OTHER"));
boolean containsRole = CurrentUserHelper.containsRole(authorities, UserRole.ADMIN_USER);
......@@ -144,8 +139,7 @@ class CurrentUserHelperTest {
@Test
void shouldContainRole() {
Collection<? extends GrantedAuthority> authorities = List.of(
new SimpleGrantedAuthority(CurrentUserHelper.ROLE_PREFIX + UserRole.ADMIN_USER)
);
new SimpleGrantedAuthority(CurrentUserHelper.ROLE_PREFIX + UserRole.ADMIN_USER));
boolean containsRole = CurrentUserHelper.containsRole(authorities, UserRole.ADMIN_USER);
......@@ -161,7 +155,7 @@ class CurrentUserHelperTest {
void shouldAddPrefixIfMissing() {
var roleWithoutPrefix = UserRole.ADMIN_USER;
var role = CurrentUserHelper.prepareRoleForCheck(roleWithoutPrefix);
var role = CurrentUserHelper.addRolePrefixIfMissing(roleWithoutPrefix);
assertThat(role).isEqualTo(String.format("%s%s", CurrentUserHelper.ROLE_PREFIX, UserRole.ADMIN_USER));
}
......@@ -170,14 +164,14 @@ class CurrentUserHelperTest {
void shouldReturnRoleIfPrefixAlreadyExists() {
var roleWithPrefix = String.format("ROLE_%s", UserRole.ADMIN_USER);
var role = CurrentUserHelper.prepareRoleForCheck(roleWithPrefix);
var role = CurrentUserHelper.addRolePrefixIfMissing(roleWithPrefix);
assertThat(role).isEqualTo(roleWithPrefix);
}
@Test
void shouldReturnNullIfPassingNull() {
var role = CurrentUserHelper.prepareRoleForCheck(null);
var role = CurrentUserHelper.addRolePrefixIfMissing(null);
assertThat(role).isNull();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment