use of org.apache.syncope.core.persistence.api.entity.resource.MappingItem in project syncope by apache.
the class MappingManagerImpl method prepareAttrs.
@Transactional(readOnly = true)
@Override
public Pair<String, Set<Attribute>> prepareAttrs(final Any<?> any, final String password, final boolean changePwd, final Boolean enable, final Provision provision) {
LOG.debug("Preparing resource attributes for {} with provision {} for attributes {}", any, provision, any.getPlainAttrs());
Set<Attribute> attributes = new HashSet<>();
String connObjectKey = null;
for (Item mapItem : MappingUtils.getPropagationItems(provision.getMapping().getItems())) {
LOG.debug("Processing expression '{}'", mapItem.getIntAttrName());
try {
Pair<String, Attribute> preparedAttr = prepareAttr(provision, mapItem, any, password);
if (preparedAttr != null) {
if (preparedAttr.getLeft() != null) {
connObjectKey = preparedAttr.getLeft();
}
if (preparedAttr.getRight() != null) {
Attribute alreadyAdded = AttributeUtil.find(preparedAttr.getRight().getName(), attributes);
if (alreadyAdded == null) {
attributes.add(preparedAttr.getRight());
} else {
attributes.remove(alreadyAdded);
Set<Object> values = new HashSet<>();
if (alreadyAdded.getValue() != null && !alreadyAdded.getValue().isEmpty()) {
values.addAll(alreadyAdded.getValue());
}
if (preparedAttr.getRight().getValue() != null) {
values.addAll(preparedAttr.getRight().getValue());
}
attributes.add(AttributeBuilder.build(preparedAttr.getRight().getName(), values));
}
}
}
} catch (Exception e) {
LOG.error("Expression '{}' processing failed", mapItem.getIntAttrName(), e);
}
}
Optional<MappingItem> connObjectKeyItem = MappingUtils.getConnObjectKeyItem(provision);
if (connObjectKeyItem.isPresent()) {
Attribute connObjectKeyExtAttr = AttributeUtil.find(connObjectKeyItem.get().getExtAttrName(), attributes);
if (connObjectKeyExtAttr != null) {
attributes.remove(connObjectKeyExtAttr);
attributes.add(AttributeBuilder.build(connObjectKeyItem.get().getExtAttrName(), connObjectKey));
}
Name name = MappingUtils.evaluateNAME(any, provision, connObjectKey);
attributes.add(name);
if (connObjectKey != null && !connObjectKey.equals(name.getNameValue()) && connObjectKeyExtAttr == null) {
attributes.add(AttributeBuilder.build(connObjectKeyItem.get().getExtAttrName(), connObjectKey));
}
}
if (enable != null) {
attributes.add(AttributeBuilder.buildEnabled(enable));
}
if (!changePwd) {
Attribute pwdAttr = AttributeUtil.find(OperationalAttributes.PASSWORD_NAME, attributes);
if (pwdAttr != null) {
attributes.remove(pwdAttr);
}
}
return Pair.of(connObjectKey, attributes);
}
use of org.apache.syncope.core.persistence.api.entity.resource.MappingItem in project syncope by apache.
the class MappingManagerImpl method getConnObjectKeyValue.
@Transactional(readOnly = true)
@Override
public Optional<String> getConnObjectKeyValue(final Any<?> any, final Provision provision) {
MappingItem mapItem = provision.getMapping().getConnObjectKeyItem().get();
List<PlainAttrValue> values;
try {
values = getIntValues(provision, mapItem, intAttrNameParser.parse(mapItem.getIntAttrName(), provision.getAnyType().getKind()), any);
} catch (ParseException e) {
LOG.error("Invalid intAttrName '{}' specified, ignoring", mapItem.getIntAttrName(), e);
values = Collections.emptyList();
}
return Optional.ofNullable(values.isEmpty() ? null : values.get(0).getValueAsString());
}
use of org.apache.syncope.core.persistence.api.entity.resource.MappingItem in project syncope by apache.
the class VirAttrHandlerImpl method getValues.
private Map<VirSchema, List<String>> getValues(final Any<?> any, final Set<VirSchema> schemas) {
Set<ExternalResource> ownedResources = anyUtilsFactory.getInstance(any).getAllResources(any);
Map<VirSchema, List<String>> result = new HashMap<>();
Map<Provision, Set<VirSchema>> toRead = new HashMap<>();
for (VirSchema schema : schemas) {
if (ownedResources.contains(schema.getProvision().getResource())) {
VirAttrCacheValue virAttrCacheValue = virAttrCache.get(any.getType().getKey(), any.getKey(), schema.getKey());
if (virAttrCache.isValidEntry(virAttrCacheValue)) {
LOG.debug("Values for {} found in cache: {}", schema, virAttrCacheValue);
result.put(schema, virAttrCacheValue.getValues());
} else {
Set<VirSchema> schemasToRead = toRead.get(schema.getProvision());
if (schemasToRead == null) {
schemasToRead = new HashSet<>();
toRead.put(schema.getProvision(), schemasToRead);
}
schemasToRead.add(schema);
}
} else {
LOG.debug("Not considering {} since {} is not assigned to {}", schema, any, schema.getProvision().getResource());
}
}
for (Map.Entry<Provision, Set<VirSchema>> entry : toRead.entrySet()) {
LOG.debug("About to read from {}: {}", entry.getKey(), entry.getValue());
Optional<MappingItem> connObjectKeyItem = MappingUtils.getConnObjectKeyItem(entry.getKey());
String connObjectKeyValue = connObjectKeyItem.isPresent() ? mappingManager.getConnObjectKeyValue(any, entry.getKey()).orElse(null) : null;
if (!connObjectKeyItem.isPresent() || connObjectKeyValue == null) {
LOG.error("No ConnObjectKey or value found for {}, ignoring...", entry.getKey());
} else {
Set<MappingItem> linkingMappingItems = new HashSet<>();
linkingMappingItems.add(connObjectKeyItem.get());
linkingMappingItems.addAll(entry.getValue().stream().map(schema -> schema.asLinkingMappingItem()).collect(Collectors.toSet()));
Connector connector = connFactory.getConnector(entry.getKey().getResource());
try {
ConnectorObject connectorObject = connector.getObject(entry.getKey().getObjectClass(), AttributeBuilder.build(connObjectKeyItem.get().getExtAttrName(), connObjectKeyValue), MappingUtils.buildOperationOptions(linkingMappingItems.iterator()));
if (connectorObject == null) {
LOG.debug("No read from {} with filter '{} == {}'", entry.getKey(), connObjectKeyItem.get().getExtAttrName(), connObjectKeyValue);
} else {
entry.getValue().forEach(schema -> {
Attribute attr = connectorObject.getAttributeByName(schema.getExtAttrName());
if (attr != null) {
VirAttrCacheValue virAttrCacheValue = new VirAttrCacheValue();
virAttrCacheValue.setValues(attr.getValue());
virAttrCache.put(any.getType().getKey(), any.getKey(), schema.getKey(), virAttrCacheValue);
LOG.debug("Values for {} set in cache: {}", schema, virAttrCacheValue);
result.put(schema, virAttrCacheValue.getValues());
}
});
}
} catch (Exception e) {
LOG.error("Error reading from {}", entry.getKey(), e);
}
}
}
return result;
}
use of org.apache.syncope.core.persistence.api.entity.resource.MappingItem in project syncope by apache.
the class ResourceTest method save.
@Test
public void save() {
ExternalResource resource = entityFactory.newEntity(ExternalResource.class);
resource.setKey("ws-target-resource-basic-save");
resource.setPropagationPriority(2);
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);
ConnInstance connector = resourceDAO.find("ws-target-resource-1").getConnector();
resource.setConnector(connector);
// save the resource
ExternalResource actual = resourceDAO.save(resource);
assertNotNull(actual);
assertNotNull(actual.getConnector());
assertNotNull(actual.getProvision(anyTypeDAO.findUser()).get().getMapping());
assertFalse(actual.getProvision(anyTypeDAO.findUser()).get().getMapping().getItems().isEmpty());
assertEquals(Integer.valueOf(2), actual.getPropagationPriority());
}
use of org.apache.syncope.core.persistence.api.entity.resource.MappingItem in project syncope by apache.
the class ResourceTest method saveInvalidMappingIntAttr.
@Test
public void saveInvalidMappingIntAttr() {
assertThrows(InvalidEntityException.class, () -> {
ExternalResource resource = entityFactory.newEntity(ExternalResource.class);
resource.setKey("ws-target-resource-basic-save-invalid");
ConnInstance connector = resourceDAO.find("ws-target-resource-1").getConnector();
resource.setConnector(connector);
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.setConnObjectKey(true);
mapping.add(connObjectKey);
// save the resource
resourceDAO.save(resource);
});
}
Aggregations