Search in sources :

Example 1 with Item

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

the class PropagationManagerImpl method createTasks.

/**
 * Create propagation tasks.
 *
 * @param any to be provisioned
 * @param password clear text password to be provisioned
 * @param changePwd whether password should be included for propagation attributes or not
 * @param enable whether user must be enabled or not
 * @param deleteOnResource whether any must be deleted anyway from external resource or not
 * @param propByRes operation to be performed per resource
 * @param vAttrs virtual attributes to be set
 * @return list of propagation tasks created
 */
protected List<PropagationTaskTO> createTasks(final Any<?> any, final String password, final boolean changePwd, final Boolean enable, final boolean deleteOnResource, final PropagationByResource propByRes, final Collection<AttrTO> vAttrs) {
    LOG.debug("Provisioning {}:\n{}", any, propByRes);
    // Avoid duplicates - see javadoc
    propByRes.purge();
    LOG.debug("After purge {}:\n{}", any, propByRes);
    // Virtual attributes
    Set<String> virtualResources = new HashSet<>();
    virtualResources.addAll(propByRes.get(ResourceOperation.CREATE));
    virtualResources.addAll(propByRes.get(ResourceOperation.UPDATE));
    virtualResources.addAll(dao(any.getType().getKind()).findAllResourceKeys(any.getKey()));
    Map<String, Set<Attribute>> vAttrMap = new HashMap<>();
    if (vAttrs != null) {
        vAttrs.forEach(vAttr -> {
            VirSchema schema = virSchemaDAO.find(vAttr.getSchema());
            if (schema == null) {
                LOG.warn("Ignoring invalid {} {}", VirSchema.class.getSimpleName(), vAttr.getSchema());
            } else if (schema.isReadonly()) {
                LOG.warn("Ignoring read-only {} {}", VirSchema.class.getSimpleName(), vAttr.getSchema());
            } else if (anyUtilsFactory.getInstance(any).getAllowedSchemas(any, VirSchema.class).contains(schema) && virtualResources.contains(schema.getProvision().getResource().getKey())) {
                Set<Attribute> values = vAttrMap.get(schema.getProvision().getResource().getKey());
                if (values == null) {
                    values = new HashSet<>();
                    vAttrMap.put(schema.getProvision().getResource().getKey(), values);
                }
                values.add(AttributeBuilder.build(schema.getExtAttrName(), vAttr.getValues()));
                propByRes.add(ResourceOperation.UPDATE, schema.getProvision().getResource().getKey());
            } else {
                LOG.warn("{} not owned by or {} not allowed for {}", schema.getProvision().getResource(), schema, any);
            }
        });
    }
    LOG.debug("With virtual attributes {}:\n{}\n{}", any, propByRes, vAttrMap);
    List<PropagationTaskTO> tasks = new ArrayList<>();
    propByRes.asMap().forEach((resourceKey, operation) -> {
        ExternalResource resource = resourceDAO.find(resourceKey);
        Provision provision = resource == null ? null : resource.getProvision(any.getType()).orElse(null);
        List<? extends Item> mappingItems = provision == null ? Collections.<Item>emptyList() : MappingUtils.getPropagationItems(provision.getMapping().getItems());
        if (resource == null) {
            LOG.error("Invalid resource name specified: {}, ignoring...", resourceKey);
        } else if (provision == null) {
            LOG.error("No provision specified on resource {} for type {}, ignoring...", resource, any.getType());
        } else if (mappingItems.isEmpty()) {
            LOG.warn("Requesting propagation for {} but no propagation mapping provided for {}", any.getType(), resource);
        } else {
            PropagationTaskTO task = new PropagationTaskTO();
            task.setResource(resource.getKey());
            task.setObjectClassName(provision.getObjectClass().getObjectClassValue());
            task.setAnyTypeKind(any.getType().getKind());
            task.setAnyType(any.getType().getKey());
            if (!deleteOnResource) {
                task.setEntityKey(any.getKey());
            }
            task.setOperation(operation);
            task.setOldConnObjectKey(propByRes.getOldConnObjectKey(resource.getKey()));
            Pair<String, Set<Attribute>> preparedAttrs = mappingManager.prepareAttrs(any, password, changePwd, enable, provision);
            task.setConnObjectKey(preparedAttrs.getKey());
            // Check if any of mandatory attributes (in the mapping) is missing or not received any value:
            // if so, add special attributes that will be evaluated by PropagationTaskExecutor
            List<String> mandatoryMissing = new ArrayList<>();
            List<String> mandatoryNullOrEmpty = new ArrayList<>();
            mappingItems.stream().filter(item -> (!item.isConnObjectKey() && JexlUtils.evaluateMandatoryCondition(item.getMandatoryCondition(), any))).forEachOrdered(item -> {
                Attribute attr = AttributeUtil.find(item.getExtAttrName(), preparedAttrs.getValue());
                if (attr == null) {
                    mandatoryMissing.add(item.getExtAttrName());
                } else if (attr.getValue() == null || attr.getValue().isEmpty()) {
                    mandatoryNullOrEmpty.add(item.getExtAttrName());
                }
            });
            if (!mandatoryMissing.isEmpty()) {
                preparedAttrs.getValue().add(AttributeBuilder.build(PropagationTaskExecutor.MANDATORY_MISSING_ATTR_NAME, mandatoryMissing));
            }
            if (!mandatoryNullOrEmpty.isEmpty()) {
                preparedAttrs.getValue().add(AttributeBuilder.build(PropagationTaskExecutor.MANDATORY_NULL_OR_EMPTY_ATTR_NAME, mandatoryNullOrEmpty));
            }
            if (vAttrMap.containsKey(resource.getKey())) {
                preparedAttrs.getValue().addAll(vAttrMap.get(resource.getKey()));
            }
            task.setAttributes(POJOHelper.serialize(preparedAttrs.getValue()));
            tasks.add(task);
            LOG.debug("PropagationTask created: {}", task);
        }
    });
    return tasks;
}
Also used : Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) PropagationManager(org.apache.syncope.core.provisioning.api.propagation.PropagationManager) POJOHelper(org.apache.syncope.core.provisioning.api.serialization.POJOHelper) AttrTO(org.apache.syncope.common.lib.to.AttrTO) AnyUtilsFactory(org.apache.syncope.core.persistence.api.entity.AnyUtilsFactory) Realm(org.apache.syncope.core.persistence.api.entity.Realm) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) UserPatch(org.apache.syncope.common.lib.patch.UserPatch) ResourceOperation(org.apache.syncope.common.lib.types.ResourceOperation) WorkflowResult(org.apache.syncope.core.provisioning.api.WorkflowResult) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AnyTypeKind(org.apache.syncope.common.lib.types.AnyTypeKind) JexlUtils(org.apache.syncope.core.provisioning.java.jexl.JexlUtils) Attribute(org.identityconnectors.framework.common.objects.Attribute) PropagationTaskTO(org.apache.syncope.common.lib.to.PropagationTaskTO) GroupDAO(org.apache.syncope.core.persistence.api.dao.GroupDAO) Pair(org.apache.commons.lang3.tuple.Pair) AnyObjectDAO(org.apache.syncope.core.persistence.api.dao.AnyObjectDAO) ConnObjectUtils(org.apache.syncope.core.provisioning.java.utils.ConnObjectUtils) MappingManager(org.apache.syncope.core.provisioning.api.MappingManager) Map(java.util.Map) PropagationByResource(org.apache.syncope.core.provisioning.api.PropagationByResource) Item(org.apache.syncope.core.persistence.api.entity.resource.Item) OrgUnit(org.apache.syncope.core.persistence.api.entity.resource.OrgUnit) Logger(org.slf4j.Logger) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) VirSchema(org.apache.syncope.core.persistence.api.entity.VirSchema) MappingUtils(org.apache.syncope.core.provisioning.java.utils.MappingUtils) EntityFactory(org.apache.syncope.core.persistence.api.entity.EntityFactory) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) List(java.util.List) Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) AttributeUtil(org.identityconnectors.framework.common.objects.AttributeUtil) AttributeBuilder(org.identityconnectors.framework.common.objects.AttributeBuilder) AnyDAO(org.apache.syncope.core.persistence.api.dao.AnyDAO) PropagationTaskExecutor(org.apache.syncope.core.provisioning.api.propagation.PropagationTaskExecutor) VirSchemaDAO(org.apache.syncope.core.persistence.api.dao.VirSchemaDAO) Collections(java.util.Collections) ExternalResourceDAO(org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO) Any(org.apache.syncope.core.persistence.api.entity.Any) Transactional(org.springframework.transaction.annotation.Transactional) HashSet(java.util.HashSet) Set(java.util.Set) PropagationTaskTO(org.apache.syncope.common.lib.to.PropagationTaskTO) HashMap(java.util.HashMap) VirSchema(org.apache.syncope.core.persistence.api.entity.VirSchema) Attribute(org.identityconnectors.framework.common.objects.Attribute) ArrayList(java.util.ArrayList) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) HashSet(java.util.HashSet)

Example 2 with Item

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

the class MappingUtils method buildOperationOptions.

/**
 * Build options for requesting all mapped connector attributes.
 *
 * @param iterator items
 * @return options for requesting all mapped connector attributes
 * @see OperationOptions
 */
public static OperationOptions buildOperationOptions(final Iterator<? extends Item> iterator) {
    OperationOptionsBuilder builder = new OperationOptionsBuilder();
    Set<String> attrsToGet = new HashSet<>();
    attrsToGet.add(Name.NAME);
    attrsToGet.add(Uid.NAME);
    attrsToGet.add(OperationalAttributes.ENABLE_NAME);
    while (iterator.hasNext()) {
        Item item = iterator.next();
        if (item.getPurpose() != MappingPurpose.NONE) {
            attrsToGet.add(item.getExtAttrName());
        }
    }
    builder.setAttributesToGet(attrsToGet);
    return builder.build();
}
Also used : Item(org.apache.syncope.core.persistence.api.entity.resource.Item) MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) OperationOptionsBuilder(org.identityconnectors.framework.common.objects.OperationOptionsBuilder) HashSet(java.util.HashSet)

Example 3 with Item

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

the class MappingManagerImpl method prepareAttrs.

@Override
public Pair<String, Set<Attribute>> prepareAttrs(final Realm realm, final OrgUnit orgUnit) {
    LOG.debug("Preparing resource attributes for {} with orgUnit {}", realm, orgUnit);
    Set<Attribute> attributes = new HashSet<>();
    String connObjectKey = null;
    for (Item orgUnitItem : MappingUtils.getPropagationItems(orgUnit.getItems())) {
        LOG.debug("Processing expression '{}'", orgUnitItem.getIntAttrName());
        String value = getIntValue(realm, orgUnitItem);
        if (orgUnitItem.isConnObjectKey()) {
            connObjectKey = value;
        }
        Attribute alreadyAdded = AttributeUtil.find(orgUnitItem.getExtAttrName(), attributes);
        if (alreadyAdded == null) {
            if (value == null) {
                attributes.add(AttributeBuilder.build(orgUnitItem.getExtAttrName()));
            } else {
                attributes.add(AttributeBuilder.build(orgUnitItem.getExtAttrName(), value));
            }
        } else if (value != null) {
            attributes.remove(alreadyAdded);
            Set<Object> values = new HashSet<>();
            if (alreadyAdded.getValue() != null && !alreadyAdded.getValue().isEmpty()) {
                values.addAll(alreadyAdded.getValue());
            }
            values.add(value);
            attributes.add(AttributeBuilder.build(orgUnitItem.getExtAttrName(), values));
        }
    }
    Optional<? extends OrgUnitItem> connObjectKeyItem = orgUnit.getConnObjectKeyItem();
    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));
        }
        attributes.add(MappingUtils.evaluateNAME(realm, orgUnit, connObjectKey));
    }
    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) Set(java.util.Set) HashSet(java.util.HashSet) Attribute(org.identityconnectors.framework.common.objects.Attribute) HashSet(java.util.HashSet)

Example 4 with Item

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

the class MappingManagerImpl method getIntValues.

@Transactional(readOnly = true)
@Override
public List<PlainAttrValue> getIntValues(final Provision provision, final Item mapItem, final IntAttrName intAttrName, final Any<?> any) {
    LOG.debug("Get internal values for {} as '{}' on {}", any, mapItem.getIntAttrName(), provision.getResource());
    Any<?> reference = null;
    Membership<?> membership = null;
    if (intAttrName.getEnclosingGroup() == null && intAttrName.getRelatedAnyObject() == null) {
        reference = any;
    }
    if (any instanceof GroupableRelatable) {
        GroupableRelatable<?, ?, ?, ?, ?> groupableRelatable = (GroupableRelatable<?, ?, ?, ?, ?>) any;
        if (intAttrName.getEnclosingGroup() != null) {
            Group group = groupDAO.findByName(intAttrName.getEnclosingGroup());
            if (group == null || !groupableRelatable.getMembership(group.getKey()).isPresent()) {
                LOG.warn("No membership for {} in {}, ignoring", intAttrName.getEnclosingGroup(), groupableRelatable);
            } else {
                reference = group;
            }
        } else if (intAttrName.getRelatedAnyObject() != null) {
            AnyObject anyObject = anyObjectDAO.findByName(intAttrName.getRelatedAnyObject());
            if (anyObject == null || groupableRelatable.getRelationships(anyObject.getKey()).isEmpty()) {
                LOG.warn("No relationship for {} in {}, ignoring", intAttrName.getRelatedAnyObject(), groupableRelatable);
            } else {
                reference = anyObject;
            }
        } else if (intAttrName.getMembershipOfGroup() != null) {
            Group group = groupDAO.findByName(intAttrName.getMembershipOfGroup());
            membership = groupableRelatable.getMembership(group.getKey()).orElse(null);
        }
    }
    if (reference == null) {
        LOG.warn("Could not determine the reference instance for {}", mapItem.getIntAttrName());
        return Collections.emptyList();
    }
    List<PlainAttrValue> values = new ArrayList<>();
    boolean transform = true;
    AnyUtils anyUtils = anyUtilsFactory.getInstance(reference);
    if (intAttrName.getField() != null) {
        PlainAttrValue attrValue = anyUtils.newPlainAttrValue();
        switch(intAttrName.getField()) {
            case "key":
                attrValue.setStringValue(reference.getKey());
                values.add(attrValue);
                break;
            case "realm":
                attrValue.setStringValue(reference.getRealm().getFullPath());
                values.add(attrValue);
                break;
            case "password":
                // ignore
                break;
            case "userOwner":
            case "groupOwner":
                Mapping uMapping = provision.getAnyType().equals(anyTypeDAO.findUser()) ? provision.getMapping() : null;
                Mapping gMapping = provision.getAnyType().equals(anyTypeDAO.findGroup()) ? provision.getMapping() : null;
                if (reference instanceof Group) {
                    Group group = (Group) reference;
                    String groupOwnerValue = null;
                    if (group.getUserOwner() != null && uMapping != null) {
                        groupOwnerValue = getGroupOwnerValue(provision, group.getUserOwner());
                    }
                    if (group.getGroupOwner() != null && gMapping != null) {
                        groupOwnerValue = getGroupOwnerValue(provision, group.getGroupOwner());
                    }
                    if (StringUtils.isNotBlank(groupOwnerValue)) {
                        attrValue.setStringValue(groupOwnerValue);
                        values.add(attrValue);
                    }
                }
                break;
            case "suspended":
                if (reference instanceof User) {
                    attrValue.setBooleanValue(((User) reference).isSuspended());
                    values.add(attrValue);
                }
                break;
            case "mustChangePassword":
                if (reference instanceof User) {
                    attrValue.setBooleanValue(((User) reference).isMustChangePassword());
                    values.add(attrValue);
                }
                break;
            default:
                try {
                    Object fieldValue = FieldUtils.readField(reference, intAttrName.getField(), true);
                    if (fieldValue instanceof Date) {
                        // needed because ConnId does not natively supports the Date type
                        attrValue.setStringValue(DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT.format((Date) fieldValue));
                    } else if (Boolean.TYPE.isInstance(fieldValue)) {
                        attrValue.setBooleanValue((Boolean) fieldValue);
                    } else if (Double.TYPE.isInstance(fieldValue) || Float.TYPE.isInstance(fieldValue)) {
                        attrValue.setDoubleValue((Double) fieldValue);
                    } else if (Long.TYPE.isInstance(fieldValue) || Integer.TYPE.isInstance(fieldValue)) {
                        attrValue.setLongValue((Long) fieldValue);
                    } else {
                        attrValue.setStringValue(fieldValue.toString());
                    }
                    values.add(attrValue);
                } catch (Exception e) {
                    LOG.error("Could not read value of '{}' from {}", intAttrName.getField(), reference, e);
                }
        }
    } else if (intAttrName.getSchemaType() != null) {
        switch(intAttrName.getSchemaType()) {
            case PLAIN:
                PlainAttr<?> attr;
                if (membership == null) {
                    attr = reference.getPlainAttr(intAttrName.getSchemaName()).orElse(null);
                } else {
                    attr = ((GroupableRelatable<?, ?, ?, ?, ?>) reference).getPlainAttr(intAttrName.getSchemaName(), membership).orElse(null);
                }
                if (attr == null) {
                    LOG.warn("Invalid PlainSchema {} or PlainAttr not found for {}", intAttrName.getSchemaName(), reference);
                } else {
                    if (attr.getUniqueValue() != null) {
                        values.add(anyUtils.clonePlainAttrValue(attr.getUniqueValue()));
                    } else if (attr.getValues() != null) {
                        attr.getValues().forEach(value -> values.add(anyUtils.clonePlainAttrValue(value)));
                    }
                }
                break;
            case DERIVED:
                DerSchema derSchema = derSchemaDAO.find(intAttrName.getSchemaName());
                if (derSchema == null) {
                    LOG.warn("Invalid DerSchema: {}", intAttrName.getSchemaName());
                } else {
                    String derValue = membership == null ? derAttrHandler.getValue(reference, derSchema) : derAttrHandler.getValue(reference, membership, derSchema);
                    if (derValue != null) {
                        PlainAttrValue attrValue = anyUtils.newPlainAttrValue();
                        attrValue.setStringValue(derValue);
                        values.add(attrValue);
                    }
                }
                break;
            case VIRTUAL:
                // virtual attributes don't get transformed
                transform = false;
                VirSchema virSchema = virSchemaDAO.find(intAttrName.getSchemaName());
                if (virSchema == null) {
                    LOG.warn("Invalid VirSchema: {}", intAttrName.getSchemaName());
                } else {
                    LOG.debug("Expire entry cache {}-{}", reference, intAttrName.getSchemaName());
                    virAttrCache.expire(reference.getType().getKey(), reference.getKey(), intAttrName.getSchemaName());
                    List<String> virValues = membership == null ? virAttrHandler.getValues(reference, virSchema) : virAttrHandler.getValues(reference, membership, virSchema);
                    virValues.forEach(virValue -> {
                        PlainAttrValue attrValue = anyUtils.newPlainAttrValue();
                        attrValue.setStringValue(virValue);
                        values.add(attrValue);
                    });
                }
                break;
            default:
        }
    } else if (intAttrName.getPrivilegesOfApplication() != null && reference instanceof User) {
        Application application = applicationDAO.find(intAttrName.getPrivilegesOfApplication());
        if (application == null) {
            LOG.warn("Invalid application: {}", intAttrName.getPrivilegesOfApplication());
        } else {
            userDAO.findAllRoles((User) reference).stream().flatMap(role -> role.getPrivileges(application).stream()).forEach(privilege -> {
                PlainAttrValue attrValue = anyUtils.newPlainAttrValue();
                attrValue.setStringValue(privilege.getKey());
                values.add(attrValue);
            });
        }
    }
    LOG.debug("Internal values: {}", values);
    List<PlainAttrValue> transformed = values;
    if (transform) {
        for (ItemTransformer transformer : MappingUtils.getItemTransformers(mapItem)) {
            transformed = transformer.beforePropagation(mapItem, any, transformed);
        }
        LOG.debug("Transformed values: {}", values);
    } else {
        LOG.debug("No transformation occurred");
    }
    return transformed;
}
Also used : Date(java.util.Date) Realm(org.apache.syncope.core.persistence.api.entity.Realm) PlainAttr(org.apache.syncope.core.persistence.api.entity.PlainAttr) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Schema(org.apache.syncope.core.persistence.api.entity.Schema) InvalidPasswordRuleConf(org.apache.syncope.core.provisioning.api.utils.policy.InvalidPasswordRuleConf) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(org.identityconnectors.framework.common.objects.Attribute) GroupDAO(org.apache.syncope.core.persistence.api.dao.GroupDAO) Pair(org.apache.commons.lang3.tuple.Pair) AttrSchemaType(org.apache.syncope.common.lib.types.AttrSchemaType) AnyObjectDAO(org.apache.syncope.core.persistence.api.dao.AnyObjectDAO) ConnObjectUtils(org.apache.syncope.core.provisioning.java.utils.ConnObjectUtils) GroupableRelatableTO(org.apache.syncope.common.lib.to.GroupableRelatableTO) OrgUnit(org.apache.syncope.core.persistence.api.entity.resource.OrgUnit) ParseException(java.text.ParseException) OperationalAttributes(org.identityconnectors.framework.common.objects.OperationalAttributes) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) OrgUnitItem(org.apache.syncope.core.persistence.api.entity.resource.OrgUnitItem) DerAttrHandler(org.apache.syncope.core.provisioning.api.DerAttrHandler) Set(java.util.Set) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) GroupTO(org.apache.syncope.common.lib.to.GroupTO) AnyTypeDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeDAO) MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) List(java.util.List) DerSchemaDAO(org.apache.syncope.core.persistence.api.dao.DerSchemaDAO) Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) AttributeUtil(org.identityconnectors.framework.common.objects.AttributeUtil) AttributeBuilder(org.identityconnectors.framework.common.objects.AttributeBuilder) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) Group(org.apache.syncope.core.persistence.api.entity.group.Group) Optional(java.util.Optional) IntAttrName(org.apache.syncope.core.provisioning.api.IntAttrName) VirAttrCache(org.apache.syncope.core.provisioning.api.cache.VirAttrCache) ApplicationDAO(org.apache.syncope.core.persistence.api.dao.ApplicationDAO) ItemTransformer(org.apache.syncope.core.provisioning.api.data.ItemTransformer) AttrTO(org.apache.syncope.common.lib.to.AttrTO) AnyUtilsFactory(org.apache.syncope.core.persistence.api.entity.AnyUtilsFactory) RealmTO(org.apache.syncope.common.lib.to.RealmTO) GroupableRelatable(org.apache.syncope.core.persistence.api.entity.GroupableRelatable) AnyTO(org.apache.syncope.common.lib.to.AnyTO) FrameworkUtil(org.identityconnectors.framework.common.FrameworkUtil) BooleanUtils(org.apache.commons.lang3.BooleanUtils) PasswordGenerator(org.apache.syncope.core.spring.security.PasswordGenerator) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Application(org.apache.syncope.core.persistence.api.entity.Application) DerSchema(org.apache.syncope.core.persistence.api.entity.DerSchema) MappingManager(org.apache.syncope.core.provisioning.api.MappingManager) FieldUtils(org.apache.commons.lang3.reflect.FieldUtils) Item(org.apache.syncope.core.persistence.api.entity.resource.Item) RealmDAO(org.apache.syncope.core.persistence.api.dao.RealmDAO) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) Mapping(org.apache.syncope.core.persistence.api.entity.resource.Mapping) Encryptor(org.apache.syncope.core.spring.security.Encryptor) Logger(org.slf4j.Logger) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) PlainSchemaDAO(org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO) VirAttrHandler(org.apache.syncope.core.provisioning.api.VirAttrHandler) User(org.apache.syncope.core.persistence.api.entity.user.User) Membership(org.apache.syncope.core.persistence.api.entity.Membership) Name(org.identityconnectors.framework.common.objects.Name) VirSchema(org.apache.syncope.core.persistence.api.entity.VirSchema) MappingUtils(org.apache.syncope.core.provisioning.java.utils.MappingUtils) Component(org.springframework.stereotype.Component) VirSchemaDAO(org.apache.syncope.core.persistence.api.dao.VirSchemaDAO) UserTO(org.apache.syncope.common.lib.to.UserTO) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils) Collections(java.util.Collections) Any(org.apache.syncope.core.persistence.api.entity.Any) DateFormatUtils(org.apache.commons.lang3.time.DateFormatUtils) AnyObjectTO(org.apache.syncope.common.lib.to.AnyObjectTO) ParsingValidationException(org.apache.syncope.core.persistence.api.attrvalue.validation.ParsingValidationException) Transactional(org.springframework.transaction.annotation.Transactional) Group(org.apache.syncope.core.persistence.api.entity.group.Group) User(org.apache.syncope.core.persistence.api.entity.user.User) PlainAttr(org.apache.syncope.core.persistence.api.entity.PlainAttr) ItemTransformer(org.apache.syncope.core.provisioning.api.data.ItemTransformer) ArrayList(java.util.ArrayList) Mapping(org.apache.syncope.core.persistence.api.entity.resource.Mapping) GroupableRelatable(org.apache.syncope.core.persistence.api.entity.GroupableRelatable) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) List(java.util.List) ArrayList(java.util.ArrayList) DerSchema(org.apache.syncope.core.persistence.api.entity.DerSchema) VirSchema(org.apache.syncope.core.persistence.api.entity.VirSchema) Date(java.util.Date) ParseException(java.text.ParseException) ParsingValidationException(org.apache.syncope.core.persistence.api.attrvalue.validation.ParsingValidationException) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils) Application(org.apache.syncope.core.persistence.api.entity.Application) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with Item

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

the class MappingManagerImpl method setIntValues.

@Transactional(readOnly = true)
@Override
public void setIntValues(final Item mapItem, final Attribute attr, final AnyTO anyTO, final AnyUtils anyUtils) {
    List<Object> values = null;
    if (attr != null) {
        values = attr.getValue();
        for (ItemTransformer transformer : MappingUtils.getItemTransformers(mapItem)) {
            values = transformer.beforePull(mapItem, anyTO, values);
        }
    }
    values = values == null ? Collections.emptyList() : values;
    IntAttrName intAttrName;
    try {
        intAttrName = intAttrNameParser.parse(mapItem.getIntAttrName(), anyUtils.getAnyTypeKind());
    } catch (ParseException e) {
        LOG.error("Invalid intAttrName '{}' specified, ignoring", mapItem.getIntAttrName(), e);
        return;
    }
    if (intAttrName.getField() != null) {
        switch(intAttrName.getField()) {
            case "password":
                if (anyTO instanceof UserTO && !values.isEmpty()) {
                    ((UserTO) anyTO).setPassword(ConnObjectUtils.getPassword(values.get(0)));
                }
                break;
            case "username":
                if (anyTO instanceof UserTO) {
                    ((UserTO) anyTO).setUsername(values.isEmpty() || values.get(0) == null ? null : values.get(0).toString());
                }
                break;
            case "name":
                if (anyTO instanceof GroupTO) {
                    ((GroupTO) anyTO).setName(values.isEmpty() || values.get(0) == null ? null : values.get(0).toString());
                } else if (anyTO instanceof AnyObjectTO) {
                    ((AnyObjectTO) anyTO).setName(values.isEmpty() || values.get(0) == null ? null : values.get(0).toString());
                }
                break;
            case "mustChangePassword":
                if (anyTO instanceof UserTO && !values.isEmpty() && values.get(0) != null) {
                    ((UserTO) anyTO).setMustChangePassword(BooleanUtils.toBoolean(values.get(0).toString()));
                }
                break;
            case "userOwner":
            case "groupOwner":
                if (anyTO instanceof GroupTO && attr != null) {
                    // using a special attribute (with schema "", that will be ignored) for carrying the
                    // GroupOwnerSchema value
                    AttrTO attrTO = new AttrTO();
                    attrTO.setSchema(StringUtils.EMPTY);
                    if (values.isEmpty() || values.get(0) == null) {
                        attrTO.getValues().add(StringUtils.EMPTY);
                    } else {
                        attrTO.getValues().add(values.get(0).toString());
                    }
                    ((GroupTO) anyTO).getPlainAttrs().add(attrTO);
                }
                break;
            default:
        }
    } else if (intAttrName.getSchemaType() != null) {
        GroupableRelatableTO groupableTO = null;
        Group group = null;
        if (anyTO instanceof GroupableRelatableTO && intAttrName.getMembershipOfGroup() != null) {
            groupableTO = (GroupableRelatableTO) anyTO;
            group = groupDAO.findByName(intAttrName.getMembershipOfGroup());
        }
        switch(intAttrName.getSchemaType()) {
            case PLAIN:
                AttrTO attrTO = new AttrTO();
                attrTO.setSchema(intAttrName.getSchemaName());
                PlainSchema schema = plainSchemaDAO.find(intAttrName.getSchemaName());
                for (Object value : values) {
                    AttrSchemaType schemaType = schema == null ? AttrSchemaType.String : schema.getType();
                    if (value != null) {
                        PlainAttrValue attrValue = anyUtils.newPlainAttrValue();
                        switch(schemaType) {
                            case String:
                                attrValue.setStringValue(value.toString());
                                break;
                            case Binary:
                                attrValue.setBinaryValue((byte[]) value);
                                break;
                            default:
                                try {
                                    attrValue.parseValue(schema, value.toString());
                                } catch (ParsingValidationException e) {
                                    LOG.error("While parsing provided value {}", value, e);
                                    attrValue.setStringValue(value.toString());
                                    schemaType = AttrSchemaType.String;
                                }
                                break;
                        }
                        attrTO.getValues().add(attrValue.getValueAsString(schemaType));
                    }
                }
                if (groupableTO == null || group == null) {
                    anyTO.getPlainAttrs().add(attrTO);
                } else {
                    Optional<MembershipTO> membership = groupableTO.getMembership(group.getKey());
                    if (!membership.isPresent()) {
                        membership = Optional.of(new MembershipTO.Builder().group(group.getKey(), group.getName()).build());
                        groupableTO.getMemberships().add(membership.get());
                    }
                    membership.get().getPlainAttrs().add(attrTO);
                }
                break;
            case DERIVED:
                attrTO = new AttrTO();
                attrTO.setSchema(intAttrName.getSchemaName());
                if (groupableTO == null || group == null) {
                    anyTO.getDerAttrs().add(attrTO);
                } else {
                    Optional<MembershipTO> membership = groupableTO.getMembership(group.getKey());
                    if (!membership.isPresent()) {
                        membership = Optional.of(new MembershipTO.Builder().group(group.getKey(), group.getName()).build());
                        groupableTO.getMemberships().add(membership.get());
                    }
                    membership.get().getDerAttrs().add(attrTO);
                }
                break;
            case VIRTUAL:
                attrTO = new AttrTO();
                attrTO.setSchema(intAttrName.getSchemaName());
                // virtual attributes don't get transformed, iterate over original attr.getValue()
                if (attr != null && attr.getValue() != null && !attr.getValue().isEmpty()) {
                    attr.getValue().stream().filter(value -> value != null).forEachOrdered(value -> attrTO.getValues().add(value.toString()));
                }
                if (groupableTO == null || group == null) {
                    anyTO.getVirAttrs().add(attrTO);
                } else {
                    Optional<MembershipTO> membership = groupableTO.getMembership(group.getKey());
                    if (!membership.isPresent()) {
                        membership = Optional.of(new MembershipTO.Builder().group(group.getKey(), group.getName()).build());
                        groupableTO.getMemberships().add(membership.get());
                    }
                    membership.get().getVirAttrs().add(attrTO);
                }
                break;
            default:
        }
    }
}
Also used : Date(java.util.Date) Realm(org.apache.syncope.core.persistence.api.entity.Realm) PlainAttr(org.apache.syncope.core.persistence.api.entity.PlainAttr) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Schema(org.apache.syncope.core.persistence.api.entity.Schema) InvalidPasswordRuleConf(org.apache.syncope.core.provisioning.api.utils.policy.InvalidPasswordRuleConf) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(org.identityconnectors.framework.common.objects.Attribute) GroupDAO(org.apache.syncope.core.persistence.api.dao.GroupDAO) Pair(org.apache.commons.lang3.tuple.Pair) AttrSchemaType(org.apache.syncope.common.lib.types.AttrSchemaType) AnyObjectDAO(org.apache.syncope.core.persistence.api.dao.AnyObjectDAO) ConnObjectUtils(org.apache.syncope.core.provisioning.java.utils.ConnObjectUtils) GroupableRelatableTO(org.apache.syncope.common.lib.to.GroupableRelatableTO) OrgUnit(org.apache.syncope.core.persistence.api.entity.resource.OrgUnit) ParseException(java.text.ParseException) OperationalAttributes(org.identityconnectors.framework.common.objects.OperationalAttributes) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) OrgUnitItem(org.apache.syncope.core.persistence.api.entity.resource.OrgUnitItem) DerAttrHandler(org.apache.syncope.core.provisioning.api.DerAttrHandler) Set(java.util.Set) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) GroupTO(org.apache.syncope.common.lib.to.GroupTO) AnyTypeDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeDAO) MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) List(java.util.List) DerSchemaDAO(org.apache.syncope.core.persistence.api.dao.DerSchemaDAO) Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) AttributeUtil(org.identityconnectors.framework.common.objects.AttributeUtil) AttributeBuilder(org.identityconnectors.framework.common.objects.AttributeBuilder) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) Group(org.apache.syncope.core.persistence.api.entity.group.Group) Optional(java.util.Optional) IntAttrName(org.apache.syncope.core.provisioning.api.IntAttrName) VirAttrCache(org.apache.syncope.core.provisioning.api.cache.VirAttrCache) ApplicationDAO(org.apache.syncope.core.persistence.api.dao.ApplicationDAO) ItemTransformer(org.apache.syncope.core.provisioning.api.data.ItemTransformer) AttrTO(org.apache.syncope.common.lib.to.AttrTO) AnyUtilsFactory(org.apache.syncope.core.persistence.api.entity.AnyUtilsFactory) RealmTO(org.apache.syncope.common.lib.to.RealmTO) GroupableRelatable(org.apache.syncope.core.persistence.api.entity.GroupableRelatable) AnyTO(org.apache.syncope.common.lib.to.AnyTO) FrameworkUtil(org.identityconnectors.framework.common.FrameworkUtil) BooleanUtils(org.apache.commons.lang3.BooleanUtils) PasswordGenerator(org.apache.syncope.core.spring.security.PasswordGenerator) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Application(org.apache.syncope.core.persistence.api.entity.Application) DerSchema(org.apache.syncope.core.persistence.api.entity.DerSchema) MappingManager(org.apache.syncope.core.provisioning.api.MappingManager) FieldUtils(org.apache.commons.lang3.reflect.FieldUtils) Item(org.apache.syncope.core.persistence.api.entity.resource.Item) RealmDAO(org.apache.syncope.core.persistence.api.dao.RealmDAO) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) Mapping(org.apache.syncope.core.persistence.api.entity.resource.Mapping) Encryptor(org.apache.syncope.core.spring.security.Encryptor) Logger(org.slf4j.Logger) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) PlainSchemaDAO(org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO) VirAttrHandler(org.apache.syncope.core.provisioning.api.VirAttrHandler) User(org.apache.syncope.core.persistence.api.entity.user.User) Membership(org.apache.syncope.core.persistence.api.entity.Membership) Name(org.identityconnectors.framework.common.objects.Name) VirSchema(org.apache.syncope.core.persistence.api.entity.VirSchema) MappingUtils(org.apache.syncope.core.provisioning.java.utils.MappingUtils) Component(org.springframework.stereotype.Component) VirSchemaDAO(org.apache.syncope.core.persistence.api.dao.VirSchemaDAO) UserTO(org.apache.syncope.common.lib.to.UserTO) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils) Collections(java.util.Collections) Any(org.apache.syncope.core.persistence.api.entity.Any) DateFormatUtils(org.apache.commons.lang3.time.DateFormatUtils) AnyObjectTO(org.apache.syncope.common.lib.to.AnyObjectTO) ParsingValidationException(org.apache.syncope.core.persistence.api.attrvalue.validation.ParsingValidationException) Transactional(org.springframework.transaction.annotation.Transactional) Group(org.apache.syncope.core.persistence.api.entity.group.Group) Optional(java.util.Optional) ItemTransformer(org.apache.syncope.core.provisioning.api.data.ItemTransformer) AttrTO(org.apache.syncope.common.lib.to.AttrTO) IntAttrName(org.apache.syncope.core.provisioning.api.IntAttrName) GroupTO(org.apache.syncope.common.lib.to.GroupTO) ParsingValidationException(org.apache.syncope.core.persistence.api.attrvalue.validation.ParsingValidationException) AnyObjectTO(org.apache.syncope.common.lib.to.AnyObjectTO) GroupableRelatableTO(org.apache.syncope.common.lib.to.GroupableRelatableTO) UserTO(org.apache.syncope.common.lib.to.UserTO) AttrSchemaType(org.apache.syncope.common.lib.types.AttrSchemaType) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) ParseException(java.text.ParseException) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) 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