Search in sources :

Example 1 with UDynGroupMembership

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

the class GroupDataBinderImpl method setDynMembership.

private void setDynMembership(final Group group, final AnyType anyType, final String dynMembershipFIQL) {
    SearchCond dynMembershipCond = SearchCondConverter.convert(dynMembershipFIQL);
    if (!dynMembershipCond.isValid()) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidSearchExpression);
        sce.getElements().add(dynMembershipFIQL);
        throw sce;
    }
    if (anyType.getKind() == AnyTypeKind.GROUP) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidAnyType);
        sce.getElements().add(anyType.getKind().name());
        throw sce;
    }
    DynGroupMembership<?> dynMembership;
    if (anyType.getKind() == AnyTypeKind.ANY_OBJECT && !group.getADynMembership(anyType).isPresent()) {
        dynMembership = entityFactory.newEntity(ADynGroupMembership.class);
        dynMembership.setGroup(group);
        ((ADynGroupMembership) dynMembership).setAnyType(anyType);
        group.add((ADynGroupMembership) dynMembership);
    } else if (anyType.getKind() == AnyTypeKind.USER && group.getUDynMembership() == null) {
        dynMembership = entityFactory.newEntity(UDynGroupMembership.class);
        dynMembership.setGroup(group);
        group.setUDynMembership((UDynGroupMembership) dynMembership);
    } else {
        dynMembership = anyType.getKind() == AnyTypeKind.ANY_OBJECT ? group.getADynMembership(anyType).get() : group.getUDynMembership();
    }
    dynMembership.setFIQLCond(dynMembershipFIQL);
}
Also used : UDynGroupMembership(org.apache.syncope.core.persistence.api.entity.user.UDynGroupMembership) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) ADynGroupMembership(org.apache.syncope.core.persistence.api.entity.anyobject.ADynGroupMembership)

Example 2 with UDynGroupMembership

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

the class GroupTest method udynMembership.

@Test
public void udynMembership() {
    // 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 group with dynamic membership
    Group group = entityFactory.newEntity(Group.class);
    group.setRealm(realmDAO.getRoot());
    group.setName("new");
    UDynGroupMembership dynMembership = entityFactory.newEntity(UDynGroupMembership.class);
    dynMembership.setFIQLCond("cool==true");
    dynMembership.setGroup(group);
    group.setUDynMembership(dynMembership);
    Group actual = groupDAO.save(group);
    assertNotNull(actual);
    groupDAO.flush();
    // 2. verify that dynamic membership is there
    actual = groupDAO.find(actual.getKey());
    assertNotNull(actual);
    assertNotNull(actual.getUDynMembership());
    assertNotNull(actual.getUDynMembership().getKey());
    assertEquals(actual, actual.getUDynMembership().getGroup());
    // 3. verify that expected users have the created group dynamically assigned
    List<String> members = groupDAO.findUDynMembers(actual);
    assertEquals(2, members.size());
    assertEquals(new HashSet<>(Arrays.asList("c9b2dec2-00a7-4855-97c0-d854842b4b24", newUserKey)), new HashSet<>(members));
    user = userDAO.findByUsername("bellini");
    assertNotNull(user);
    Collection<Group> dynGroupMemberships = findDynGroups(user);
    assertEquals(1, dynGroupMemberships.size());
    assertTrue(dynGroupMemberships.contains(actual.getUDynMembership().getGroup()));
    // 4. delete the new user and verify that dynamic membership was updated
    userDAO.delete(newUserKey);
    userDAO.flush();
    actual = groupDAO.find(actual.getKey());
    members = groupDAO.findUDynMembers(actual);
    assertEquals(1, members.size());
    assertEquals("c9b2dec2-00a7-4855-97c0-d854842b4b24", members.get(0));
    // 5. delete group and verify that dynamic membership was also removed
    String dynMembershipKey = actual.getUDynMembership().getKey();
    groupDAO.delete(actual);
    groupDAO.flush();
    assertNull(entityManager().find(JPAUDynGroupMembership.class, dynMembershipKey));
    dynGroupMemberships = findDynGroups(user);
    assertTrue(dynGroupMemberships.isEmpty());
}
Also used : Group(org.apache.syncope.core.persistence.api.entity.group.Group) User(org.apache.syncope.core.persistence.api.entity.user.User) JPAUDynGroupMembership(org.apache.syncope.core.persistence.jpa.entity.user.JPAUDynGroupMembership) UDynGroupMembership(org.apache.syncope.core.persistence.api.entity.user.UDynGroupMembership) JPAUDynGroupMembership(org.apache.syncope.core.persistence.jpa.entity.user.JPAUDynGroupMembership) UPlainAttr(org.apache.syncope.core.persistence.api.entity.user.UPlainAttr) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Aggregations

UDynGroupMembership (org.apache.syncope.core.persistence.api.entity.user.UDynGroupMembership)2 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)1 SearchCond (org.apache.syncope.core.persistence.api.dao.search.SearchCond)1 ADynGroupMembership (org.apache.syncope.core.persistence.api.entity.anyobject.ADynGroupMembership)1 Group (org.apache.syncope.core.persistence.api.entity.group.Group)1 UPlainAttr (org.apache.syncope.core.persistence.api.entity.user.UPlainAttr)1 User (org.apache.syncope.core.persistence.api.entity.user.User)1 AbstractTest (org.apache.syncope.core.persistence.jpa.AbstractTest)1 JPAUDynGroupMembership (org.apache.syncope.core.persistence.jpa.entity.user.JPAUDynGroupMembership)1 Test (org.junit.jupiter.api.Test)1