Search in sources :

Example 1 with SecurityRoleEntity

use of org.finra.herd.model.jpa.SecurityRoleEntity in project herd by FINRAOS.

the class SecurityFunctionDaoTest method createSecurityRoleEntity.

/**
 * Creates and persists a security role entity.
 *
 * @param code the name of the security role
 *
 * @return the security role entity
 */
private SecurityRoleEntity createSecurityRoleEntity(String code) {
    SecurityRoleEntity securityRoleEntity = new SecurityRoleEntity();
    securityRoleEntity.setCode(code);
    return herdDao.saveAndRefresh(securityRoleEntity);
}
Also used : SecurityRoleEntity(org.finra.herd.model.jpa.SecurityRoleEntity)

Example 2 with SecurityRoleEntity

use of org.finra.herd.model.jpa.SecurityRoleEntity 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 SecurityRoleEntity

use of org.finra.herd.model.jpa.SecurityRoleEntity in project herd by FINRAOS.

the class SecurityRoleDaoImpl method getAllSecurityRoles.

@Override
@Cacheable(DaoSpringModuleConfig.HERD_CACHE_NAME)
public List<SecurityRoleEntity> getAllSecurityRoles() {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<SecurityRoleEntity> criteria = builder.createQuery(SecurityRoleEntity.class);
    // The criteria root is the security role
    Root<SecurityRoleEntity> securityRoleEntity = criteria.from(SecurityRoleEntity.class);
    // Create select query
    criteria.select(securityRoleEntity);
    // Get the role code column.
    Path<String> roleCodeColumn = securityRoleEntity.get(SecurityRoleEntity_.code);
    // Set the order by clause
    criteria.orderBy(builder.asc(roleCodeColumn));
    // run the query to get the list of security role entities and return them
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) SecurityRoleEntity(org.finra.herd.model.jpa.SecurityRoleEntity) Cacheable(org.springframework.cache.annotation.Cacheable)

Example 4 with SecurityRoleEntity

use of org.finra.herd.model.jpa.SecurityRoleEntity in project herd by FINRAOS.

the class SecurityFunctionDaoImpl method getSecurityFunctionsForRole.

@Override
@Cacheable(DaoSpringModuleConfig.HERD_CACHE_NAME)
public List<String> getSecurityFunctionsForRole(String roleCd) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<String> criteria = builder.createQuery(String.class);
    // The criteria root is the security role function mapping.
    Root<SecurityRoleFunctionEntity> securityRoleFunctionEntity = criteria.from(SecurityRoleFunctionEntity.class);
    // Join to the other tables we can filter on.
    Join<SecurityRoleFunctionEntity, SecurityRoleEntity> securityRoleEntity = securityRoleFunctionEntity.join(SecurityRoleFunctionEntity_.securityRole);
    Join<SecurityRoleFunctionEntity, SecurityFunctionEntity> securityFunctionEntity = securityRoleFunctionEntity.join(SecurityRoleFunctionEntity_.securityFunction);
    // Get the columns.
    Path<String> functionCodeColumn = securityFunctionEntity.get(SecurityFunctionEntity_.code);
    // Add the select clause.
    criteria.select(functionCodeColumn);
    // Add the where clause.
    criteria.where(builder.equal(builder.upper(securityRoleEntity.get(SecurityRoleEntity_.code)), roleCd.toUpperCase()));
    // Add the order by clause.
    criteria.orderBy(builder.asc(functionCodeColumn));
    // Run the query to get a list of functions.
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) SecurityRoleFunctionEntity(org.finra.herd.model.jpa.SecurityRoleFunctionEntity) SecurityFunctionEntity(org.finra.herd.model.jpa.SecurityFunctionEntity) SecurityRoleEntity(org.finra.herd.model.jpa.SecurityRoleEntity) Cacheable(org.springframework.cache.annotation.Cacheable)

Example 5 with SecurityRoleEntity

use of org.finra.herd.model.jpa.SecurityRoleEntity in project herd by FINRAOS.

the class SecurityRoleDaoTestHelper method createSecurityRoleEntity.

/**
 * Creates and persists a security role entity.
 *
 * @param code the name of the security role
 *
 * @return the security role entity
 */
public SecurityRoleEntity createSecurityRoleEntity(String code) {
    SecurityRoleEntity securityRoleEntity = new SecurityRoleEntity();
    securityRoleEntity.setCode(code);
    return securityRoleDao.saveAndRefresh(securityRoleEntity);
}
Also used : SecurityRoleEntity(org.finra.herd.model.jpa.SecurityRoleEntity)

Aggregations

SecurityRoleEntity (org.finra.herd.model.jpa.SecurityRoleEntity)9 SecurityFunctionEntity (org.finra.herd.model.jpa.SecurityFunctionEntity)4 SecurityRoleFunctionEntity (org.finra.herd.model.jpa.SecurityRoleFunctionEntity)4 Test (org.junit.Test)3 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 Cacheable (org.springframework.cache.annotation.Cacheable)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 NamespaceAuthorization (org.finra.herd.model.api.xml.NamespaceAuthorization)1 UserAuthorizations (org.finra.herd.model.api.xml.UserAuthorizations)1 ApplicationUser (org.finra.herd.model.dto.ApplicationUser)1 SecurityUserWrapper (org.finra.herd.model.dto.SecurityUserWrapper)1 Authentication (org.springframework.security.core.Authentication)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1