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