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);
}
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);
}
}
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();
}
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();
}
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);
}
Aggregations