Search in sources :

Example 6 with Item

use of org.apache.syncope.core.persistence.api.entity.resource.Item in project syncope by apache.

the class DefaultPullCorrelationRule method getSearchCond.

@Override
public SearchCond getSearchCond(final ConnectorObject connObj, final Provision provision) {
    Map<String, Item> mappingItems = provision.getMapping().getItems().stream().collect(Collectors.toMap(Item::getIntAttrName, Function.identity()));
    // search for anys by attribute(s) specified in the policy
    SearchCond searchCond = null;
    for (String schema : conf.getSchemas()) {
        Item mappingItem = mappingItems.get(schema);
        Attribute attr = mappingItem == null ? null : connObj.getAttributeByName(mappingItem.getExtAttrName());
        if (attr == null) {
            throw new IllegalArgumentException("Connector object does not contains the attributes to perform the search: " + schema);
        }
        AttributeCond.Type type;
        String expression = null;
        if (attr.getValue() == null || attr.getValue().isEmpty() || (attr.getValue().size() == 1 && attr.getValue().get(0) == null)) {
            type = AttributeCond.Type.ISNULL;
        } else {
            type = AttributeCond.Type.EQ;
            expression = attr.getValue().size() > 1 ? attr.getValue().toString() : attr.getValue().get(0).toString();
        }
        SearchCond nodeCond;
        // any objects: just key or name can be selected
        if ("key".equalsIgnoreCase(schema) || "username".equalsIgnoreCase(schema) || "name".equalsIgnoreCase(schema)) {
            AnyCond cond = new AnyCond();
            cond.setSchema(schema);
            cond.setType(type);
            cond.setExpression(expression);
            nodeCond = SearchCond.getLeafCond(cond);
        } else {
            AttributeCond cond = new AttributeCond();
            cond.setSchema(schema);
            cond.setType(type);
            cond.setExpression(expression);
            nodeCond = SearchCond.getLeafCond(cond);
        }
        searchCond = searchCond == null ? nodeCond : SearchCond.getAndCond(searchCond, nodeCond);
    }
    return searchCond;
}
Also used : Item(org.apache.syncope.core.persistence.api.entity.resource.Item) Attribute(org.identityconnectors.framework.common.objects.Attribute) AttributeCond(org.apache.syncope.core.persistence.api.dao.search.AttributeCond) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) AnyCond(org.apache.syncope.core.persistence.api.dao.search.AnyCond)

Example 7 with Item

use of org.apache.syncope.core.persistence.api.entity.resource.Item in project syncope by apache.

the class ResourceDataBinderImpl method populateMapping.

private void populateMapping(final MappingTO mappingTO, final Mapping mapping, final AnyTypeClassTO allowedSchemas) {
    mapping.setConnObjectLink(mappingTO.getConnObjectLink());
    SyncopeClientCompositeException scce = SyncopeClientException.buildComposite();
    SyncopeClientException invalidMapping = SyncopeClientException.build(ClientExceptionType.InvalidMapping);
    SyncopeClientException requiredValuesMissing = SyncopeClientException.build(ClientExceptionType.RequiredValuesMissing);
    for (ItemTO itemTO : mappingTO.getItems()) {
        if (itemTO == null) {
            LOG.error("Null {}", ItemTO.class.getSimpleName());
            invalidMapping.getElements().add("Null " + ItemTO.class.getSimpleName());
        } else if (itemTO.getIntAttrName() == null) {
            requiredValuesMissing.getElements().add("intAttrName");
            scce.addException(requiredValuesMissing);
        } else {
            IntAttrName intAttrName = null;
            try {
                intAttrName = intAttrNameParser.parse(itemTO.getIntAttrName(), mapping.getProvision().getAnyType().getKind());
            } catch (ParseException e) {
                LOG.error("Invalid intAttrName '{}'", itemTO.getIntAttrName(), e);
            }
            if (intAttrName == null || intAttrName.getSchemaType() == null && intAttrName.getField() == null && intAttrName.getPrivilegesOfApplication() == null) {
                LOG.error("'{}' not existing", itemTO.getIntAttrName());
                invalidMapping.getElements().add("'" + itemTO.getIntAttrName() + "' not existing");
            } else {
                boolean allowed = true;
                if (intAttrName.getSchemaType() != null && intAttrName.getEnclosingGroup() == null && intAttrName.getRelatedAnyObject() == null && intAttrName.getPrivilegesOfApplication() == null) {
                    switch(intAttrName.getSchemaType()) {
                        case PLAIN:
                            allowed = allowedSchemas.getPlainSchemas().contains(intAttrName.getSchemaName());
                            break;
                        case DERIVED:
                            allowed = allowedSchemas.getDerSchemas().contains(intAttrName.getSchemaName());
                            break;
                        case VIRTUAL:
                            allowed = allowedSchemas.getVirSchemas().contains(intAttrName.getSchemaName());
                            break;
                        default:
                    }
                }
                if (allowed) {
                    // no mandatory condition implies mandatory condition false
                    if (!JexlUtils.isExpressionValid(itemTO.getMandatoryCondition() == null ? "false" : itemTO.getMandatoryCondition())) {
                        SyncopeClientException invalidMandatoryCondition = SyncopeClientException.build(ClientExceptionType.InvalidValues);
                        invalidMandatoryCondition.getElements().add(itemTO.getMandatoryCondition());
                        scce.addException(invalidMandatoryCondition);
                    }
                    MappingItem item = entityFactory.newEntity(MappingItem.class);
                    BeanUtils.copyProperties(itemTO, item, ITEM_IGNORE_PROPERTIES);
                    item.setMapping(mapping);
                    if (item.isConnObjectKey()) {
                        if (intAttrName.getSchemaType() == SchemaType.VIRTUAL) {
                            invalidMapping.getElements().add("Virtual attributes cannot be set as ConnObjectKey");
                        }
                        if ("password".equals(intAttrName.getField())) {
                            invalidMapping.getElements().add("Password attributes cannot be set as ConnObjectKey");
                        }
                        mapping.setConnObjectKeyItem(item);
                    } else {
                        mapping.add(item);
                    }
                    itemTO.getTransformers().forEach(transformerKey -> {
                        Implementation transformer = implementationDAO.find(transformerKey);
                        if (transformer == null) {
                            LOG.debug("Invalid " + Implementation.class.getSimpleName() + " {}, ignoring...", transformerKey);
                        } else {
                            item.add(transformer);
                        }
                    });
                    // remove all implementations not contained in the TO
                    item.getTransformers().removeIf(implementation -> !itemTO.getTransformers().contains(implementation.getKey()));
                    if (intAttrName.getEnclosingGroup() != null && item.getPurpose() != MappingPurpose.PROPAGATION) {
                        invalidMapping.getElements().add("Only " + MappingPurpose.PROPAGATION.name() + " allowed when referring to groups");
                    }
                    if (intAttrName.getRelatedAnyObject() != null && item.getPurpose() != MappingPurpose.PROPAGATION) {
                        invalidMapping.getElements().add("Only " + MappingPurpose.PROPAGATION.name() + " allowed when referring to any objects");
                    }
                    if (intAttrName.getPrivilegesOfApplication() != null && item.getPurpose() != MappingPurpose.PROPAGATION) {
                        invalidMapping.getElements().add("Only " + MappingPurpose.PROPAGATION.name() + " allowed when referring to privileges");
                    }
                    if (intAttrName.getSchemaType() == SchemaType.DERIVED && item.getPurpose() != MappingPurpose.PROPAGATION) {
                        invalidMapping.getElements().add("Only " + MappingPurpose.PROPAGATION.name() + " allowed for derived");
                    }
                    if (intAttrName.getSchemaType() == SchemaType.VIRTUAL) {
                        if (item.getPurpose() != MappingPurpose.PROPAGATION) {
                            invalidMapping.getElements().add("Only " + MappingPurpose.PROPAGATION.name() + " allowed for virtual");
                        }
                        VirSchema schema = virSchemaDAO.find(item.getIntAttrName());
                        if (schema != null && schema.getProvision().equals(item.getMapping().getProvision())) {
                            invalidMapping.getElements().add("No need to map virtual schema on linking resource");
                        }
                    }
                } else {
                    LOG.error("'{}' not allowed", itemTO.getIntAttrName());
                    invalidMapping.getElements().add("'" + itemTO.getIntAttrName() + "' not allowed");
                }
            }
        }
    }
    if (!invalidMapping.getElements().isEmpty()) {
        scce.addException(invalidMapping);
    }
    if (scce.hasExceptions()) {
        throw scce;
    }
}
Also used : ProvisionTO(org.apache.syncope.common.lib.to.ProvisionTO) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) PullPolicy(org.apache.syncope.core.persistence.api.entity.policy.PullPolicy) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType) Autowired(org.springframework.beans.factory.annotation.Autowired) Entity(org.apache.syncope.core.persistence.api.entity.Entity) JexlUtils(org.apache.syncope.core.provisioning.java.jexl.JexlUtils) OrgUnit(org.apache.syncope.core.persistence.api.entity.resource.OrgUnit) AuthContextUtils(org.apache.syncope.core.spring.security.AuthContextUtils) ParseException(java.text.ParseException) ImplementationDAO(org.apache.syncope.core.persistence.api.dao.ImplementationDAO) AnyTypeClass(org.apache.syncope.core.persistence.api.entity.AnyTypeClass) MappingTO(org.apache.syncope.common.lib.to.MappingTO) OrgUnitItem(org.apache.syncope.core.persistence.api.entity.resource.OrgUnitItem) Implementation(org.apache.syncope.core.persistence.api.entity.Implementation) SchemaType(org.apache.syncope.common.lib.types.SchemaType) ConnInstanceDAO(org.apache.syncope.core.persistence.api.dao.ConnInstanceDAO) ResourceDataBinder(org.apache.syncope.core.provisioning.api.data.ResourceDataBinder) Collectors(java.util.stream.Collectors) MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) AnyTypeDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeDAO) EntityFactory(org.apache.syncope.core.persistence.api.entity.EntityFactory) List(java.util.List) AccountPolicy(org.apache.syncope.core.persistence.api.entity.policy.AccountPolicy) Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) ObjectClass(org.identityconnectors.framework.common.objects.ObjectClass) PolicyDAO(org.apache.syncope.core.persistence.api.dao.PolicyDAO) ConfDAO(org.apache.syncope.core.persistence.api.dao.ConfDAO) ExternalResourceHistoryConfDAO(org.apache.syncope.core.persistence.api.dao.ExternalResourceHistoryConfDAO) IntAttrName(org.apache.syncope.core.provisioning.api.IntAttrName) ResourceHistoryConfTO(org.apache.syncope.common.lib.to.ResourceHistoryConfTO) BeanUtils(org.apache.syncope.core.spring.BeanUtils) HashSet(java.util.HashSet) PasswordPolicy(org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy) ItemTO(org.apache.syncope.common.lib.to.ItemTO) SyncopeClientCompositeException(org.apache.syncope.common.lib.SyncopeClientCompositeException) ClientExceptionType(org.apache.syncope.common.lib.types.ClientExceptionType) Item(org.apache.syncope.core.persistence.api.entity.resource.Item) AnyTypeClassTO(org.apache.syncope.common.lib.to.AnyTypeClassTO) Mapping(org.apache.syncope.core.persistence.api.entity.resource.Mapping) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ResourceTO(org.apache.syncope.common.lib.to.ResourceTO) ItemContainerTO(org.apache.syncope.common.lib.to.ItemContainerTO) IteratorChain(org.apache.syncope.common.lib.collections.IteratorChain) ExternalResourceHistoryConf(org.apache.syncope.core.persistence.api.entity.resource.ExternalResourceHistoryConf) ConnInstance(org.apache.syncope.core.persistence.api.entity.ConnInstance) VirSchema(org.apache.syncope.core.persistence.api.entity.VirSchema) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) Component(org.springframework.stereotype.Component) MappingPurpose(org.apache.syncope.common.lib.types.MappingPurpose) OrgUnitTO(org.apache.syncope.common.lib.to.OrgUnitTO) VirSchemaDAO(org.apache.syncope.core.persistence.api.dao.VirSchemaDAO) IntAttrNameParser(org.apache.syncope.core.provisioning.java.IntAttrNameParser) Collections(java.util.Collections) AnyTypeClassDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeClassDAO) SyncopeClientCompositeException(org.apache.syncope.common.lib.SyncopeClientCompositeException) MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) VirSchema(org.apache.syncope.core.persistence.api.entity.VirSchema) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) ParseException(java.text.ParseException) ItemTO(org.apache.syncope.common.lib.to.ItemTO) Implementation(org.apache.syncope.core.persistence.api.entity.Implementation) IntAttrName(org.apache.syncope.core.provisioning.api.IntAttrName)

Example 8 with Item

use of org.apache.syncope.core.persistence.api.entity.resource.Item in project syncope by apache.

the class MappingManagerImpl method prepareAttrs.

@Transactional(readOnly = true)
@Override
public Pair<String, Set<Attribute>> prepareAttrs(final Any<?> any, final String password, final boolean changePwd, final Boolean enable, final Provision provision) {
    LOG.debug("Preparing resource attributes for {} with provision {} for attributes {}", any, provision, any.getPlainAttrs());
    Set<Attribute> attributes = new HashSet<>();
    String connObjectKey = null;
    for (Item mapItem : MappingUtils.getPropagationItems(provision.getMapping().getItems())) {
        LOG.debug("Processing expression '{}'", mapItem.getIntAttrName());
        try {
            Pair<String, Attribute> preparedAttr = prepareAttr(provision, mapItem, any, password);
            if (preparedAttr != null) {
                if (preparedAttr.getLeft() != null) {
                    connObjectKey = preparedAttr.getLeft();
                }
                if (preparedAttr.getRight() != null) {
                    Attribute alreadyAdded = AttributeUtil.find(preparedAttr.getRight().getName(), attributes);
                    if (alreadyAdded == null) {
                        attributes.add(preparedAttr.getRight());
                    } else {
                        attributes.remove(alreadyAdded);
                        Set<Object> values = new HashSet<>();
                        if (alreadyAdded.getValue() != null && !alreadyAdded.getValue().isEmpty()) {
                            values.addAll(alreadyAdded.getValue());
                        }
                        if (preparedAttr.getRight().getValue() != null) {
                            values.addAll(preparedAttr.getRight().getValue());
                        }
                        attributes.add(AttributeBuilder.build(preparedAttr.getRight().getName(), values));
                    }
                }
            }
        } catch (Exception e) {
            LOG.error("Expression '{}' processing failed", mapItem.getIntAttrName(), e);
        }
    }
    Optional<MappingItem> connObjectKeyItem = MappingUtils.getConnObjectKeyItem(provision);
    if (connObjectKeyItem.isPresent()) {
        Attribute connObjectKeyExtAttr = AttributeUtil.find(connObjectKeyItem.get().getExtAttrName(), attributes);
        if (connObjectKeyExtAttr != null) {
            attributes.remove(connObjectKeyExtAttr);
            attributes.add(AttributeBuilder.build(connObjectKeyItem.get().getExtAttrName(), connObjectKey));
        }
        Name name = MappingUtils.evaluateNAME(any, provision, connObjectKey);
        attributes.add(name);
        if (connObjectKey != null && !connObjectKey.equals(name.getNameValue()) && connObjectKeyExtAttr == null) {
            attributes.add(AttributeBuilder.build(connObjectKeyItem.get().getExtAttrName(), connObjectKey));
        }
    }
    if (enable != null) {
        attributes.add(AttributeBuilder.buildEnabled(enable));
    }
    if (!changePwd) {
        Attribute pwdAttr = AttributeUtil.find(OperationalAttributes.PASSWORD_NAME, attributes);
        if (pwdAttr != null) {
            attributes.remove(pwdAttr);
        }
    }
    return Pair.of(connObjectKey, attributes);
}
Also used : OrgUnitItem(org.apache.syncope.core.persistence.api.entity.resource.OrgUnitItem) MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) Item(org.apache.syncope.core.persistence.api.entity.resource.Item) MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) Attribute(org.identityconnectors.framework.common.objects.Attribute) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) ParseException(java.text.ParseException) ParsingValidationException(org.apache.syncope.core.persistence.api.attrvalue.validation.ParsingValidationException) HashSet(java.util.HashSet) IntAttrName(org.apache.syncope.core.provisioning.api.IntAttrName) Name(org.identityconnectors.framework.common.objects.Name) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Item (org.apache.syncope.core.persistence.api.entity.resource.Item)8 HashSet (java.util.HashSet)7 MappingItem (org.apache.syncope.core.persistence.api.entity.resource.MappingItem)6 OrgUnitItem (org.apache.syncope.core.persistence.api.entity.resource.OrgUnitItem)5 Attribute (org.identityconnectors.framework.common.objects.Attribute)5 ParseException (java.text.ParseException)4 Collections (java.util.Collections)4 List (java.util.List)4 Set (java.util.Set)4 VirSchemaDAO (org.apache.syncope.core.persistence.api.dao.VirSchemaDAO)4 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 StringUtils (org.apache.commons.lang3.StringUtils)3 Pair (org.apache.commons.lang3.tuple.Pair)3 AttrTO (org.apache.syncope.common.lib.to.AttrTO)3 AnyObjectDAO (org.apache.syncope.core.persistence.api.dao.AnyObjectDAO)3 GroupDAO (org.apache.syncope.core.persistence.api.dao.GroupDAO)3 UserDAO (org.apache.syncope.core.persistence.api.dao.UserDAO)3 Any (org.apache.syncope.core.persistence.api.entity.Any)3 AnyUtilsFactory (org.apache.syncope.core.persistence.api.entity.AnyUtilsFactory)3