Search in sources :

Example 16 with UserNamespaceAuthorizationEntity

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) NamespaceEntity(org.finra.herd.model.jpa.NamespaceEntity) UserNamespaceAuthorizationEntity(org.finra.herd.model.jpa.UserNamespaceAuthorizationEntity) Predicate(javax.persistence.criteria.Predicate)

Example 17 with UserNamespaceAuthorizationEntity

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));
}
Also used : UserNamespaceAuthorizationKey(org.finra.herd.model.api.xml.UserNamespaceAuthorizationKey) ArrayList(java.util.ArrayList) UserNamespaceAuthorizationEntity(org.finra.herd.model.jpa.UserNamespaceAuthorizationEntity) Test(org.junit.Test)

Example 18 with UserNamespaceAuthorizationEntity

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());
    }
}
Also used : NamespaceEntity(org.finra.herd.model.jpa.NamespaceEntity) UserNamespaceAuthorizationEntity(org.finra.herd.model.jpa.UserNamespaceAuthorizationEntity) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 19 with UserNamespaceAuthorizationEntity

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")));
}
Also used : UserNamespaceAuthorizationKey(org.finra.herd.model.api.xml.UserNamespaceAuthorizationKey) NamespaceEntity(org.finra.herd.model.jpa.NamespaceEntity) UserNamespaceAuthorizationEntity(org.finra.herd.model.jpa.UserNamespaceAuthorizationEntity) Test(org.junit.Test)

Example 20 with UserNamespaceAuthorizationEntity

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));
            }
        }
    }
}
Also used : NamespaceAuthorization(org.finra.herd.model.api.xml.NamespaceAuthorization) UserNamespaceAuthorizationEntity(org.finra.herd.model.jpa.UserNamespaceAuthorizationEntity) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

UserNamespaceAuthorizationEntity (org.finra.herd.model.jpa.UserNamespaceAuthorizationEntity)38 Test (org.junit.Test)25 UserNamespaceAuthorizationKey (org.finra.herd.model.api.xml.UserNamespaceAuthorizationKey)22 UserNamespaceAuthorization (org.finra.herd.model.api.xml.UserNamespaceAuthorization)18 NamespaceEntity (org.finra.herd.model.jpa.NamespaceEntity)12 UserNamespaceAuthorizations (org.finra.herd.model.api.xml.UserNamespaceAuthorizations)8 ArrayList (java.util.ArrayList)7 NamespacePermission (org.finra.herd.model.annotation.NamespacePermission)5 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 Predicate (javax.persistence.criteria.Predicate)4 UserNamespaceAuthorizationUpdateRequest (org.finra.herd.model.api.xml.UserNamespaceAuthorizationUpdateRequest)4 NamespaceAuthorization (org.finra.herd.model.api.xml.NamespaceAuthorization)3 ApplicationUser (org.finra.herd.model.dto.ApplicationUser)3 Order (javax.persistence.criteria.Order)2 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 AlreadyExistsException (org.finra.herd.model.AlreadyExistsException)1