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

OZG-5176 Refactored CurrentUserHelper

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