use of org.finra.herd.model.jpa.UserNamespaceAuthorizationEntity in project herd by FINRAOS.
the class UserNamespaceAuthorizationDaoImpl method getUserNamespaceAuthorizationsByNamespace.
@Override
public List<UserNamespaceAuthorizationEntity> getUserNamespaceAuthorizationsByNamespace(String namespace) {
// Create the criteria builder and the criteria.
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<UserNamespaceAuthorizationEntity> criteria = builder.createQuery(UserNamespaceAuthorizationEntity.class);
// The criteria root is the user namespace authorization.
Root<UserNamespaceAuthorizationEntity> userNamespaceAuthorizationEntity = criteria.from(UserNamespaceAuthorizationEntity.class);
// Join to the other tables we can filter on.
Join<UserNamespaceAuthorizationEntity, NamespaceEntity> namespaceEntity = userNamespaceAuthorizationEntity.join(UserNamespaceAuthorizationEntity_.namespace);
// Create the standard restrictions (i.e. the standard where clauses).
Predicate queryRestriction = builder.equal(builder.upper(namespaceEntity.get(NamespaceEntity_.code)), namespace.toUpperCase());
// Order by user id.
Order orderBy = builder.asc(userNamespaceAuthorizationEntity.get(UserNamespaceAuthorizationEntity_.userId));
// Add all clauses for the query.
criteria.select(userNamespaceAuthorizationEntity).where(queryRestriction).orderBy(orderBy);
// Execute the query and return the result list.
return entityManager.createQuery(criteria).getResultList();
}
use of org.finra.herd.model.jpa.UserNamespaceAuthorizationEntity in project herd by FINRAOS.
the class UserNamespaceAuthorizationDaoTest method testGetUserNamespaceAuthorizationsByNamespace.
@Test
public void testGetUserNamespaceAuthorizationsByNamespace() {
// Create user namespace authorization keys. The keys are listed out of order to validate the order by logic.
List<UserNamespaceAuthorizationKey> keys = Arrays.asList(new UserNamespaceAuthorizationKey(USER_ID_2, NAMESPACE_2), new UserNamespaceAuthorizationKey(USER_ID_2, NAMESPACE), new UserNamespaceAuthorizationKey(USER_ID, NAMESPACE_2), new UserNamespaceAuthorizationKey(USER_ID, NAMESPACE));
// Create and persist the relative database entities.
List<UserNamespaceAuthorizationEntity> userNamespaceAuthorizationEntities = new ArrayList<>();
for (UserNamespaceAuthorizationKey key : keys) {
userNamespaceAuthorizationEntities.add(userNamespaceAuthorizationDaoTestHelper.createUserNamespaceAuthorizationEntity(key, SUPPORTED_NAMESPACE_PERMISSIONS));
}
// Get user namespace authorizations by namespace.
assertEquals(Arrays.asList(userNamespaceAuthorizationEntities.get(3), userNamespaceAuthorizationEntities.get(1)), userNamespaceAuthorizationDao.getUserNamespaceAuthorizationsByNamespace(NAMESPACE));
// Test case insensitivity of the namespace input parameter.
assertEquals(Arrays.asList(userNamespaceAuthorizationEntities.get(3), userNamespaceAuthorizationEntities.get(1)), userNamespaceAuthorizationDao.getUserNamespaceAuthorizationsByNamespace(NAMESPACE.toUpperCase()));
assertEquals(Arrays.asList(userNamespaceAuthorizationEntities.get(3), userNamespaceAuthorizationEntities.get(1)), userNamespaceAuthorizationDao.getUserNamespaceAuthorizationsByNamespace(NAMESPACE.toLowerCase()));
// Try to retrieve user namespace authorization using a non-existing namespace.
assertEquals(new ArrayList<>(), userNamespaceAuthorizationDao.getUserNamespaceAuthorizationsByNamespace(NAMESPACE_3));
}
use of org.finra.herd.model.jpa.UserNamespaceAuthorizationEntity in project herd by FINRAOS.
the class UserNamespaceAuthorizationDaoTest method testGetUserNamespaceAuthorizationsByUserIdStartsWith.
@Test
public void testGetUserNamespaceAuthorizationsByUserIdStartsWith() {
NamespaceEntity namespaceEntity = new NamespaceEntity();
namespaceEntity.setCode(NAMESPACE);
namespaceDao.saveAndRefresh(namespaceEntity);
UserNamespaceAuthorizationEntity userNamespaceAuthorizationEntity1 = new UserNamespaceAuthorizationEntity();
userNamespaceAuthorizationEntity1.setUserId("ab");
userNamespaceAuthorizationEntity1.setNamespace(namespaceEntity);
userNamespaceAuthorizationDao.saveAndRefresh(userNamespaceAuthorizationEntity1);
UserNamespaceAuthorizationEntity userNamespaceAuthorizationEntity2 = new UserNamespaceAuthorizationEntity();
userNamespaceAuthorizationEntity2.setUserId("ac");
userNamespaceAuthorizationEntity2.setNamespace(namespaceEntity);
userNamespaceAuthorizationDao.saveAndRefresh(userNamespaceAuthorizationEntity2);
UserNamespaceAuthorizationEntity userNamespaceAuthorizationEntity3 = new UserNamespaceAuthorizationEntity();
userNamespaceAuthorizationEntity3.setUserId("bc");
userNamespaceAuthorizationEntity3.setNamespace(namespaceEntity);
userNamespaceAuthorizationDao.saveAndRefresh(userNamespaceAuthorizationEntity3);
{
List<UserNamespaceAuthorizationEntity> result = userNamespaceAuthorizationDao.getUserNamespaceAuthorizationsByUserIdStartsWith("a");
assertEquals(2, result.size());
assertEquals("ab", result.get(0).getUserId());
assertEquals("ac", result.get(1).getUserId());
}
{
List<UserNamespaceAuthorizationEntity> result = userNamespaceAuthorizationDao.getUserNamespaceAuthorizationsByUserIdStartsWith("b");
assertEquals(1, result.size());
assertEquals("bc", result.get(0).getUserId());
}
{
List<UserNamespaceAuthorizationEntity> result = userNamespaceAuthorizationDao.getUserNamespaceAuthorizationsByUserIdStartsWith("c");
assertEquals(0, result.size());
}
}
use of org.finra.herd.model.jpa.UserNamespaceAuthorizationEntity in project herd by FINRAOS.
the class UserNamespaceAuthorizationDaoTest method testGetUserNamespaceAuthorizationByKey.
@Test
public void testGetUserNamespaceAuthorizationByKey() {
// Create and persist the relative database entities.
NamespaceEntity namespaceEntity = namespaceDaoTestHelper.createNamespaceEntity(NAMESPACE);
UserNamespaceAuthorizationEntity userNamespaceAuthorizationEntity = userNamespaceAuthorizationDaoTestHelper.createUserNamespaceAuthorizationEntity(USER_ID, namespaceEntity, Arrays.asList(NamespacePermissionEnum.READ, NamespacePermissionEnum.WRITE));
// Get a user namespace authorization.
assertEquals(userNamespaceAuthorizationEntity, userNamespaceAuthorizationDao.getUserNamespaceAuthorizationByKey(new UserNamespaceAuthorizationKey(USER_ID, NAMESPACE)));
// Test case insensitivity of user namespace authorization key.
assertEquals(userNamespaceAuthorizationEntity, userNamespaceAuthorizationDao.getUserNamespaceAuthorizationByKey(new UserNamespaceAuthorizationKey(USER_ID.toUpperCase(), NAMESPACE.toUpperCase())));
assertEquals(userNamespaceAuthorizationEntity, userNamespaceAuthorizationDao.getUserNamespaceAuthorizationByKey(new UserNamespaceAuthorizationKey(USER_ID.toLowerCase(), NAMESPACE.toLowerCase())));
// Try to retrieve user namespace authorization using invalid input parameters.
assertNull(userNamespaceAuthorizationDao.getUserNamespaceAuthorizationByKey(new UserNamespaceAuthorizationKey("I_DO_NOT_EXIST", NAMESPACE)));
assertNull(userNamespaceAuthorizationDao.getUserNamespaceAuthorizationByKey(new UserNamespaceAuthorizationKey(USER_ID, "I_DO_NOT_EXIST")));
}
use of org.finra.herd.model.jpa.UserNamespaceAuthorizationEntity in project herd by FINRAOS.
the class UserNamespaceAuthorizationHelper method buildNamespaceAuthorizations.
/**
* Builds a set of namespace authorizations per specified user and adds them to the application user.
*
* @param applicationUser the application user
*/
public void buildNamespaceAuthorizations(ApplicationUser applicationUser) {
// Get the user id from the application user.
String userId = applicationUser.getUserId();
// Check if user namespace authorization is not enabled or this user is a namespace authorization administrator.
if (BooleanUtils.isNotTrue(configurationHelper.getBooleanProperty(ConfigurationValue.USER_NAMESPACE_AUTHORIZATION_ENABLED)) || isNamespaceAuthorizationAdmin(userId)) {
// Assign all permissions for all namespaces configured in the system.
applicationUser.setNamespaceAuthorizations(getAllNamespaceAuthorizations());
} else {
// Assign a set of namespace authorizations per specified user.
Set<NamespaceAuthorization> namespaceAuthorizations = new HashSet<>();
applicationUser.setNamespaceAuthorizations(namespaceAuthorizations);
for (UserNamespaceAuthorizationEntity userNamespaceAuthorizationEntity : userNamespaceAuthorizationDao.getUserNamespaceAuthorizationsByUserId(userId)) {
namespaceAuthorizations.add(toNamespaceAuthorization(userNamespaceAuthorizationEntity));
}
// Search authorizations by wildcard token
for (UserNamespaceAuthorizationEntity wildcardEntity : userNamespaceAuthorizationDao.getUserNamespaceAuthorizationsByUserIdStartsWith(WildcardHelper.WILDCARD_TOKEN)) {
if (wildcardHelper.matches(userId.toUpperCase(), wildcardEntity.getUserId().toUpperCase())) {
namespaceAuthorizations.add(toNamespaceAuthorization(wildcardEntity));
}
}
}
}
Aggregations