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