Search in sources :

Example 41 with SearchCond

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

the class PushJobDelegate method doExecuteProvisioning.

@Override
protected String doExecuteProvisioning(final PushTask pushTask, final Connector connector, final boolean dryRun) throws JobExecutionException {
    LOG.debug("Executing push on {}", pushTask.getResource());
    List<PushActions> actions = new ArrayList<>();
    pushTask.getActions().forEach(impl -> {
        try {
            actions.add(ImplementationManager.build(impl));
        } catch (Exception e) {
            LOG.warn("While building {}", impl, e);
        }
    });
    profile = new ProvisioningProfile<>(connector, pushTask);
    profile.getActions().addAll(actions);
    profile.setDryRun(dryRun);
    profile.setResAct(null);
    if (!profile.isDryRun()) {
        for (PushActions action : actions) {
            action.beforeAll(profile);
        }
    }
    status.set("Initialization completed");
    // First realms...
    if (pushTask.getResource().getOrgUnit() != null) {
        status.set("Pushing realms");
        rhandler = buildRealmHandler();
        for (Realm realm : realmDAO.findDescendants(profile.getTask().getSourceRealm())) {
            // Never push the root realm
            if (realm.getParent() != null) {
                try {
                    rhandler.handle(realm.getKey());
                    reportHandled(SyncopeConstants.REALM_ANYTYPE, realm.getName());
                } catch (Exception e) {
                    LOG.warn("Failure pushing '{}' on '{}'", realm, pushTask.getResource(), e);
                    throw new JobExecutionException("While pushing " + realm + " on " + pushTask.getResource(), e);
                }
            }
        }
    }
    // ...then provisions for any types
    ahandler = buildAnyObjectHandler();
    uhandler = buildUserHandler();
    ghandler = buildGroupHandler();
    for (Provision provision : pushTask.getResource().getProvisions()) {
        if (provision.getMapping() != null) {
            status.set("Pushing " + provision.getAnyType().getKey());
            AnyDAO<?> anyDAO = getAnyDAO(provision.getAnyType().getKind());
            SyncopePushResultHandler handler;
            switch(provision.getAnyType().getKind()) {
                case USER:
                    handler = uhandler;
                    break;
                case GROUP:
                    handler = ghandler;
                    break;
                case ANY_OBJECT:
                default:
                    handler = ahandler;
            }
            Optional<? extends PushTaskAnyFilter> anyFilter = pushTask.getFilter(provision.getAnyType());
            String filter = anyFilter.isPresent() ? anyFilter.get().getFIQLCond() : null;
            SearchCond cond = StringUtils.isBlank(filter) ? anyDAO.getAllMatchingCond() : SearchCondConverter.convert(filter);
            int count = searchDAO.count(Collections.singleton(profile.getTask().getSourceRealm().getFullPath()), cond, provision.getAnyType().getKind());
            for (int page = 1; page <= (count / AnyDAO.DEFAULT_PAGE_SIZE) + 1 && !interrupt; page++) {
                List<? extends Any<?>> anys = searchDAO.search(Collections.singleton(profile.getTask().getSourceRealm().getFullPath()), cond, page, AnyDAO.DEFAULT_PAGE_SIZE, Collections.<OrderByClause>emptyList(), provision.getAnyType().getKind());
                doHandle(anys, handler, pushTask.getResource());
            }
        }
    }
    if (!profile.isDryRun() && !interrupt) {
        for (PushActions action : actions) {
            action.afterAll(profile);
        }
    }
    if (interrupt) {
        interrupted = true;
    }
    status.set("Push done");
    String result = createReport(profile.getResults(), pushTask.getResource(), dryRun);
    LOG.debug("Push result: {}", result);
    return result;
}
Also used : Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) ArrayList(java.util.ArrayList) JobExecutionException(org.quartz.JobExecutionException) SyncopePushResultHandler(org.apache.syncope.core.provisioning.api.pushpull.SyncopePushResultHandler) JobExecutionException(org.quartz.JobExecutionException) PushActions(org.apache.syncope.core.provisioning.api.pushpull.PushActions) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) Realm(org.apache.syncope.core.persistence.api.entity.Realm)

Example 42 with SearchCond

use of org.apache.syncope.core.persistence.api.dao.search.SearchCond 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)

Example 43 with SearchCond

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

the class SearchCondVisitor method complex.

private <E extends Enum<?>> SearchCond complex(final String operator, final String left, final String right, final List<SCIMComplexConf<E>> items) {
    if (left.endsWith(".type")) {
        Optional<SCIMComplexConf<E>> item = items.stream().filter(object -> object.getType().name().equals(StringUtils.strip(right, "\""))).findFirst();
        if (item.isPresent()) {
            AttributeCond attributeCond = new AttributeCond();
            attributeCond.setSchema(item.get().getValue());
            attributeCond.setType(AttributeCond.Type.ISNOTNULL);
            return SearchCond.getLeafCond(attributeCond);
        }
    } else if (!conf.getUserConf().getEmails().isEmpty() && (MULTIVALUE.contains(left) || left.endsWith(".value"))) {
        List<SearchCond> orConds = new ArrayList<>();
        items.forEach(item -> {
            AttributeCond cond = new AttributeCond();
            cond.setSchema(item.getValue());
            cond.setExpression(StringUtils.strip(right, "\""));
            orConds.add(setOperator(cond, operator));
        });
        if (!orConds.isEmpty()) {
            return SearchCond.getOrCond(orConds);
        }
    }
    return null;
}
Also used : Arrays(java.util.Arrays) SCIMUserConf(org.apache.syncope.common.lib.scim.SCIMUserConf) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) SCIMUserAddressConf(org.apache.syncope.common.lib.scim.SCIMUserAddressConf) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) List(java.util.List) AttributeCond(org.apache.syncope.core.persistence.api.dao.search.AttributeCond) Resource(org.apache.syncope.ext.scimv2.api.type.Resource) SCIMComplexConf(org.apache.syncope.common.lib.scim.SCIMComplexConf) SCIMConf(org.apache.syncope.common.lib.scim.SCIMConf) Map(java.util.Map) Optional(java.util.Optional) AnyCond(org.apache.syncope.core.persistence.api.dao.search.AnyCond) AttributeCond(org.apache.syncope.core.persistence.api.dao.search.AttributeCond) SCIMComplexConf(org.apache.syncope.common.lib.scim.SCIMComplexConf) ArrayList(java.util.ArrayList) List(java.util.List)

Example 44 with SearchCond

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

the class SCIMFilterTest method type.

@Test
public void type() {
    SearchCond cond = SearchCondConverter.convert(VISITOR, "userType eq \"Employee\" and (emails.type eq \"work\")");
    assertNotNull(cond);
    assertEquals(SearchCond.Type.AND, cond.getType());
    SearchCond left = cond.getLeftSearchCond();
    assertNotNull(left);
    assertNotNull(left.getAttributeCond());
    assertEquals("userType", left.getAttributeCond().getSchema());
    assertEquals(AttributeCond.Type.IEQ, left.getAttributeCond().getType());
    assertEquals("Employee", left.getAttributeCond().getExpression());
    SearchCond right = cond.getRightSearchCond();
    assertNotNull(right);
    assertNotNull(right.getAttributeCond());
    assertEquals("email", right.getAttributeCond().getSchema());
    assertEquals(AttributeCond.Type.ISNOTNULL, right.getAttributeCond().getType());
}
Also used : SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) Test(org.junit.jupiter.api.Test)

Example 45 with SearchCond

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

the class SCIMFilterTest method eq.

@Test
public void eq() {
    SearchCond cond = SearchCondConverter.convert(VISITOR, "userName eq \"bjensen\"");
    assertNotNull(cond);
    assertNotNull(cond.getAnyCond());
    assertEquals("username", cond.getAnyCond().getSchema());
    assertEquals(AttributeCond.Type.IEQ, cond.getAnyCond().getType());
    assertEquals("bjensen", cond.getAnyCond().getExpression());
}
Also used : SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) Test(org.junit.jupiter.api.Test)

Aggregations

SearchCond (org.apache.syncope.core.persistence.api.dao.search.SearchCond)74 Test (org.junit.jupiter.api.Test)55 AttributeCond (org.apache.syncope.core.persistence.api.dao.search.AttributeCond)30 AnyCond (org.apache.syncope.core.persistence.api.dao.search.AnyCond)26 AbstractTest (org.apache.syncope.core.persistence.jpa.AbstractTest)25 User (org.apache.syncope.core.persistence.api.entity.user.User)20 UserFiqlSearchConditionBuilder (org.apache.syncope.common.lib.search.UserFiqlSearchConditionBuilder)17 MembershipCond (org.apache.syncope.core.persistence.api.dao.search.MembershipCond)12 OrderByClause (org.apache.syncope.core.persistence.api.dao.search.OrderByClause)11 ArrayList (java.util.ArrayList)10 Group (org.apache.syncope.core.persistence.api.entity.group.Group)10 List (java.util.List)8 AnyTypeCond (org.apache.syncope.core.persistence.api.dao.search.AnyTypeCond)8 AssignableCond (org.apache.syncope.core.persistence.api.dao.search.AssignableCond)7 MemberCond (org.apache.syncope.core.persistence.api.dao.search.MemberCond)7 Collections (java.util.Collections)6 Collectors (java.util.stream.Collectors)6 SyncopeConstants (org.apache.syncope.common.lib.SyncopeConstants)6 RelationshipCond (org.apache.syncope.core.persistence.api.dao.search.RelationshipCond)6 ResourceCond (org.apache.syncope.core.persistence.api.dao.search.ResourceCond)6