Search in sources :

Example 1 with UserAuthorizations

use of org.finra.herd.model.api.xml.UserAuthorizations in project herd by FINRAOS.

the class CurrentUserRestControllerTest method testGetCurrentUser.

@Test
public void testGetCurrentUser() throws Exception {
    // Create a set of test namespace authorizations.
    Set<NamespaceAuthorization> namespaceAuthorizations = new LinkedHashSet<>();
    namespaceAuthorizations.add(new NamespaceAuthorization(NAMESPACE, SUPPORTED_NAMESPACE_PERMISSIONS));
    namespaceAuthorizations.add(new NamespaceAuthorization(NAMESPACE_2, SUPPORTED_NAMESPACE_PERMISSIONS));
    UserAuthorizations userAuthorizations = new UserAuthorizations();
    userAuthorizations.setNamespaceAuthorizations(new ArrayList(namespaceAuthorizations));
    when(currentUserService.getCurrentUser()).thenReturn(userAuthorizations);
    // Get the current user information.
    UserAuthorizations resultUserAuthorizations = currentUserRestController.getCurrentUser();
    // Verify the external calls.
    verify(currentUserService).getCurrentUser();
    verifyNoMoreInteractions(currentUserService);
    // Validate the returned object.
    assertEquals(userAuthorizations, resultUserAuthorizations);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) NamespaceAuthorization(org.finra.herd.model.api.xml.NamespaceAuthorization) UserAuthorizations(org.finra.herd.model.api.xml.UserAuthorizations) Test(org.junit.Test)

Example 2 with UserAuthorizations

use of org.finra.herd.model.api.xml.UserAuthorizations in project herd by FINRAOS.

the class CurrentUserServiceTest method testGetCurrentUser.

@Test
public void testGetCurrentUser() throws Exception {
    // Create a set of test namespace authorizations.
    Set<NamespaceAuthorization> namespaceAuthorizations = new LinkedHashSet<>();
    namespaceAuthorizations.add(new NamespaceAuthorization(NAMESPACE, SUPPORTED_NAMESPACE_PERMISSIONS));
    namespaceAuthorizations.add(new NamespaceAuthorization(NAMESPACE_2, SUPPORTED_NAMESPACE_PERMISSIONS));
    // Create test roles
    List<SecurityRoleEntity> securityRoleEntities = securityRoleDaoTestHelper.createTestSecurityRoles();
    // Fetch the security role codes to add to the application user.
    Set<String> roles = securityRoleEntities.stream().map(SecurityRoleEntity::getCode).collect(Collectors.toSet());
    // Override the security context to return an application user populated with test values.
    Authentication originalAuthentication = SecurityContextHolder.getContext().getAuthentication();
    try {
        SecurityContextHolder.getContext().setAuthentication(new Authentication() {

            @Override
            public String getName() {
                return null;
            }

            @Override
            public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
            }

            @Override
            public boolean isAuthenticated() {
                return false;
            }

            @Override
            public Object getPrincipal() {
                List<SimpleGrantedAuthority> authorities = Arrays.asList(new SimpleGrantedAuthority(SECURITY_FUNCTION), new SimpleGrantedAuthority(SECURITY_FUNCTION_2));
                ApplicationUser applicationUser = new ApplicationUser(this.getClass());
                applicationUser.setUserId(USER_ID);
                applicationUser.setRoles(roles);
                applicationUser.setNamespaceAuthorizations(namespaceAuthorizations);
                return new SecurityUserWrapper(USER_ID, STRING_VALUE, true, true, true, true, authorities, applicationUser);
            }

            @Override
            public Object getDetails() {
                return null;
            }

            @Override
            public Object getCredentials() {
                return null;
            }

            @Override
            public Collection<? extends GrantedAuthority> getAuthorities() {
                return null;
            }
        });
        // Get the current user information.
        UserAuthorizations userAuthorizations = currentUserService.getCurrentUser();
        // Validate the response object.
        assertEquals(new UserAuthorizations(USER_ID, new ArrayList<>(namespaceAuthorizations), new ArrayList<>(roles), Arrays.asList(SECURITY_FUNCTION, SECURITY_FUNCTION_2)), userAuthorizations);
    } finally {
        // Restore the original authentication.
        SecurityContextHolder.getContext().setAuthentication(originalAuthentication);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ApplicationUser(org.finra.herd.model.dto.ApplicationUser) SecurityUserWrapper(org.finra.herd.model.dto.SecurityUserWrapper) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) NamespaceAuthorization(org.finra.herd.model.api.xml.NamespaceAuthorization) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Authentication(org.springframework.security.core.Authentication) Collection(java.util.Collection) SecurityRoleEntity(org.finra.herd.model.jpa.SecurityRoleEntity) ArrayList(java.util.ArrayList) List(java.util.List) UserAuthorizations(org.finra.herd.model.api.xml.UserAuthorizations) Test(org.junit.Test)

Example 3 with UserAuthorizations

use of org.finra.herd.model.api.xml.UserAuthorizations in project herd by FINRAOS.

the class CurrentUserServiceTest method testGetCurrentUserNoAuthentication.

@Test
public void testGetCurrentUserNoAuthentication() throws Exception {
    // Override the security context to have no authentication.
    Authentication originalAuthentication = SecurityContextHolder.getContext().getAuthentication();
    try {
        // Get the current user information.
        UserAuthorizations userAuthorizations = currentUserService.getCurrentUser();
        // Validate the response object.
        assertEquals(new UserAuthorizations(null, null, NO_SECURITY_ROLES, NO_SECURITY_FUNCTIONS), userAuthorizations);
    } finally {
        // Restore the original authentication.
        SecurityContextHolder.getContext().setAuthentication(originalAuthentication);
    }
}
Also used : Authentication(org.springframework.security.core.Authentication) UserAuthorizations(org.finra.herd.model.api.xml.UserAuthorizations) Test(org.junit.Test)

Example 4 with UserAuthorizations

use of org.finra.herd.model.api.xml.UserAuthorizations in project herd by FINRAOS.

the class CurrentUserServiceTest method testGetCurrentUserNoSecurityRolesAndFunctions.

@Test
public void testGetCurrentUserNoSecurityRolesAndFunctions() throws Exception {
    // Create a set of test namespace authorizations.
    Set<NamespaceAuthorization> namespaceAuthorizations = new LinkedHashSet<>();
    namespaceAuthorizations.add(new NamespaceAuthorization(NAMESPACE, SUPPORTED_NAMESPACE_PERMISSIONS));
    namespaceAuthorizations.add(new NamespaceAuthorization(NAMESPACE_2, SUPPORTED_NAMESPACE_PERMISSIONS));
    // Override the security context to return an application user populated with test values.
    Authentication originalAuthentication = SecurityContextHolder.getContext().getAuthentication();
    try {
        SecurityContextHolder.getContext().setAuthentication(new Authentication() {

            @Override
            public String getName() {
                return null;
            }

            @Override
            public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
            }

            @Override
            public boolean isAuthenticated() {
                return false;
            }

            @Override
            public Object getPrincipal() {
                List<SimpleGrantedAuthority> authorities = new ArrayList<>();
                ApplicationUser applicationUser = new ApplicationUser(this.getClass());
                applicationUser.setUserId(USER_ID);
                applicationUser.setNamespaceAuthorizations(namespaceAuthorizations);
                return new SecurityUserWrapper(USER_ID, STRING_VALUE, true, true, true, true, authorities, applicationUser);
            }

            @Override
            public Object getDetails() {
                return null;
            }

            @Override
            public Object getCredentials() {
                return null;
            }

            @Override
            public Collection<? extends GrantedAuthority> getAuthorities() {
                return null;
            }
        });
        // Get the current user information.
        UserAuthorizations userAuthorizations = currentUserService.getCurrentUser();
        // Validate the response object.
        assertEquals(new UserAuthorizations(USER_ID, new ArrayList<>(namespaceAuthorizations), NO_SECURITY_ROLES, NO_SECURITY_FUNCTIONS), userAuthorizations);
    } finally {
        // Restore the original authentication.
        SecurityContextHolder.getContext().setAuthentication(originalAuthentication);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ApplicationUser(org.finra.herd.model.dto.ApplicationUser) SecurityUserWrapper(org.finra.herd.model.dto.SecurityUserWrapper) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) NamespaceAuthorization(org.finra.herd.model.api.xml.NamespaceAuthorization) Authentication(org.springframework.security.core.Authentication) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) UserAuthorizations(org.finra.herd.model.api.xml.UserAuthorizations) Test(org.junit.Test)

Example 5 with UserAuthorizations

use of org.finra.herd.model.api.xml.UserAuthorizations in project herd by FINRAOS.

the class CurrentUserServiceImpl method getCurrentUser.

@Override
public UserAuthorizations getCurrentUser() {
    // Create the user authorizations.
    UserAuthorizations userAuthorizations = new UserAuthorizations();
    // Get the application user.
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        SecurityUserWrapper securityUserWrapper = (SecurityUserWrapper) authentication.getPrincipal();
        ApplicationUser applicationUser = securityUserWrapper.getApplicationUser();
        userAuthorizations.setUserId(applicationUser.getUserId());
        // If roles are present on the application user then filter the herd-specific security roles and add that information to the Current user.
        if (CollectionUtils.isNotEmpty(applicationUser.getRoles())) {
            userAuthorizations.setSecurityRoles(new ArrayList<>(getValidSecurityRoles(applicationUser.getRoles())));
        }
        // Get all granted authorities for this user.
        Collection<GrantedAuthority> grantedAuthorities = securityUserWrapper.getAuthorities();
        // Add relative security functions as per granted authorities, if any are present.
        if (CollectionUtils.isNotEmpty(grantedAuthorities)) {
            userAuthorizations.setSecurityFunctions(grantedAuthorities.stream().map(grantedAuthority -> new String(grantedAuthority.getAuthority())).collect(Collectors.toList()));
        }
        userAuthorizations.setNamespaceAuthorizations(new ArrayList<>(applicationUser.getNamespaceAuthorizations()));
    }
    return userAuthorizations;
}
Also used : ApplicationUser(org.finra.herd.model.dto.ApplicationUser) Authentication(org.springframework.security.core.Authentication) SecurityUserWrapper(org.finra.herd.model.dto.SecurityUserWrapper) GrantedAuthority(org.springframework.security.core.GrantedAuthority) UserAuthorizations(org.finra.herd.model.api.xml.UserAuthorizations)

Aggregations

UserAuthorizations (org.finra.herd.model.api.xml.UserAuthorizations)5 Test (org.junit.Test)4 Authentication (org.springframework.security.core.Authentication)4 ArrayList (java.util.ArrayList)3 LinkedHashSet (java.util.LinkedHashSet)3 NamespaceAuthorization (org.finra.herd.model.api.xml.NamespaceAuthorization)3 ApplicationUser (org.finra.herd.model.dto.ApplicationUser)3 SecurityUserWrapper (org.finra.herd.model.dto.SecurityUserWrapper)3 GrantedAuthority (org.springframework.security.core.GrantedAuthority)3 Collection (java.util.Collection)2 List (java.util.List)2 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)2 SecurityRoleEntity (org.finra.herd.model.jpa.SecurityRoleEntity)1