Search in sources :

Example 1 with ExternalResource

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

the class JPAExternalResourceDAO method delete.

@Override
public void delete(final String name) {
    ExternalResource resource = find(name);
    if (resource == null) {
        return;
    }
    taskDAO().deleteAll(resource, TaskType.PROPAGATION);
    taskDAO().deleteAll(resource, TaskType.PULL);
    taskDAO().deleteAll(resource, TaskType.PUSH);
    realmDAO().findByResource(resource).forEach(realm -> realm.getResources().remove(resource));
    anyObjectDAO().findByResource(resource).forEach(anyObject -> anyObject.getResources().remove(resource));
    userDAO().findByResource(resource).forEach(user -> user.getResources().remove(resource));
    groupDAO().findByResource(resource).forEach(group -> group.getResources().remove(resource));
    policyDAO().findByResource(resource).forEach(policy -> policy.getResources().remove(resource));
    resource.getProvisions().stream().filter(provision -> provision.getMapping() != null).peek(provision -> provision.getMapping().getItems().forEach(item -> item.setMapping(null))).peek(provision -> {
        provision.getMapping().getItems().clear();
        provision.setMapping(null);
        provision.setResource(null);
    }).forEachOrdered(provision -> virSchemaDAO().findByProvision(provision).forEach(schema -> virSchemaDAO().delete(schema.getKey())));
    externalResourceHistoryConfDAO().deleteByEntity(resource);
    if (resource.getConnector() != null && resource.getConnector().getResources() != null && !resource.getConnector().getResources().isEmpty()) {
        resource.getConnector().getResources().remove(resource);
    }
    resource.setConnector(null);
    entityManager().remove(resource);
}
Also used : JPAMappingItem(org.apache.syncope.core.persistence.jpa.entity.resource.JPAMappingItem) JPAExternalResource(org.apache.syncope.core.persistence.jpa.entity.resource.JPAExternalResource) StandardEntitlement(org.apache.syncope.common.lib.types.StandardEntitlement) PullPolicy(org.apache.syncope.core.persistence.api.entity.policy.PullPolicy) ConnectorRegistry(org.apache.syncope.core.provisioning.api.ConnectorRegistry) Autowired(org.springframework.beans.factory.annotation.Autowired) TypedQuery(javax.persistence.TypedQuery) TaskDAO(org.apache.syncope.core.persistence.api.dao.TaskDAO) JPAProvision(org.apache.syncope.core.persistence.jpa.entity.resource.JPAProvision) HashSet(java.util.HashSet) PasswordPolicy(org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy) GroupDAO(org.apache.syncope.core.persistence.api.dao.GroupDAO) DelegatedAdministrationException(org.apache.syncope.core.spring.security.DelegatedAdministrationException) AnyObjectDAO(org.apache.syncope.core.persistence.api.dao.AnyObjectDAO) RealmDAO(org.apache.syncope.core.persistence.api.dao.RealmDAO) AuthContextUtils(org.apache.syncope.core.spring.security.AuthContextUtils) Repository(org.springframework.stereotype.Repository) AnyTypeClass(org.apache.syncope.core.persistence.api.entity.AnyTypeClass) JPAMapping(org.apache.syncope.core.persistence.jpa.entity.resource.JPAMapping) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) Policy(org.apache.syncope.core.persistence.api.entity.policy.Policy) Set(java.util.Set) Implementation(org.apache.syncope.core.persistence.api.entity.Implementation) Collectors(java.util.stream.Collectors) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) List(java.util.List) Query(javax.persistence.Query) AccountPolicy(org.apache.syncope.core.persistence.api.entity.policy.AccountPolicy) Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) PolicyDAO(org.apache.syncope.core.persistence.api.dao.PolicyDAO) VirSchemaDAO(org.apache.syncope.core.persistence.api.dao.VirSchemaDAO) ApplicationContextProvider(org.apache.syncope.core.spring.ApplicationContextProvider) Collections(java.util.Collections) ExternalResourceDAO(org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO) ExternalResourceHistoryConfDAO(org.apache.syncope.core.persistence.api.dao.ExternalResourceHistoryConfDAO) TaskType(org.apache.syncope.common.lib.types.TaskType) Transactional(org.springframework.transaction.annotation.Transactional) JPAExternalResource(org.apache.syncope.core.persistence.jpa.entity.resource.JPAExternalResource) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource)

Example 2 with ExternalResource

use of org.apache.syncope.core.persistence.api.entity.resource.ExternalResource 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 3 with ExternalResource

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

the class PropagationManagerImpl method createTasks.

@Override
public List<PropagationTaskTO> createTasks(final Realm realm, final PropagationByResource propByRes, final Collection<String> noPropResourceKeys) {
    if (noPropResourceKeys != null) {
        propByRes.removeAll(noPropResourceKeys);
    }
    LOG.debug("Provisioning {}:\n{}", realm, propByRes);
    // Avoid duplicates - see javadoc
    propByRes.purge();
    LOG.debug("After purge {}:\n{}", realm, propByRes);
    List<PropagationTaskTO> tasks = new ArrayList<>();
    propByRes.asMap().forEach((resourceKey, operation) -> {
        ExternalResource resource = resourceDAO.find(resourceKey);
        OrgUnit orgUnit = resource == null ? null : resource.getOrgUnit();
        if (resource == null) {
            LOG.error("Invalid resource name specified: {}, ignoring...", resourceKey);
        } else if (orgUnit == null) {
            LOG.error("No orgUnit specified on resource {}, ignoring...", resource);
        } else if (StringUtils.isBlank(orgUnit.getConnObjectLink())) {
            LOG.warn("Requesting propagation for {} but no ConnObjectLink provided for {}", realm.getFullPath(), resource);
        } else {
            PropagationTaskTO task = new PropagationTaskTO();
            task.setResource(resource.getKey());
            task.setObjectClassName(orgUnit.getObjectClass().getObjectClassValue());
            task.setEntityKey(realm.getKey());
            task.setOperation(operation);
            task.setOldConnObjectKey(propByRes.getOldConnObjectKey(resource.getKey()));
            Pair<String, Set<Attribute>> preparedAttrs = mappingManager.prepareAttrs(realm, orgUnit);
            task.setConnObjectKey(preparedAttrs.getKey());
            task.setAttributes(POJOHelper.serialize(preparedAttrs.getValue()));
            tasks.add(task);
            LOG.debug("PropagationTask created: {}", task);
        }
    });
    return tasks;
}
Also used : OrgUnit(org.apache.syncope.core.persistence.api.entity.resource.OrgUnit) HashSet(java.util.HashSet) Set(java.util.Set) PropagationTaskTO(org.apache.syncope.common.lib.to.PropagationTaskTO) ArrayList(java.util.ArrayList) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource)

Example 4 with ExternalResource

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

the class ResourceDataBinderTest method issue42.

@Test
public void issue42() {
    PlainSchema userId = plainSchemaDAO.find("userId");
    Set<MappingItem> beforeUserIdMappings = new HashSet<>();
    for (ExternalResource res : resourceDAO.findAll()) {
        if (res.getProvision(anyTypeDAO.findUser()).isPresent() && res.getProvision(anyTypeDAO.findUser()).get().getMapping() != null) {
            for (MappingItem mapItem : res.getProvision(anyTypeDAO.findUser()).get().getMapping().getItems()) {
                if (userId.getKey().equals(mapItem.getIntAttrName())) {
                    beforeUserIdMappings.add(mapItem);
                }
            }
        }
    }
    ResourceTO resourceTO = new ResourceTO();
    resourceTO.setKey("resource-issue42");
    resourceTO.setConnector("88a7a819-dab5-46b4-9b90-0b9769eabdb8");
    resourceTO.setEnforceMandatoryCondition(true);
    ProvisionTO provisionTO = new ProvisionTO();
    provisionTO.setAnyType(AnyTypeKind.USER.name());
    provisionTO.setObjectClass(ObjectClass.ACCOUNT_NAME);
    resourceTO.getProvisions().add(provisionTO);
    MappingTO mapping = new MappingTO();
    provisionTO.setMapping(mapping);
    ItemTO item = new ItemTO();
    item.setIntAttrName("userId");
    item.setExtAttrName("campo1");
    item.setConnObjectKey(true);
    item.setMandatoryCondition("false");
    item.setPurpose(MappingPurpose.BOTH);
    mapping.setConnObjectKeyItem(item);
    ExternalResource resource = resourceDataBinder.create(resourceTO);
    resource = resourceDAO.save(resource);
    assertNotNull(resource);
    assertNotNull(resource.getProvision(anyTypeDAO.findUser()).get().getMapping());
    assertEquals(1, resource.getProvision(anyTypeDAO.findUser()).get().getMapping().getItems().size());
    resourceDAO.flush();
    ExternalResource actual = resourceDAO.find("resource-issue42");
    assertNotNull(actual);
    assertEquals(resource, actual);
    userId = plainSchemaDAO.find("userId");
    Set<MappingItem> afterUserIdMappings = new HashSet<>();
    for (ExternalResource res : resourceDAO.findAll()) {
        if (res.getProvision(anyTypeDAO.findUser()).isPresent() && res.getProvision(anyTypeDAO.findUser()).get().getMapping() != null) {
            for (MappingItem mapItem : res.getProvision(anyTypeDAO.findUser()).get().getMapping().getItems()) {
                if (userId.getKey().equals(mapItem.getIntAttrName())) {
                    afterUserIdMappings.add(mapItem);
                }
            }
        }
    }
    assertEquals(beforeUserIdMappings.size(), afterUserIdMappings.size() - 1);
}
Also used : MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) MappingTO(org.apache.syncope.common.lib.to.MappingTO) ResourceTO(org.apache.syncope.common.lib.to.ResourceTO) ProvisionTO(org.apache.syncope.common.lib.to.ProvisionTO) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) ItemTO(org.apache.syncope.common.lib.to.ItemTO) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 5 with ExternalResource

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

the class ConnectorManager method load.

@Transactional(readOnly = true)
@Override
public void load() {
    // This is needed in order to avoid encoding problems when sending error messages via REST
    CurrentLocale.set(Locale.ENGLISH);
    // Load all connector bundles
    connIdBundleManager.getConnManagers();
    // Load all resource-specific connectors
    int connectors = 0;
    for (ExternalResource resource : resourceDAO.findAll()) {
        LOG.info("Registering resource-connector pair {}-{}", resource, resource.getConnector());
        try {
            registerConnector(resource);
            connectors++;
        } catch (Exception e) {
            LOG.error("While registering resource-connector pair {}-{}", resource, resource.getConnector(), e);
        }
    }
    LOG.info("Done loading {} connectors", connectors);
}
Also used : ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ExternalResource (org.apache.syncope.core.persistence.api.entity.resource.ExternalResource)59 Test (org.junit.jupiter.api.Test)30 AbstractTest (org.apache.syncope.core.persistence.jpa.AbstractTest)28 Provision (org.apache.syncope.core.persistence.api.entity.resource.Provision)23 MappingItem (org.apache.syncope.core.persistence.api.entity.resource.MappingItem)21 ConnInstance (org.apache.syncope.core.persistence.api.entity.ConnInstance)15 List (java.util.List)14 Collectors (java.util.stream.Collectors)14 HashSet (java.util.HashSet)13 NotFoundException (org.apache.syncope.core.persistence.api.dao.NotFoundException)13 Autowired (org.springframework.beans.factory.annotation.Autowired)13 Set (java.util.Set)12 Transactional (org.springframework.transaction.annotation.Transactional)12 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)10 ExternalResourceDAO (org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO)10 Collections (java.util.Collections)9 AnyTypeDAO (org.apache.syncope.core.persistence.api.dao.AnyTypeDAO)9 Realm (org.apache.syncope.core.persistence.api.entity.Realm)9 PasswordPolicy (org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy)9 AnyType (org.apache.syncope.core.persistence.api.entity.AnyType)8