Search in sources :

Example 1 with MembershipCond

use of org.apache.syncope.core.persistence.api.dao.search.MembershipCond in project syncope by apache.

the class AnySearchTest method issueSYNCOPE980.

@Test
public void issueSYNCOPE980() {
    AnyType service = entityFactory.newEntity(AnyType.class);
    service.setKey("SERVICE");
    service.setKind(AnyTypeKind.ANY_OBJECT);
    service = anyTypeDAO.save(service);
    Group citizen = groupDAO.findByName("citizen");
    assertNotNull(citizen);
    AnyObject anyObject = entityFactory.newEntity(AnyObject.class);
    anyObject.setName("one");
    anyObject.setType(service);
    anyObject.setRealm(realmDAO.findByFullPath(SyncopeConstants.ROOT_REALM));
    AMembership membership = entityFactory.newEntity(AMembership.class);
    membership.setRightEnd(citizen);
    membership.setLeftEnd(anyObject);
    anyObject.add(membership);
    anyObjectDAO.save(anyObject);
    anyObject = anyObjectDAO.find("fc6dbc3a-6c07-4965-8781-921e7401a4a5");
    membership = entityFactory.newEntity(AMembership.class);
    membership.setRightEnd(citizen);
    membership.setLeftEnd(anyObject);
    anyObject.add(membership);
    anyObjectDAO.save(anyObject);
    anyObjectDAO.flush();
    MembershipCond groupCond = new MembershipCond();
    groupCond.setGroup("citizen");
    SearchCond searchCondition = SearchCond.getLeafCond(groupCond);
    List<AnyObject> matching = searchDAO.search(searchCondition, AnyTypeKind.ANY_OBJECT);
    assertEquals(2, matching.size());
    AnyTypeCond anyTypeCond = new AnyTypeCond();
    anyTypeCond.setAnyTypeKey(service.getKey());
    searchCondition = SearchCond.getAndCond(SearchCond.getLeafCond(groupCond), SearchCond.getLeafCond(anyTypeCond));
    matching = searchDAO.search(searchCondition, AnyTypeKind.ANY_OBJECT);
    assertEquals(1, matching.size());
}
Also used : Group(org.apache.syncope.core.persistence.api.entity.group.Group) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) AnyTypeCond(org.apache.syncope.core.persistence.api.dao.search.AnyTypeCond) AMembership(org.apache.syncope.core.persistence.api.entity.anyobject.AMembership) MembershipCond(org.apache.syncope.core.persistence.api.dao.search.MembershipCond) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Example 2 with MembershipCond

use of org.apache.syncope.core.persistence.api.dao.search.MembershipCond in project syncope by apache.

the class AnySearchTest method searchByGroup.

@Test
public void searchByGroup() {
    MembershipCond groupCond = new MembershipCond();
    groupCond.setGroup("root");
    List<User> users = searchDAO.search(SearchCond.getLeafCond(groupCond), AnyTypeKind.USER);
    assertNotNull(users);
    assertEquals(2, users.size());
    groupCond = new MembershipCond();
    groupCond.setGroup("secretary");
    users = searchDAO.search(SearchCond.getNotLeafCond(groupCond), AnyTypeKind.USER);
    assertNotNull(users);
    assertEquals(5, users.size());
}
Also used : User(org.apache.syncope.core.persistence.api.entity.user.User) MembershipCond(org.apache.syncope.core.persistence.api.dao.search.MembershipCond) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Example 3 with MembershipCond

use of org.apache.syncope.core.persistence.api.dao.search.MembershipCond in project syncope by apache.

the class AnySearchTest method searchCaseInsensitiveWithLikeCondition.

@Test
public void searchCaseInsensitiveWithLikeCondition() {
    AttributeCond fullnameLeafCond = new AttributeCond(AttributeCond.Type.ILIKE);
    fullnameLeafCond.setSchema("fullname");
    fullnameLeafCond.setExpression("%O%");
    MembershipCond groupCond = new MembershipCond();
    groupCond.setGroup("root");
    AttributeCond loginDateCond = new AttributeCond(AttributeCond.Type.EQ);
    loginDateCond.setSchema("loginDate");
    loginDateCond.setExpression("2009-05-26");
    SearchCond subCond = SearchCond.getAndCond(SearchCond.getLeafCond(fullnameLeafCond), SearchCond.getLeafCond(groupCond));
    assertTrue(subCond.isValid());
    SearchCond cond = SearchCond.getAndCond(subCond, SearchCond.getLeafCond(loginDateCond));
    assertTrue(cond.isValid());
    List<User> users = searchDAO.search(cond, AnyTypeKind.USER);
    assertNotNull(users);
    assertEquals(1, users.size());
}
Also used : User(org.apache.syncope.core.persistence.api.entity.user.User) AttributeCond(org.apache.syncope.core.persistence.api.dao.search.AttributeCond) MembershipCond(org.apache.syncope.core.persistence.api.dao.search.MembershipCond) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Example 4 with MembershipCond

use of org.apache.syncope.core.persistence.api.dao.search.MembershipCond in project syncope by apache.

the class AnySearchTest method searchWithLikeCondition.

@Test
public void searchWithLikeCondition() {
    AttributeCond fullnameLeafCond = new AttributeCond(AttributeCond.Type.LIKE);
    fullnameLeafCond.setSchema("fullname");
    fullnameLeafCond.setExpression("%o%");
    MembershipCond groupCond = new MembershipCond();
    groupCond.setGroup("root");
    AttributeCond loginDateCond = new AttributeCond(AttributeCond.Type.EQ);
    loginDateCond.setSchema("loginDate");
    loginDateCond.setExpression("2009-05-26");
    SearchCond subCond = SearchCond.getAndCond(SearchCond.getLeafCond(fullnameLeafCond), SearchCond.getLeafCond(groupCond));
    assertTrue(subCond.isValid());
    SearchCond cond = SearchCond.getAndCond(subCond, SearchCond.getLeafCond(loginDateCond));
    assertTrue(cond.isValid());
    List<User> users = searchDAO.search(cond, AnyTypeKind.USER);
    assertNotNull(users);
    assertEquals(1, users.size());
}
Also used : User(org.apache.syncope.core.persistence.api.entity.user.User) AttributeCond(org.apache.syncope.core.persistence.api.dao.search.AttributeCond) MembershipCond(org.apache.syncope.core.persistence.api.dao.search.MembershipCond) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Example 5 with MembershipCond

use of org.apache.syncope.core.persistence.api.dao.search.MembershipCond in project syncope by apache.

the class GroupServiceImpl method replace.

@Override
public Response replace(final String id, final SCIMGroup group) {
    if (!id.equals(group.getId())) {
        throw new BadRequestException(ErrorType.invalidPath, "Expected " + id + ", found " + group.getId());
    }
    ResponseBuilder builder = checkETag(Resource.Group, id);
    if (builder != null) {
        return builder.build();
    }
    // save current group members
    Set<String> beforeMembers = new HashSet<>();
    MembershipCond membCond = new MembershipCond();
    membCond.setGroup(id);
    SearchCond searchCond = SearchCond.getLeafCond(membCond);
    int count = userLogic().search(searchCond, 1, 1, Collections.<OrderByClause>emptyList(), SyncopeConstants.ROOT_REALM, false).getLeft();
    for (int page = 1; page <= (count / AnyDAO.DEFAULT_PAGE_SIZE) + 1; page++) {
        beforeMembers.addAll(userLogic().search(searchCond, page, AnyDAO.DEFAULT_PAGE_SIZE, Collections.<OrderByClause>emptyList(), SyncopeConstants.ROOT_REALM, false).getRight().stream().map(EntityTO::getKey).collect(Collectors.toSet()));
    }
    // update group, don't change members
    ProvisioningResult<GroupTO> result = groupLogic().update(AnyOperations.diff(binder().toGroupTO(group), groupLogic().read(id), false), false);
    // assign new members
    Set<String> afterMembers = new HashSet<>();
    group.getMembers().forEach(member -> {
        afterMembers.add(member.getValue());
        if (!beforeMembers.contains(member.getValue())) {
            UserPatch patch = new UserPatch();
            patch.setKey(member.getValue());
            patch.getMemberships().add(new MembershipPatch.Builder().operation(PatchOperation.ADD_REPLACE).group(result.getEntity().getKey()).build());
            try {
                userLogic().update(patch, false);
            } catch (Exception e) {
                LOG.error("While setting membership of {} to {}", result.getEntity().getKey(), member.getValue(), e);
            }
        }
    });
    // remove unconfirmed members
    beforeMembers.stream().filter(member -> !afterMembers.contains(member)).forEach(user -> {
        UserPatch patch = new UserPatch();
        patch.setKey(user);
        patch.getMemberships().add(new MembershipPatch.Builder().operation(PatchOperation.DELETE).group(result.getEntity().getKey()).build());
        try {
            userLogic().update(patch, false);
        } catch (Exception e) {
            LOG.error("While removing membership of {} from {}", result.getEntity().getKey(), user, e);
        }
    });
    return updateResponse(result.getEntity().getKey(), binder().toSCIMGroup(result.getEntity(), uriInfo.getAbsolutePathBuilder().path(result.getEntity().getKey()).build().toASCIIString(), Collections.<String>emptyList(), Collections.<String>emptyList()));
}
Also used : Arrays(java.util.Arrays) BadRequestException(org.apache.syncope.ext.scimv2.api.BadRequestException) ErrorType(org.apache.syncope.ext.scimv2.api.type.ErrorType) OrderByClause(org.apache.syncope.core.persistence.api.dao.search.OrderByClause) ArrayUtils(org.apache.commons.lang3.ArrayUtils) UserPatch(org.apache.syncope.common.lib.patch.UserPatch) ProvisioningResult(org.apache.syncope.common.lib.to.ProvisioningResult) StringUtils(org.apache.commons.lang3.StringUtils) HashSet(java.util.HashSet) MembershipPatch(org.apache.syncope.common.lib.patch.MembershipPatch) EntityTO(org.apache.syncope.common.lib.to.EntityTO) SortOrder(org.apache.syncope.ext.scimv2.api.type.SortOrder) ListResponse(org.apache.syncope.ext.scimv2.api.data.ListResponse) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) SyncopeConstants(org.apache.syncope.common.lib.SyncopeConstants) SCIMGroup(org.apache.syncope.ext.scimv2.api.data.SCIMGroup) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) Set(java.util.Set) GroupTO(org.apache.syncope.common.lib.to.GroupTO) Collectors(java.util.stream.Collectors) Resource(org.apache.syncope.ext.scimv2.api.type.Resource) Response(javax.ws.rs.core.Response) AnyDAO(org.apache.syncope.core.persistence.api.dao.AnyDAO) PatchOperation(org.apache.syncope.common.lib.types.PatchOperation) SCIMSearchRequest(org.apache.syncope.ext.scimv2.api.data.SCIMSearchRequest) Member(org.apache.syncope.ext.scimv2.api.data.Member) GroupService(org.apache.syncope.ext.scimv2.api.service.GroupService) MembershipCond(org.apache.syncope.core.persistence.api.dao.search.MembershipCond) Collections(java.util.Collections) AnyOperations(org.apache.syncope.common.lib.AnyOperations) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) MembershipCond(org.apache.syncope.core.persistence.api.dao.search.MembershipCond) UserPatch(org.apache.syncope.common.lib.patch.UserPatch) BadRequestException(org.apache.syncope.ext.scimv2.api.BadRequestException) GroupTO(org.apache.syncope.common.lib.to.GroupTO) EntityTO(org.apache.syncope.common.lib.to.EntityTO) OrderByClause(org.apache.syncope.core.persistence.api.dao.search.OrderByClause) BadRequestException(org.apache.syncope.ext.scimv2.api.BadRequestException) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) HashSet(java.util.HashSet)

Aggregations

MembershipCond (org.apache.syncope.core.persistence.api.dao.search.MembershipCond)11 SearchCond (org.apache.syncope.core.persistence.api.dao.search.SearchCond)8 Test (org.junit.jupiter.api.Test)7 User (org.apache.syncope.core.persistence.api.entity.user.User)6 AbstractTest (org.apache.syncope.core.persistence.jpa.AbstractTest)6 AttributeCond (org.apache.syncope.core.persistence.api.dao.search.AttributeCond)4 OrderByClause (org.apache.syncope.core.persistence.api.dao.search.OrderByClause)3 AnyTypeCond (org.apache.syncope.core.persistence.api.dao.search.AnyTypeCond)2 PrivilegeCond (org.apache.syncope.core.persistence.api.dao.search.PrivilegeCond)2 RelationshipCond (org.apache.syncope.core.persistence.api.dao.search.RelationshipCond)2 RelationshipTypeCond (org.apache.syncope.core.persistence.api.dao.search.RelationshipTypeCond)2 RoleCond (org.apache.syncope.core.persistence.api.dao.search.RoleCond)2 Member (org.apache.syncope.ext.scimv2.api.data.Member)2 SCIMGroup (org.apache.syncope.ext.scimv2.api.data.SCIMGroup)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1