use of org.apache.syncope.core.persistence.api.entity.resource.MappingItem in project syncope by apache.
the class ResourceDataBinderImpl method getResourceTO.
@Override
public ResourceTO getResourceTO(final ExternalResource resource) {
ResourceTO resourceTO = new ResourceTO();
// set the resource name
resourceTO.setKey(resource.getKey());
// set the connector instance
ConnInstance connector = resource.getConnector();
resourceTO.setConnector(connector == null ? null : connector.getKey());
resourceTO.setConnectorDisplayName(connector == null ? null : connector.getDisplayName());
// set the provision information
resource.getProvisions().stream().map(provision -> {
ProvisionTO provisionTO = new ProvisionTO();
provisionTO.setKey(provision.getKey());
provisionTO.setAnyType(provision.getAnyType().getKey());
provisionTO.setObjectClass(provision.getObjectClass().getObjectClassValue());
provisionTO.getAuxClasses().addAll(provision.getAuxClasses().stream().map(cls -> cls.getKey()).collect(Collectors.toList()));
provisionTO.setSyncToken(provision.getSerializedSyncToken());
if (provision.getMapping() != null) {
MappingTO mappingTO = new MappingTO();
provisionTO.setMapping(mappingTO);
mappingTO.setConnObjectLink(provision.getMapping().getConnObjectLink());
populateItems(provision.getMapping().getItems(), mappingTO);
}
virSchemaDAO.findByProvision(provision).forEach(virSchema -> {
provisionTO.getVirSchemas().add(virSchema.getKey());
MappingItem linkingMappingItem = virSchema.asLinkingMappingItem();
ItemTO itemTO = new ItemTO();
itemTO.setKey(linkingMappingItem.getKey());
BeanUtils.copyProperties(linkingMappingItem, itemTO, ITEM_IGNORE_PROPERTIES);
provisionTO.getMapping().getLinkingItems().add(itemTO);
});
return provisionTO;
}).forEachOrdered(provisionTO -> {
resourceTO.getProvisions().add(provisionTO);
});
if (resource.getOrgUnit() != null) {
OrgUnit orgUnit = resource.getOrgUnit();
OrgUnitTO orgUnitTO = new OrgUnitTO();
orgUnitTO.setKey(orgUnit.getKey());
orgUnitTO.setObjectClass(orgUnit.getObjectClass().getObjectClassValue());
orgUnitTO.setSyncToken(orgUnit.getSerializedSyncToken());
orgUnitTO.setConnObjectLink(orgUnit.getConnObjectLink());
populateItems(orgUnit.getItems(), orgUnitTO);
resourceTO.setOrgUnit(orgUnitTO);
}
resourceTO.setEnforceMandatoryCondition(resource.isEnforceMandatoryCondition());
resourceTO.setPropagationPriority(resource.getPropagationPriority());
resourceTO.setRandomPwdIfNotProvided(resource.isRandomPwdIfNotProvided());
resourceTO.setCreateTraceLevel(resource.getCreateTraceLevel());
resourceTO.setUpdateTraceLevel(resource.getUpdateTraceLevel());
resourceTO.setDeleteTraceLevel(resource.getDeleteTraceLevel());
resourceTO.setProvisioningTraceLevel(resource.getProvisioningTraceLevel());
resourceTO.setPasswordPolicy(resource.getPasswordPolicy() == null ? null : resource.getPasswordPolicy().getKey());
resourceTO.setAccountPolicy(resource.getAccountPolicy() == null ? null : resource.getAccountPolicy().getKey());
resourceTO.setPullPolicy(resource.getPullPolicy() == null ? null : resource.getPullPolicy().getKey());
resourceTO.getConfOverride().addAll(resource.getConfOverride());
Collections.sort(resourceTO.getConfOverride());
resourceTO.setOverrideCapabilities(resource.isOverrideCapabilities());
resourceTO.getCapabilitiesOverride().addAll(resource.getCapabilitiesOverride());
resourceTO.getPropagationActions().addAll(resource.getPropagationActions().stream().map(Entity::getKey).collect(Collectors.toList()));
return resourceTO;
}
use of org.apache.syncope.core.persistence.api.entity.resource.MappingItem in project syncope by apache.
the class ResourceLogic method listConnObjects.
@PreAuthorize("hasRole('" + StandardEntitlement.RESOURCE_LIST_CONNOBJECT + "')")
@Transactional(readOnly = true)
public Pair<SearchResult, List<ConnObjectTO>> listConnObjects(final String key, final String anyTypeKey, final int size, final String pagedResultsCookie, final List<OrderByClause> orderBy) {
ExternalResource resource;
ObjectClass objectClass;
OperationOptions options;
if (SyncopeConstants.REALM_ANYTYPE.equals(anyTypeKey)) {
resource = resourceDAO.authFind(key);
if (resource == null) {
throw new NotFoundException("Resource '" + key + "'");
}
if (resource.getOrgUnit() == null) {
throw new NotFoundException("Realm provisioning for resource '" + key + "'");
}
objectClass = resource.getOrgUnit().getObjectClass();
options = MappingUtils.buildOperationOptions(MappingUtils.getPropagationItems(resource.getOrgUnit().getItems()).iterator());
} else {
Triple<ExternalResource, AnyType, Provision> init = connObjectInit(key, anyTypeKey);
resource = init.getLeft();
objectClass = init.getRight().getObjectClass();
init.getRight().getMapping().getItems();
Set<MappingItem> linkinMappingItems = virSchemaDAO.findByProvision(init.getRight()).stream().map(virSchema -> virSchema.asLinkingMappingItem()).collect(Collectors.toSet());
Iterator<MappingItem> mapItems = new IteratorChain<>(init.getRight().getMapping().getItems().iterator(), linkinMappingItems.iterator());
options = MappingUtils.buildOperationOptions(mapItems);
}
final List<ConnObjectTO> connObjects = new ArrayList<>();
SearchResult searchResult = connFactory.getConnector(resource).search(objectClass, null, new ResultsHandler() {
private int count;
@Override
public boolean handle(final ConnectorObject connectorObject) {
connObjects.add(ConnObjectUtils.getConnObjectTO(connectorObject));
// safety protection against uncontrolled result size
count++;
return count < size;
}
}, size, pagedResultsCookie, orderBy, options);
return ImmutablePair.of(searchResult, connObjects);
}
use of org.apache.syncope.core.persistence.api.entity.resource.MappingItem in project syncope by apache.
the class ResourceLogic method readConnObject.
@PreAuthorize("hasRole('" + StandardEntitlement.RESOURCE_GET_CONNOBJECT + "')")
@Transactional(readOnly = true)
public ConnObjectTO readConnObject(final String key, final String anyTypeKey, final String anyKey) {
Triple<ExternalResource, AnyType, Provision> init = connObjectInit(key, anyTypeKey);
// 1. find any
Any<?> any = init.getMiddle().getKind() == AnyTypeKind.USER ? userDAO.find(anyKey) : init.getMiddle().getKind() == AnyTypeKind.ANY_OBJECT ? anyObjectDAO.find(anyKey) : groupDAO.find(anyKey);
if (any == null) {
throw new NotFoundException(init.getMiddle() + " " + anyKey);
}
// 2. build connObjectKeyItem
Optional<MappingItem> connObjectKeyItem = MappingUtils.getConnObjectKeyItem(init.getRight());
if (!connObjectKeyItem.isPresent()) {
throw new NotFoundException("ConnObjectKey mapping for " + init.getMiddle() + " " + anyKey + " on resource '" + key + "'");
}
Optional<String> connObjectKeyValue = mappingManager.getConnObjectKeyValue(any, init.getRight());
// 3. determine attributes to query
Set<MappingItem> linkinMappingItems = virSchemaDAO.findByProvision(init.getRight()).stream().map(virSchema -> virSchema.asLinkingMappingItem()).collect(Collectors.toSet());
Iterator<MappingItem> mapItems = new IteratorChain<>(init.getRight().getMapping().getItems().iterator(), linkinMappingItems.iterator());
// 4. read from the underlying connector
Connector connector = connFactory.getConnector(init.getLeft());
ConnectorObject connectorObject = connector.getObject(init.getRight().getObjectClass(), AttributeBuilder.build(connObjectKeyItem.get().getExtAttrName(), connObjectKeyValue.get()), MappingUtils.buildOperationOptions(mapItems));
if (connectorObject == null) {
throw new NotFoundException("Object " + connObjectKeyValue.get() + " with class " + init.getRight().getObjectClass() + " not found on resource " + key);
}
// 5. build result
Set<Attribute> attributes = connectorObject.getAttributes();
if (AttributeUtil.find(Uid.NAME, attributes) == null) {
attributes.add(connectorObject.getUid());
}
if (AttributeUtil.find(Name.NAME, attributes) == null) {
attributes.add(connectorObject.getName());
}
return ConnObjectUtils.getConnObjectTO(connectorObject);
}
use of org.apache.syncope.core.persistence.api.entity.resource.MappingItem in project syncope by apache.
the class ResourceTest method saveInvalidProvision.
@Test
public void saveInvalidProvision() {
assertThrows(InvalidEntityException.class, () -> {
ExternalResource resource = entityFactory.newEntity(ExternalResource.class);
resource.setKey("invalidProvision");
Provision provision = entityFactory.newEntity(Provision.class);
provision.setAnyType(anyTypeDAO.findUser());
provision.setObjectClass(ObjectClass.ACCOUNT);
provision.setResource(resource);
resource.add(provision);
Mapping mapping = entityFactory.newEntity(Mapping.class);
mapping.setProvision(provision);
provision.setMapping(mapping);
MappingItem connObjectKey = entityFactory.newEntity(MappingItem.class);
connObjectKey.setExtAttrName("username");
connObjectKey.setIntAttrName("fullname");
connObjectKey.setPurpose(MappingPurpose.BOTH);
mapping.setConnObjectKeyItem(connObjectKey);
provision = entityFactory.newEntity(Provision.class);
provision.setAnyType(anyTypeDAO.findGroup());
provision.setObjectClass(ObjectClass.ACCOUNT);
provision.setResource(resource);
resource.add(provision);
ConnInstance connector = resourceDAO.find("ws-target-resource-1").getConnector();
resource.setConnector(connector);
// save the resource
resourceDAO.save(resource);
});
}
use of org.apache.syncope.core.persistence.api.entity.resource.MappingItem in project syncope by apache.
the class ResourceDataBinderImpl method populateMapping.
private void populateMapping(final MappingTO mappingTO, final Mapping mapping, final AnyTypeClassTO allowedSchemas) {
mapping.setConnObjectLink(mappingTO.getConnObjectLink());
SyncopeClientCompositeException scce = SyncopeClientException.buildComposite();
SyncopeClientException invalidMapping = SyncopeClientException.build(ClientExceptionType.InvalidMapping);
SyncopeClientException requiredValuesMissing = SyncopeClientException.build(ClientExceptionType.RequiredValuesMissing);
for (ItemTO itemTO : mappingTO.getItems()) {
if (itemTO == null) {
LOG.error("Null {}", ItemTO.class.getSimpleName());
invalidMapping.getElements().add("Null " + ItemTO.class.getSimpleName());
} else if (itemTO.getIntAttrName() == null) {
requiredValuesMissing.getElements().add("intAttrName");
scce.addException(requiredValuesMissing);
} else {
IntAttrName intAttrName = null;
try {
intAttrName = intAttrNameParser.parse(itemTO.getIntAttrName(), mapping.getProvision().getAnyType().getKind());
} catch (ParseException e) {
LOG.error("Invalid intAttrName '{}'", itemTO.getIntAttrName(), e);
}
if (intAttrName == null || intAttrName.getSchemaType() == null && intAttrName.getField() == null && intAttrName.getPrivilegesOfApplication() == null) {
LOG.error("'{}' not existing", itemTO.getIntAttrName());
invalidMapping.getElements().add("'" + itemTO.getIntAttrName() + "' not existing");
} else {
boolean allowed = true;
if (intAttrName.getSchemaType() != null && intAttrName.getEnclosingGroup() == null && intAttrName.getRelatedAnyObject() == null && intAttrName.getPrivilegesOfApplication() == null) {
switch(intAttrName.getSchemaType()) {
case PLAIN:
allowed = allowedSchemas.getPlainSchemas().contains(intAttrName.getSchemaName());
break;
case DERIVED:
allowed = allowedSchemas.getDerSchemas().contains(intAttrName.getSchemaName());
break;
case VIRTUAL:
allowed = allowedSchemas.getVirSchemas().contains(intAttrName.getSchemaName());
break;
default:
}
}
if (allowed) {
// no mandatory condition implies mandatory condition false
if (!JexlUtils.isExpressionValid(itemTO.getMandatoryCondition() == null ? "false" : itemTO.getMandatoryCondition())) {
SyncopeClientException invalidMandatoryCondition = SyncopeClientException.build(ClientExceptionType.InvalidValues);
invalidMandatoryCondition.getElements().add(itemTO.getMandatoryCondition());
scce.addException(invalidMandatoryCondition);
}
MappingItem item = entityFactory.newEntity(MappingItem.class);
BeanUtils.copyProperties(itemTO, item, ITEM_IGNORE_PROPERTIES);
item.setMapping(mapping);
if (item.isConnObjectKey()) {
if (intAttrName.getSchemaType() == SchemaType.VIRTUAL) {
invalidMapping.getElements().add("Virtual attributes cannot be set as ConnObjectKey");
}
if ("password".equals(intAttrName.getField())) {
invalidMapping.getElements().add("Password attributes cannot be set as ConnObjectKey");
}
mapping.setConnObjectKeyItem(item);
} else {
mapping.add(item);
}
itemTO.getTransformers().forEach(transformerKey -> {
Implementation transformer = implementationDAO.find(transformerKey);
if (transformer == null) {
LOG.debug("Invalid " + Implementation.class.getSimpleName() + " {}, ignoring...", transformerKey);
} else {
item.add(transformer);
}
});
// remove all implementations not contained in the TO
item.getTransformers().removeIf(implementation -> !itemTO.getTransformers().contains(implementation.getKey()));
if (intAttrName.getEnclosingGroup() != null && item.getPurpose() != MappingPurpose.PROPAGATION) {
invalidMapping.getElements().add("Only " + MappingPurpose.PROPAGATION.name() + " allowed when referring to groups");
}
if (intAttrName.getRelatedAnyObject() != null && item.getPurpose() != MappingPurpose.PROPAGATION) {
invalidMapping.getElements().add("Only " + MappingPurpose.PROPAGATION.name() + " allowed when referring to any objects");
}
if (intAttrName.getPrivilegesOfApplication() != null && item.getPurpose() != MappingPurpose.PROPAGATION) {
invalidMapping.getElements().add("Only " + MappingPurpose.PROPAGATION.name() + " allowed when referring to privileges");
}
if (intAttrName.getSchemaType() == SchemaType.DERIVED && item.getPurpose() != MappingPurpose.PROPAGATION) {
invalidMapping.getElements().add("Only " + MappingPurpose.PROPAGATION.name() + " allowed for derived");
}
if (intAttrName.getSchemaType() == SchemaType.VIRTUAL) {
if (item.getPurpose() != MappingPurpose.PROPAGATION) {
invalidMapping.getElements().add("Only " + MappingPurpose.PROPAGATION.name() + " allowed for virtual");
}
VirSchema schema = virSchemaDAO.find(item.getIntAttrName());
if (schema != null && schema.getProvision().equals(item.getMapping().getProvision())) {
invalidMapping.getElements().add("No need to map virtual schema on linking resource");
}
}
} else {
LOG.error("'{}' not allowed", itemTO.getIntAttrName());
invalidMapping.getElements().add("'" + itemTO.getIntAttrName() + "' not allowed");
}
}
}
}
if (!invalidMapping.getElements().isEmpty()) {
scce.addException(invalidMapping);
}
if (scce.hasExceptions()) {
throw scce;
}
}
Aggregations