use of org.apache.syncope.core.persistence.api.entity.resource.Item in project syncope by apache.
the class DefaultPullCorrelationRule method getSearchCond.
@Override
public SearchCond getSearchCond(final ConnectorObject connObj, final Provision provision) {
Map<String, Item> mappingItems = provision.getMapping().getItems().stream().collect(Collectors.toMap(Item::getIntAttrName, Function.identity()));
// search for anys by attribute(s) specified in the policy
SearchCond searchCond = null;
for (String schema : conf.getSchemas()) {
Item mappingItem = mappingItems.get(schema);
Attribute attr = mappingItem == null ? null : connObj.getAttributeByName(mappingItem.getExtAttrName());
if (attr == null) {
throw new IllegalArgumentException("Connector object does not contains the attributes to perform the search: " + schema);
}
AttributeCond.Type type;
String expression = null;
if (attr.getValue() == null || attr.getValue().isEmpty() || (attr.getValue().size() == 1 && attr.getValue().get(0) == null)) {
type = AttributeCond.Type.ISNULL;
} else {
type = AttributeCond.Type.EQ;
expression = attr.getValue().size() > 1 ? attr.getValue().toString() : attr.getValue().get(0).toString();
}
SearchCond nodeCond;
// any objects: just key or name can be selected
if ("key".equalsIgnoreCase(schema) || "username".equalsIgnoreCase(schema) || "name".equalsIgnoreCase(schema)) {
AnyCond cond = new AnyCond();
cond.setSchema(schema);
cond.setType(type);
cond.setExpression(expression);
nodeCond = SearchCond.getLeafCond(cond);
} else {
AttributeCond cond = new AttributeCond();
cond.setSchema(schema);
cond.setType(type);
cond.setExpression(expression);
nodeCond = SearchCond.getLeafCond(cond);
}
searchCond = searchCond == null ? nodeCond : SearchCond.getAndCond(searchCond, nodeCond);
}
return searchCond;
}
use of org.apache.syncope.core.persistence.api.entity.resource.Item 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;
}
}
use of org.apache.syncope.core.persistence.api.entity.resource.Item 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);
}
Aggregations