Package org.apache.syncope.core.propagation

Examples of org.apache.syncope.core.propagation.PropagationByResource


        updateStatus(user);
        SyncopeUser updated = userDAO.save(user);

        // see if there is any propagation to be done
        PropagationByResource propByRes =
                (PropagationByResource) runtimeService.getVariable(user.getWorkflowId(), PROP_BY_RESOURCE);

        // fetch - if available - the encrpted password
        String clearPassword = null;
        String encryptedPwd = (String) runtimeService.getVariable(user.getWorkflowId(), ENCRYPTED_PWD);
View Full Code Here


            result.setStatus(SyncResult.Status.FAILURE);
            result.setMessage("Update failed, trying to sync status anyway (if configured)\n" + e.getMessage());

            updated = new WorkflowResult<Map.Entry<Long, Boolean>>(
                    new AbstractMap.SimpleEntry<Long, Boolean>(id, false), new PropagationByResource(),
                    new HashSet<String>());
        }

        Boolean enabled = readEnabled(delta.getObject());
        if (enabled != null) {
View Full Code Here

            tasks = propagationManager.getUserUpdateTaskIds(updated, changedPwd,
                    actual.getVirtualAttributesToBeRemoved(), actual.getVirtualAttributesToBeUpdated());
        } else {
            // 2b. generate the propagation task list in two phases: first the ones containing password,
            // the the rest (with no password)
            final PropagationByResource origPropByRes = new PropagationByResource();
            origPropByRes.merge(updated.getPropByRes());

            Set<String> pwdResourceNames = actual.getPwdPropRequest().getResources();
            SyncopeUser user = binder.getUserFromId(updated.getResult().getKey());
            pwdResourceNames.retainAll(user.getResourceNames());
            final PropagationByResource pwdPropByRes = new PropagationByResource();
            pwdPropByRes.addAll(ResourceOperation.UPDATE, pwdResourceNames);
            updated.setPropByRes(pwdPropByRes);

            if (!pwdPropByRes.isEmpty()) {
                Set<String> toBeExcluded = new HashSet<String>(user.getResourceNames());
                toBeExcluded.addAll(actual.getResourcesToBeAdded());
                toBeExcluded.removeAll(pwdResourceNames);
                tasks.addAll(propagationManager.getUserUpdateTaskIds(
                        updated,
                        changedPwd,
                        actual.getVirtualAttributesToBeRemoved(),
                        actual.getVirtualAttributesToBeUpdated(),
                        toBeExcluded));
            }

            final PropagationByResource nonPwdPropByRes = new PropagationByResource();
            nonPwdPropByRes.merge(origPropByRes);
            nonPwdPropByRes.removeAll(pwdResourceNames);
            nonPwdPropByRes.purge();
            updated.setPropByRes(nonPwdPropByRes);

            if (!nonPwdPropByRes.isEmpty()) {
                tasks.addAll(propagationManager.getUserUpdateTaskIds(
                        updated,
                        null,
                        actual.getVirtualAttributesToBeRemoved(),
                        actual.getVirtualAttributesToBeUpdated(),
View Full Code Here

    protected void doExecute(final DelegateExecution execution) throws Exception {
        SyncopeUser user = (SyncopeUser) execution.getVariable(ActivitiUserWorkflowAdapter.SYNCOPE_USER);
        UserMod userMod = (UserMod) execution.getVariable(ActivitiUserWorkflowAdapter.USER_MOD);

        // update SyncopeUser
        PropagationByResource propByRes = dataBinder.update(user, userMod);

        // report updated user and propagation by resource as result
        execution.setVariable(ActivitiUserWorkflowAdapter.SYNCOPE_USER, user);
        execution.setVariable(ActivitiUserWorkflowAdapter.PROP_BY_RESOURCE, propByRes);
    }
View Full Code Here

    public PropagationByResource fillVirtual(final AbstractAttributable attributable,
            final Set<String> vAttrsToBeRemoved, final Set<AttributeMod> vAttrsToBeUpdated,
            final AttributableUtil attributableUtil) {

        PropagationByResource propByRes = new PropagationByResource();

        // 1. virtual attributes to be removed
        for (String vAttrToBeRemoved : vAttrsToBeRemoved) {
            AbstractVirSchema virtualSchema = getVirtualSchema(vAttrToBeRemoved, attributableUtil.virtualSchemaClass());

            if (virtualSchema != null) {
                AbstractVirAttr virtualAttribute = attributable.getVirtualAttribute(virtualSchema.getName());

                if (virtualAttribute == null) {
                    LOG.debug("No virtual attribute found for schema {}", virtualSchema.getName());
                } else {
                    attributable.removeVirtualAttribute(virtualAttribute);
                    virAttrDAO.delete(virtualAttribute);
                }

                for (SchemaMapping mapping : resourceDAO.findAllMappings()) {
                    if (virtualSchema.getName().equals(mapping.getIntAttrName())
                            && mapping.getIntMappingType() == attributableUtil.virtualIntMappingType()
                            && mapping.getResource() != null
                            && attributable.getResources().contains(mapping.getResource())) {

                        propByRes.add(PropagationOperation.UPDATE, mapping.getResource().getName());

                        // TODO: must be avoided to use virtual attribute as AccountId
                        if (mapping.isAccountid() && virtualAttribute != null
                                && !virtualAttribute.getValues().isEmpty()) {

                            propByRes.addOldAccountId(mapping.getResource().getName(), virtualAttribute.getValues().get(
                                    0));
                        }
                    }
                }
            }
        }

        LOG.debug("Virtual attributes to be removed:\n{}", propByRes);

        // 2. virtual attributes to be updated
        for (AttributeMod vAttrToBeUpdated : vAttrsToBeUpdated) {
            AbstractVirSchema virtualSchema = getVirtualSchema(vAttrToBeUpdated.getSchema(), attributableUtil.
                    virtualSchemaClass());

            if (virtualSchema != null) {
                for (SchemaMapping mapping : resourceDAO.findAllMappings()) {
                    if (virtualSchema.getName().equals(mapping.getIntAttrName())
                            && mapping.getIntMappingType() == attributableUtil.virtualIntMappingType()
                            && mapping.getResource() != null
                            && attributable.getResources().contains(mapping.getResource())) {

                        propByRes.add(PropagationOperation.UPDATE, mapping.getResource().getName());
                    }
                }

                AbstractVirAttr virtualAttribute = attributable.getVirtualAttribute(virtualSchema.getName());
View Full Code Here

    protected PropagationByResource fill(final AbstractAttributable attributable,
            final AbstractAttributableMod attributableMod, final AttributableUtil attributableUtil,
            final SyncopeClientCompositeErrorException compositeErrorException)
            throws SyncopeClientCompositeErrorException {

        PropagationByResource propByRes = new PropagationByResource();

        SyncopeClientException invalidValues = new SyncopeClientException(SyncopeClientExceptionType.InvalidValues);

        // 1. resources to be removed
        ExternalResource resource;
        for (String resourceToBeRemoved : attributableMod.getResourcesToBeRemoved()) {

            resource = getResource(resourceToBeRemoved);

            if (resource != null) {
                propByRes.add(PropagationOperation.DELETE, resource.getName());
                attributable.removeResource(resource);
            }
        }

        LOG.debug("Resources to be removed:\n{}", propByRes);

        // 2. resources to be added
        for (String resourceToBeAdded : attributableMod.getResourcesToBeAdded()) {

            resource = getResource(resourceToBeAdded);

            if (resource != null) {
                propByRes.add(PropagationOperation.CREATE, resource.getName());
                attributable.addResource(resource);
            }
        }

        LOG.debug("Resources to be added:\n{}", propByRes);

        AbstractSchema schema;
        AbstractAttr attribute;
        AbstractDerSchema derivedSchema;
        AbstractDerAttr derivedAttribute;

        // 3. attributes to be removed
        for (String attributeToBeRemoved : attributableMod.getAttributesToBeRemoved()) {

            schema = getSchema(attributeToBeRemoved, attributableUtil.schemaClass());

            if (schema != null) {
                attribute = attributable.getAttribute(schema.getName());

                if (attribute == null) {
                    LOG.debug("No attribute found for schema {}", schema);
                } else {
                    String newValue = null;
                    for (AttributeMod mod : attributableMod.getAttributesToBeUpdated()) {
                        if (schema.getName().equals(mod.getSchema())) {
                            newValue = mod.getValuesToBeAdded().get(0);
                        }
                    }

                    if (!schema.isUniqueConstraint() || (!attribute.getUniqueValue().getStringValue().equals(newValue))) {

                        attributable.removeAttribute(attribute);
                        attributeDAO.delete(attribute.getId(), attributableUtil.attributeClass());
                    }
                }

                for (SchemaMapping mapping : resourceDAO.findAllMappings()) {
                    if (schema.getName().equals(mapping.getIntAttrName())
                            && mapping.getIntMappingType() == attributableUtil.intMappingType()
                            && mapping.getResource() != null
                            && attributable.getResources().contains(mapping.getResource())) {

                        propByRes.add(PropagationOperation.UPDATE, mapping.getResource().getName());

                        if (mapping.isAccountid() && attribute != null && !attribute.getValuesAsStrings().isEmpty()) {

                            propByRes.addOldAccountId(mapping.getResource().getName(), attribute.getValuesAsStrings().
                                    iterator().next());
                        }
                    }
                }
            }
        }

        LOG.debug("Attributes to be removed:\n{}", propByRes);

        // 4. attributes to be updated
        Set<Long> valuesToBeRemoved;
        List<String> valuesToBeAdded;
        for (AttributeMod attributeMod : attributableMod.getAttributesToBeUpdated()) {

            schema = getSchema(attributeMod.getSchema(), attributableUtil.schemaClass());

            if (schema != null) {
                for (SchemaMapping mapping : resourceDAO.findAllMappings()) {
                    if (schema.getName().equals(mapping.getIntAttrName())
                            && mapping.getIntMappingType() == attributableUtil.intMappingType()
                            && mapping.getResource() != null
                            && attributable.getResources().contains(mapping.getResource())) {

                        propByRes.add(PropagationOperation.UPDATE, mapping.getResource().getName());
                    }
                }

                attribute = attributable.getAttribute(schema.getName());
                if (attribute == null) {
                    attribute = attributableUtil.newAttribute();
                    attribute.setSchema(schema);
                    attribute.setOwner(attributable);

                    attributable.addAttribute(attribute);
                }

                // 1.1 remove values
                valuesToBeRemoved = new HashSet<Long>();
                for (String valueToBeRemoved : attributeMod.getValuesToBeRemoved()) {

                    if (attribute.getSchema().isUniqueConstraint()) {
                        if (attribute.getUniqueValue() != null
                                && valueToBeRemoved.equals(attribute.getUniqueValue().getValueAsString())) {

                            valuesToBeRemoved.add(attribute.getUniqueValue().getId());
                        }
                    } else {
                        for (AbstractAttrValue mav : attribute.getValues()) {
                            if (valueToBeRemoved.equals(mav.getValueAsString())) {
                                valuesToBeRemoved.add(mav.getId());
                            }
                        }
                    }
                }
                for (Long attributeValueId : valuesToBeRemoved) {
                    attributeValueDAO.delete(attributeValueId, attributableUtil.attributeValueClass());
                }

                // 1.2 add values
                valuesToBeAdded = attributeMod.getValuesToBeAdded();
                if (valuesToBeAdded != null
                        && !valuesToBeAdded.isEmpty()
                        && (!schema.isUniqueConstraint() || attribute.getUniqueValue() == null || !valuesToBeAdded.
                        iterator().next().equals(attribute.getUniqueValue().getValueAsString()))) {

                    fillAttribute(attributeMod.getValuesToBeAdded(), attributableUtil, schema, attribute, invalidValues);
                }

                // if no values are in, the attribute can be safely removed
                if (attribute.getValuesAsStrings().isEmpty()) {
                    attributeDAO.delete(attribute);
                }
            }
        }

        if (!invalidValues.isEmpty()) {
            compositeErrorException.addException(invalidValues);
        }

        LOG.debug("Attributes to be updated:\n{}", propByRes);

        // 5. derived attributes to be removed
        for (String derivedAttributeToBeRemoved : attributableMod.getDerivedAttributesToBeRemoved()) {

            derivedSchema = getDerivedSchema(derivedAttributeToBeRemoved, attributableUtil.derivedSchemaClass());

            if (derivedSchema != null) {
                derivedAttribute = attributable.getDerivedAttribute(derivedSchema.getName());

                if (derivedAttribute == null) {
                    LOG.debug("No derived attribute found for schema {}", derivedSchema.getName());
                } else {
                    derAttrDAO.delete(derivedAttribute);
                }

                for (SchemaMapping mapping : resourceDAO.findAllMappings()) {
                    if (derivedSchema.getName().equals(mapping.getIntAttrName())
                            && mapping.getIntMappingType() == attributableUtil.derivedIntMappingType()
                            && mapping.getResource() != null
                            && attributable.getResources().contains(mapping.getResource())) {

                        propByRes.add(PropagationOperation.UPDATE, mapping.getResource().getName());

                        if (mapping.isAccountid() && derivedAttribute != null
                                && !derivedAttribute.getValue(attributable.getAttributes()).isEmpty()) {

                            propByRes.addOldAccountId(mapping.getResource().getName(),
                                    derivedAttribute.getValue(attributable.getAttributes()));
                        }
                    }
                }
            }
        }

        LOG.debug("Derived attributes to be removed:\n{}", propByRes);

        // 6. derived attributes to be added
        for (String derivedAttributeToBeAdded : attributableMod.getDerivedAttributesToBeAdded()) {

            derivedSchema = getDerivedSchema(derivedAttributeToBeAdded, attributableUtil.derivedSchemaClass());

            if (derivedSchema != null) {
                for (SchemaMapping mapping : resourceDAO.findAllMappings()) {
                    if (derivedSchema.getName().equals(mapping.getIntAttrName())
                            && mapping.getIntMappingType() == attributableUtil.derivedIntMappingType()
                            && mapping.getResource() != null
                            && attributable.getResources().contains(mapping.getResource())) {

                        propByRes.add(PropagationOperation.UPDATE, mapping.getResource().getName());
                    }
                }

                derivedAttribute = attributableUtil.newDerivedAttribute();
                derivedAttribute.setDerivedSchema(derivedSchema);
View Full Code Here

        if (propagateEnable == null) {
            propagateEnable = enabled;
        }

        // save resources to be propagated and password for later - after form submission - propagation
        PropagationByResource propByRes = new PropagationByResource();
        propByRes.set(PropagationOperation.CREATE, user.getResourceNames());

        String formTaskId = getFormTask(user);
        if (formTaskId != null) {
            // SYNCOPE-238: This is needed to simplify the task query in this.getForms()
            taskService.setVariableLocal(formTaskId, TASK_IS_FORM, Boolean.TRUE);
View Full Code Here

        Set<String> task = doExecuteTask(user, "update", Collections.singletonMap(USER_MOD, (Object) userMod));

        updateStatus(user);
        SyncopeUser updated = userDAO.save(user);

        PropagationByResource propByRes = (PropagationByResource) runtimeService.getVariable(user.getWorkflowId(),
                PROP_BY_RESOURCE);

        // save resources to be propagated and password for later - after form submission - propagation
        String formTaskId = getFormTask(user);
        if (formTaskId != null) {
View Full Code Here

        updateStatus(user);
        SyncopeUser updated = userDAO.save(user);

        // see if there is any propagation to be done
        PropagationByResource propByRes = (PropagationByResource) runtimeService.getVariable(user.getWorkflowId(),
                PROP_BY_RESOURCE);

        // fetch - if available - the encrpted password
        String clearPassword = null;
        String encryptedPwd = (String) runtimeService.getVariable(user.getWorkflowId(), ENCRYPTED_PWD);
View Full Code Here

     * @see PropagationByResource
     */
    public PropagationByResource update(final SyncopeUser user, final UserMod userMod)
            throws SyncopeClientCompositeErrorException {

        PropagationByResource propByRes = new PropagationByResource();

        SyncopeClientCompositeErrorException scce = new SyncopeClientCompositeErrorException(HttpStatus.BAD_REQUEST);

        // when requesting to add user to new resources, either directly or
        // through role subscription, password is mandatory (issue 147)
        // first, let's take current resources into account
        Set<String> currentResources = user.getResourceNames();

        // password
        if (userMod.getPassword() != null) {
            int passwordHistorySize = 0;
            try {
                Policy policy = policyDAO.getGlobalPasswordPolicy();
                PasswordPolicySpec passwordPolicy = policy.getSpecification();
                passwordHistorySize = passwordPolicy.getHistoryLength();
            } catch (Exception ignore) {
                // ignore exceptions
            }

            user.setPassword(userMod.getPassword(), getCipherAlgoritm(), passwordHistorySize);

            user.setChangePwdDate(new Date());

            propByRes.addAll(PropagationOperation.UPDATE, currentResources);
        }

        // username
        if (userMod.getUsername() != null && !userMod.getUsername().equals(user.getUsername())) {

            String oldUsername = user.getUsername();

            user.setUsername(userMod.getUsername());
            propByRes.addAll(PropagationOperation.UPDATE, currentResources);

            for (ExternalResource resource : user.getResources()) {
                for (SchemaMapping mapping : resource.getMappings()) {
                    if (mapping.isAccountid() && mapping.getIntMappingType() == IntMappingType.Username) {
                        propByRes.addOldAccountId(resource.getName(), oldUsername);
                    }
                }
            }
        }

        // attributes, derived attributes, virtual attributes and resources
        propByRes.merge(fill(user, userMod, AttributableUtil.getInstance(AttributableType.USER), scce));

        // store the role ids of membership required to be added
        Set<Long> membershipToBeAddedRoleIds = new HashSet<Long>();
        for (MembershipMod membToBeAdded : userMod.getMembershipsToBeAdded()) {
            membershipToBeAddedRoleIds.add(membToBeAdded.getRole());
        }

        final Set<String> toBeDeprovisioned = new HashSet<String>();
        final Set<String> toBeProvisioned = new HashSet<String>();

        // memberships to be removed
        Membership membership = null;
        for (Long membershipId : userMod.getMembershipsToBeRemoved()) {
            LOG.debug("Membership to be removed: {}", membershipId);

            membership = membershipDAO.find(membershipId);
            if (membership == null) {
                LOG.debug("Invalid membership id specified to be removed: {}", membershipId);
            } else {

                if (!membershipToBeAddedRoleIds.contains(membership.getSyncopeRole().getId())) {
                    toBeDeprovisioned.addAll(membership.getSyncopeRole().getResourceNames());
                }

                // In order to make the removeMembership() below to work,
                // we need to be sure to take exactly the same membership
                // of the user object currently in memory (which has potentially
                // some modifications compared to the one stored in the DB
                membership = user.getMembership(membership.getSyncopeRole().getId());
                if (membershipToBeAddedRoleIds.contains(membership.getSyncopeRole().getId())) {

                    Set<Long> attributeIds = new HashSet<Long>(membership.getAttributes().size());
                    for (AbstractAttr attribute : membership.getAttributes()) {
                        attributeIds.add(attribute.getId());
                    }
                    for (Long attributeId : attributeIds) {
                        attributeDAO.delete(attributeId, MAttr.class);
                    }
                    attributeIds.clear();

                    // remove derived attributes
                    for (AbstractDerAttr derAttr : membership.getDerivedAttributes()) {

                        attributeIds.add(derAttr.getId());
                    }
                    for (Long derAttrId : attributeIds) {
                        derAttrDAO.delete(derAttrId, MDerAttr.class);
                    }
                    attributeIds.clear();

                    // remove virtual attributes
                    for (AbstractVirAttr virAttr : membership.getVirtualAttributes()) {

                        attributeIds.add(virAttr.getId());
                    }
                    for (Long virAttrId : attributeIds) {
                        virAttrDAO.delete(virAttrId, MVirAttr.class);
                    }
                    attributeIds.clear();
                } else {
                    user.removeMembership(membership);

                    membershipDAO.delete(membershipId);
                }
            }
        }

        // memberships to be added
        for (MembershipMod membershipMod : userMod.getMembershipsToBeAdded()) {
            LOG.debug("Membership to be added: role({})", membershipMod.getRole());

            SyncopeRole role = roleDAO.find(membershipMod.getRole());
            if (role == null) {
                LOG.debug("Ignoring invalid role {}", membershipMod.getRole());
            } else {
                membership = user.getMembership(role.getId());
                if (membership == null) {
                    membership = new Membership();
                    membership.setSyncopeRole(role);
                    membership.setSyncopeUser(user);

                    user.addMembership(membership);

                    toBeProvisioned.addAll(role.getResourceNames());
                }

                propByRes.merge(fill(membership, membershipMod,
                        AttributableUtil.getInstance(AttributableType.MEMBERSHIP), scce));
            }
        }

        // now, let's see if there are new resource subscriptions without providing password
        if (StringUtils.isBlank(userMod.getPassword())) {
            Set<String> updatedResources = user.getResourceNames();
            updatedResources.removeAll(currentResources);

            for (String res : updatedResources) {
                final ExternalResource extRes = resourceDAO.find(res);

                if (extRes != null && !SchemaMappingUtil.getMappings(
                        extRes.getMappings(), "password", IntMappingType.Password).isEmpty()) {

                    SyncopeClientException sce =
                            new SyncopeClientException(SyncopeClientExceptionType.RequiredValuesMissing);
                    sce.addElement("password cannot be empty " + "when subscribing to new resources");
                    scce.addException(sce);

                    throw scce;
                }
            }
        }

        propByRes.addAll(PropagationOperation.DELETE, toBeDeprovisioned);
        propByRes.addAll(PropagationOperation.UPDATE, toBeProvisioned);

        /**
         * In case of new memberships all the current resources have to be updated in order to propagate new role and
         * membership attribute values.
         */
        if (!toBeDeprovisioned.isEmpty() || !toBeProvisioned.isEmpty()) {
            currentResources.removeAll(toBeDeprovisioned);
            propByRes.addAll(PropagationOperation.UPDATE, currentResources);
        }

        return propByRes;
    }
View Full Code Here

TOP

Related Classes of org.apache.syncope.core.propagation.PropagationByResource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.