use of org.apache.syncope.core.persistence.api.entity.VirSchema in project syncope by apache.
the class JPAVirSchemaDAO method findByAnyTypeClasses.
@Override
public List<VirSchema> findByAnyTypeClasses(final Collection<AnyTypeClass> anyTypeClasses) {
StringBuilder queryString = new StringBuilder("SELECT e FROM ").append(JPAVirSchema.class.getSimpleName()).append(" e WHERE ");
for (AnyTypeClass anyTypeClass : anyTypeClasses) {
queryString.append("e.anyTypeClass.id='").append(anyTypeClass.getKey()).append("' OR ");
}
TypedQuery<VirSchema> query = entityManager().createQuery(queryString.substring(0, queryString.length() - 4), VirSchema.class);
return query.getResultList();
}
use of org.apache.syncope.core.persistence.api.entity.VirSchema in project syncope by apache.
the class JPAVirSchemaDAO method findByProvision.
@Override
public List<VirSchema> findByProvision(final Provision provision) {
Query query = entityManager().createNativeQuery("SELECT t0.id FROM VirSchema t0 " + "LEFT OUTER JOIN " + JPAAnyTypeClass.TABLE + " t1 ON t0.ANYTYPECLASS_ID = t1.id " + "LEFT OUTER JOIN " + JPAProvision.TABLE + " t2 ON t0.PROVISION_ID = t2.id " + "LEFT OUTER JOIN " + JPAAnyType.TABLE + " t3 ON t2.ANYTYPE_ID = t3.id " + "LEFT OUTER JOIN " + JPAMapping.TABLE + " t4 ON t2.id = t4.PROVISION_ID " + "LEFT OUTER JOIN " + JPAExternalResource.TABLE + " t5 ON t2.RESOURCE_ID = t5.id " + "LEFT OUTER JOIN " + JPAAccountPolicy.TABLE + " t6 ON t5.ACCOUNTPOLICY_ID = t6.id " + "LEFT OUTER JOIN " + JPAConnInstance.TABLE + " t7 ON t5.CONNECTOR_ID = t7.id " + "LEFT OUTER JOIN " + JPAPasswordPolicy.TABLE + " t8 ON t5.PASSWORDPOLICY_ID = t8.id " + "LEFT OUTER JOIN " + JPAPullPolicy.TABLE + " t9 ON t5.PULLPOLICY_ID = t9.id " + "WHERE t0.PROVISION_ID = ?1");
query.setParameter(1, provision.getKey());
List<VirSchema> result = new ArrayList<>();
for (Object key : query.getResultList()) {
String actualKey = key instanceof Object[] ? (String) ((Object[]) key)[0] : ((String) key);
VirSchema virSchema = find(actualKey);
if (virSchema == null) {
LOG.error("Could not find schema with id {}, even though returned by the native query", actualKey);
} else if (!result.contains(virSchema)) {
result.add(virSchema);
}
}
return result;
}
use of org.apache.syncope.core.persistence.api.entity.VirSchema in project syncope by apache.
the class JPAVirSchemaDAO method delete.
@Override
public void delete(final String key) {
VirSchema schema = find(key);
if (schema == null) {
return;
}
resourceDAO().deleteMapping(key);
if (schema.getAnyTypeClass() != null) {
schema.getAnyTypeClass().getVirSchemas().remove(schema);
}
entityManager().remove(schema);
}
use of org.apache.syncope.core.persistence.api.entity.VirSchema 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;
}
use of org.apache.syncope.core.persistence.api.entity.VirSchema in project syncope by apache.
the class MappingManagerImpl method getIntValues.
@Transactional(readOnly = true)
@Override
public List<PlainAttrValue> getIntValues(final Provision provision, final Item mapItem, final IntAttrName intAttrName, final Any<?> any) {
LOG.debug("Get internal values for {} as '{}' on {}", any, mapItem.getIntAttrName(), provision.getResource());
Any<?> reference = null;
Membership<?> membership = null;
if (intAttrName.getEnclosingGroup() == null && intAttrName.getRelatedAnyObject() == null) {
reference = any;
}
if (any instanceof GroupableRelatable) {
GroupableRelatable<?, ?, ?, ?, ?> groupableRelatable = (GroupableRelatable<?, ?, ?, ?, ?>) any;
if (intAttrName.getEnclosingGroup() != null) {
Group group = groupDAO.findByName(intAttrName.getEnclosingGroup());
if (group == null || !groupableRelatable.getMembership(group.getKey()).isPresent()) {
LOG.warn("No membership for {} in {}, ignoring", intAttrName.getEnclosingGroup(), groupableRelatable);
} else {
reference = group;
}
} else if (intAttrName.getRelatedAnyObject() != null) {
AnyObject anyObject = anyObjectDAO.findByName(intAttrName.getRelatedAnyObject());
if (anyObject == null || groupableRelatable.getRelationships(anyObject.getKey()).isEmpty()) {
LOG.warn("No relationship for {} in {}, ignoring", intAttrName.getRelatedAnyObject(), groupableRelatable);
} else {
reference = anyObject;
}
} else if (intAttrName.getMembershipOfGroup() != null) {
Group group = groupDAO.findByName(intAttrName.getMembershipOfGroup());
membership = groupableRelatable.getMembership(group.getKey()).orElse(null);
}
}
if (reference == null) {
LOG.warn("Could not determine the reference instance for {}", mapItem.getIntAttrName());
return Collections.emptyList();
}
List<PlainAttrValue> values = new ArrayList<>();
boolean transform = true;
AnyUtils anyUtils = anyUtilsFactory.getInstance(reference);
if (intAttrName.getField() != null) {
PlainAttrValue attrValue = anyUtils.newPlainAttrValue();
switch(intAttrName.getField()) {
case "key":
attrValue.setStringValue(reference.getKey());
values.add(attrValue);
break;
case "realm":
attrValue.setStringValue(reference.getRealm().getFullPath());
values.add(attrValue);
break;
case "password":
// ignore
break;
case "userOwner":
case "groupOwner":
Mapping uMapping = provision.getAnyType().equals(anyTypeDAO.findUser()) ? provision.getMapping() : null;
Mapping gMapping = provision.getAnyType().equals(anyTypeDAO.findGroup()) ? provision.getMapping() : null;
if (reference instanceof Group) {
Group group = (Group) reference;
String groupOwnerValue = null;
if (group.getUserOwner() != null && uMapping != null) {
groupOwnerValue = getGroupOwnerValue(provision, group.getUserOwner());
}
if (group.getGroupOwner() != null && gMapping != null) {
groupOwnerValue = getGroupOwnerValue(provision, group.getGroupOwner());
}
if (StringUtils.isNotBlank(groupOwnerValue)) {
attrValue.setStringValue(groupOwnerValue);
values.add(attrValue);
}
}
break;
case "suspended":
if (reference instanceof User) {
attrValue.setBooleanValue(((User) reference).isSuspended());
values.add(attrValue);
}
break;
case "mustChangePassword":
if (reference instanceof User) {
attrValue.setBooleanValue(((User) reference).isMustChangePassword());
values.add(attrValue);
}
break;
default:
try {
Object fieldValue = FieldUtils.readField(reference, intAttrName.getField(), true);
if (fieldValue instanceof Date) {
// needed because ConnId does not natively supports the Date type
attrValue.setStringValue(DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT.format((Date) fieldValue));
} else if (Boolean.TYPE.isInstance(fieldValue)) {
attrValue.setBooleanValue((Boolean) fieldValue);
} else if (Double.TYPE.isInstance(fieldValue) || Float.TYPE.isInstance(fieldValue)) {
attrValue.setDoubleValue((Double) fieldValue);
} else if (Long.TYPE.isInstance(fieldValue) || Integer.TYPE.isInstance(fieldValue)) {
attrValue.setLongValue((Long) fieldValue);
} else {
attrValue.setStringValue(fieldValue.toString());
}
values.add(attrValue);
} catch (Exception e) {
LOG.error("Could not read value of '{}' from {}", intAttrName.getField(), reference, e);
}
}
} else if (intAttrName.getSchemaType() != null) {
switch(intAttrName.getSchemaType()) {
case PLAIN:
PlainAttr<?> attr;
if (membership == null) {
attr = reference.getPlainAttr(intAttrName.getSchemaName()).orElse(null);
} else {
attr = ((GroupableRelatable<?, ?, ?, ?, ?>) reference).getPlainAttr(intAttrName.getSchemaName(), membership).orElse(null);
}
if (attr == null) {
LOG.warn("Invalid PlainSchema {} or PlainAttr not found for {}", intAttrName.getSchemaName(), reference);
} else {
if (attr.getUniqueValue() != null) {
values.add(anyUtils.clonePlainAttrValue(attr.getUniqueValue()));
} else if (attr.getValues() != null) {
attr.getValues().forEach(value -> values.add(anyUtils.clonePlainAttrValue(value)));
}
}
break;
case DERIVED:
DerSchema derSchema = derSchemaDAO.find(intAttrName.getSchemaName());
if (derSchema == null) {
LOG.warn("Invalid DerSchema: {}", intAttrName.getSchemaName());
} else {
String derValue = membership == null ? derAttrHandler.getValue(reference, derSchema) : derAttrHandler.getValue(reference, membership, derSchema);
if (derValue != null) {
PlainAttrValue attrValue = anyUtils.newPlainAttrValue();
attrValue.setStringValue(derValue);
values.add(attrValue);
}
}
break;
case VIRTUAL:
// virtual attributes don't get transformed
transform = false;
VirSchema virSchema = virSchemaDAO.find(intAttrName.getSchemaName());
if (virSchema == null) {
LOG.warn("Invalid VirSchema: {}", intAttrName.getSchemaName());
} else {
LOG.debug("Expire entry cache {}-{}", reference, intAttrName.getSchemaName());
virAttrCache.expire(reference.getType().getKey(), reference.getKey(), intAttrName.getSchemaName());
List<String> virValues = membership == null ? virAttrHandler.getValues(reference, virSchema) : virAttrHandler.getValues(reference, membership, virSchema);
virValues.forEach(virValue -> {
PlainAttrValue attrValue = anyUtils.newPlainAttrValue();
attrValue.setStringValue(virValue);
values.add(attrValue);
});
}
break;
default:
}
} else if (intAttrName.getPrivilegesOfApplication() != null && reference instanceof User) {
Application application = applicationDAO.find(intAttrName.getPrivilegesOfApplication());
if (application == null) {
LOG.warn("Invalid application: {}", intAttrName.getPrivilegesOfApplication());
} else {
userDAO.findAllRoles((User) reference).stream().flatMap(role -> role.getPrivileges(application).stream()).forEach(privilege -> {
PlainAttrValue attrValue = anyUtils.newPlainAttrValue();
attrValue.setStringValue(privilege.getKey());
values.add(attrValue);
});
}
}
LOG.debug("Internal values: {}", values);
List<PlainAttrValue> transformed = values;
if (transform) {
for (ItemTransformer transformer : MappingUtils.getItemTransformers(mapItem)) {
transformed = transformer.beforePropagation(mapItem, any, transformed);
}
LOG.debug("Transformed values: {}", values);
} else {
LOG.debug("No transformation occurred");
}
return transformed;
}
Aggregations