Search in sources :

Example 1 with DynRoleMembership

use of org.apache.syncope.core.persistence.api.entity.user.DynRoleMembership in project syncope by apache.

the class RoleDataBinderImpl method setDynMembership.

private void setDynMembership(final Role role, final String dynMembershipFIQL) {
    SearchCond dynMembershipCond = SearchCondConverter.convert(dynMembershipFIQL);
    if (!dynMembershipCond.isValid()) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidSearchExpression);
        sce.getElements().add(dynMembershipFIQL);
        throw sce;
    }
    DynRoleMembership dynMembership;
    if (role.getDynMembership() == null) {
        dynMembership = entityFactory.newEntity(DynRoleMembership.class);
        dynMembership.setRole(role);
        role.setDynMembership(dynMembership);
    } else {
        dynMembership = role.getDynMembership();
    }
    dynMembership.setFIQLCond(dynMembershipFIQL);
}
Also used : SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) DynRoleMembership(org.apache.syncope.core.persistence.api.entity.user.DynRoleMembership)

Example 2 with DynRoleMembership

use of org.apache.syncope.core.persistence.api.entity.user.DynRoleMembership in project syncope by apache.

the class RoleTest method dynMembership.

@Test
public void dynMembership() {
    // 0. create user matching the condition below
    User user = entityFactory.newEntity(User.class);
    user.setUsername("username");
    user.setRealm(realmDAO.findByFullPath("/even/two"));
    user.add(anyTypeClassDAO.find("other"));
    UPlainAttr attr = entityFactory.newEntity(UPlainAttr.class);
    attr.setOwner(user);
    attr.setSchema(plainSchemaDAO.find("cool"));
    attr.add("true", anyUtilsFactory.getInstance(AnyTypeKind.USER));
    user.add(attr);
    user = userDAO.save(user);
    String newUserKey = user.getKey();
    assertNotNull(newUserKey);
    // 1. create role with dynamic membership
    Role role = entityFactory.newEntity(Role.class);
    role.setKey("new");
    role.add(realmDAO.getRoot());
    role.add(realmDAO.findByFullPath("/even/two"));
    role.getEntitlements().add(StandardEntitlement.LOG_LIST);
    role.getEntitlements().add(StandardEntitlement.LOG_SET_LEVEL);
    DynRoleMembership dynMembership = entityFactory.newEntity(DynRoleMembership.class);
    dynMembership.setFIQLCond("cool==true");
    dynMembership.setRole(role);
    role.setDynMembership(dynMembership);
    Role actual = roleDAO.save(role);
    assertNotNull(actual);
    roleDAO.flush();
    // 2. verify that dynamic membership is there
    actual = roleDAO.find(actual.getKey());
    assertNotNull(actual);
    assertNotNull(actual.getDynMembership());
    assertNotNull(actual.getDynMembership().getKey());
    assertEquals(actual, actual.getDynMembership().getRole());
    // 3. verify that expected users have the created role dynamically assigned
    List<String> members = roleDAO.findDynMembers(actual);
    assertEquals(2, members.size());
    assertEquals(new HashSet<>(Arrays.asList("c9b2dec2-00a7-4855-97c0-d854842b4b24", newUserKey)), new HashSet<>(members));
    user = userDAO.find("c9b2dec2-00a7-4855-97c0-d854842b4b24");
    assertNotNull(user);
    Collection<Role> dynRoleMemberships = findDynRoles(user);
    assertEquals(1, dynRoleMemberships.size());
    assertTrue(dynRoleMemberships.contains(actual.getDynMembership().getRole()));
    // 4. delete the new user and verify that dynamic membership was updated
    userDAO.delete(newUserKey);
    userDAO.flush();
    actual = roleDAO.find(actual.getKey());
    members = roleDAO.findDynMembers(actual);
    assertEquals(1, members.size());
    assertEquals("c9b2dec2-00a7-4855-97c0-d854842b4b24", members.get(0));
    // 5. delete role and verify that dynamic membership was also removed
    String dynMembershipKey = actual.getDynMembership().getKey();
    roleDAO.delete(actual);
    roleDAO.flush();
    assertNull(entityManager().find(JPADynRoleMembership.class, dynMembershipKey));
    dynRoleMemberships = findDynRoles(user);
    assertTrue(dynRoleMemberships.isEmpty());
}
Also used : Role(org.apache.syncope.core.persistence.api.entity.Role) User(org.apache.syncope.core.persistence.api.entity.user.User) JPADynRoleMembership(org.apache.syncope.core.persistence.jpa.entity.user.JPADynRoleMembership) DynRoleMembership(org.apache.syncope.core.persistence.api.entity.user.DynRoleMembership) JPADynRoleMembership(org.apache.syncope.core.persistence.jpa.entity.user.JPADynRoleMembership) UPlainAttr(org.apache.syncope.core.persistence.api.entity.user.UPlainAttr) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Example 3 with DynRoleMembership

use of org.apache.syncope.core.persistence.api.entity.user.DynRoleMembership in project syncope by apache.

the class AnySearchTest method searchByDynMembership.

@Test
public void searchByDynMembership() {
    // 1. create role with dynamic membership
    Role role = entityFactory.newEntity(Role.class);
    role.setKey("new");
    role.add(realmDAO.getRoot());
    role.add(realmDAO.findByFullPath("/even/two"));
    role.getEntitlements().add(StandardEntitlement.LOG_LIST);
    role.getEntitlements().add(StandardEntitlement.LOG_SET_LEVEL);
    DynRoleMembership dynMembership = entityFactory.newEntity(DynRoleMembership.class);
    dynMembership.setFIQLCond("cool==true");
    dynMembership.setRole(role);
    role.setDynMembership(dynMembership);
    role = roleDAO.save(role);
    assertNotNull(role);
    roleDAO.flush();
    // 2. search user by this dynamic role
    RoleCond roleCond = new RoleCond();
    roleCond.setRole(role.getKey());
    List<User> users = searchDAO.search(SearchCond.getLeafCond(roleCond), AnyTypeKind.USER);
    assertNotNull(users);
    assertEquals(1, users.size());
    assertEquals("c9b2dec2-00a7-4855-97c0-d854842b4b24", users.get(0).getKey());
}
Also used : Role(org.apache.syncope.core.persistence.api.entity.Role) User(org.apache.syncope.core.persistence.api.entity.user.User) RoleCond(org.apache.syncope.core.persistence.api.dao.search.RoleCond) DynRoleMembership(org.apache.syncope.core.persistence.api.entity.user.DynRoleMembership) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Aggregations

DynRoleMembership (org.apache.syncope.core.persistence.api.entity.user.DynRoleMembership)3 Role (org.apache.syncope.core.persistence.api.entity.Role)2 User (org.apache.syncope.core.persistence.api.entity.user.User)2 AbstractTest (org.apache.syncope.core.persistence.jpa.AbstractTest)2 Test (org.junit.jupiter.api.Test)2 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)1 RoleCond (org.apache.syncope.core.persistence.api.dao.search.RoleCond)1 SearchCond (org.apache.syncope.core.persistence.api.dao.search.SearchCond)1 UPlainAttr (org.apache.syncope.core.persistence.api.entity.user.UPlainAttr)1 JPADynRoleMembership (org.apache.syncope.core.persistence.jpa.entity.user.JPADynRoleMembership)1