use of org.apache.syncope.core.persistence.api.entity.PlainSchema in project syncope by apache.
the class AnyObjectDataBinderImpl method update.
@Override
public PropagationByResource update(final AnyObject toBeUpdated, final AnyObjectPatch anyObjectPatch) {
// Re-merge any pending change from workflow tasks
AnyObject anyObject = anyObjectDAO.save(toBeUpdated);
PropagationByResource propByRes = new PropagationByResource();
SyncopeClientCompositeException scce = SyncopeClientException.buildComposite();
AnyUtils anyUtils = anyUtilsFactory.getInstance(AnyTypeKind.ANY_OBJECT);
Collection<String> currentResources = anyObjectDAO.findAllResourceKeys(anyObject.getKey());
// fetch connObjectKeys before update
Map<String, String> oldConnObjectKeys = getConnObjectKeys(anyObject, anyUtils);
// realm
setRealm(anyObject, anyObjectPatch);
// name
if (anyObjectPatch.getName() != null && StringUtils.isNotBlank(anyObjectPatch.getName().getValue())) {
propByRes.addAll(ResourceOperation.UPDATE, anyObjectDAO.findAllResourceKeys(anyObject.getKey()));
anyObject.setName(anyObjectPatch.getName().getValue());
}
// attributes and resources
propByRes.merge(fill(anyObject, anyObjectPatch, anyUtils, scce));
// relationships
anyObjectPatch.getRelationships().stream().filter(patch -> patch.getRelationshipTO() != null).forEachOrdered((patch) -> {
RelationshipType relationshipType = relationshipTypeDAO.find(patch.getRelationshipTO().getType());
if (relationshipType == null) {
LOG.debug("Ignoring invalid relationship type {}", patch.getRelationshipTO().getType());
} else {
Optional<? extends ARelationship> relationship = anyObject.getRelationship(relationshipType, patch.getRelationshipTO().getOtherEndKey());
if (relationship.isPresent()) {
anyObject.getRelationships().remove(relationship.get());
relationship.get().setLeftEnd(null);
}
if (patch.getOperation() == PatchOperation.ADD_REPLACE) {
if (StringUtils.isBlank(patch.getRelationshipTO().getOtherEndType()) || AnyTypeKind.USER.name().equals(patch.getRelationshipTO().getOtherEndType()) || AnyTypeKind.GROUP.name().equals(patch.getRelationshipTO().getOtherEndType())) {
SyncopeClientException invalidAnyType = SyncopeClientException.build(ClientExceptionType.InvalidAnyType);
invalidAnyType.getElements().add(AnyType.class.getSimpleName() + " not allowed for relationship: " + patch.getRelationshipTO().getOtherEndType());
scce.addException(invalidAnyType);
} else {
AnyObject otherEnd = anyObjectDAO.find(patch.getRelationshipTO().getOtherEndKey());
if (otherEnd == null) {
LOG.debug("Ignoring invalid any object {}", patch.getRelationshipTO().getOtherEndKey());
} else if (anyObject.getRealm().getFullPath().startsWith(otherEnd.getRealm().getFullPath())) {
ARelationship newRelationship = entityFactory.newEntity(ARelationship.class);
newRelationship.setType(relationshipType);
newRelationship.setRightEnd(otherEnd);
newRelationship.setLeftEnd(anyObject);
anyObject.add(newRelationship);
} else {
LOG.error("{} cannot be assigned to {}", otherEnd, anyObject);
SyncopeClientException unassignable = SyncopeClientException.build(ClientExceptionType.InvalidRelationship);
unassignable.getElements().add("Cannot be assigned: " + otherEnd);
scce.addException(unassignable);
}
}
}
}
});
// prepare for membership-related resource management
Collection<ExternalResource> resources = anyObjectDAO.findAllResources(anyObject);
Map<String, Set<String>> reasons = new HashMap<>();
anyObject.getResources().forEach(resource -> {
reasons.put(resource.getKey(), new HashSet<>(Collections.singleton(anyObject.getKey())));
});
anyObjectDAO.findAllGroupKeys(anyObject).forEach(group -> {
groupDAO.findAllResourceKeys(group).forEach(resource -> {
if (!reasons.containsKey(resource)) {
reasons.put(resource, new HashSet<>());
}
reasons.get(resource).add(group);
});
});
Set<String> toBeDeprovisioned = new HashSet<>();
Set<String> toBeProvisioned = new HashSet<>();
SyncopeClientException invalidValues = SyncopeClientException.build(ClientExceptionType.InvalidValues);
// memberships
anyObjectPatch.getMemberships().stream().filter((membPatch) -> (membPatch.getGroup() != null)).forEachOrdered(membPatch -> {
Optional<? extends AMembership> membership = anyObject.getMembership(membPatch.getGroup());
if (membership.isPresent()) {
anyObject.getMemberships().remove(membership.get());
membership.get().setLeftEnd(null);
anyObject.getPlainAttrs(membership.get()).forEach(attr -> {
anyObject.remove(attr);
attr.setOwner(null);
});
if (membPatch.getOperation() == PatchOperation.DELETE) {
groupDAO.findAllResourceKeys(membership.get().getRightEnd().getKey()).stream().filter(resource -> reasons.containsKey(resource)).forEach(resource -> {
reasons.get(resource).remove(membership.get().getRightEnd().getKey());
toBeProvisioned.add(resource);
});
}
}
if (membPatch.getOperation() == PatchOperation.ADD_REPLACE) {
Group group = groupDAO.find(membPatch.getGroup());
if (group == null) {
LOG.debug("Ignoring invalid group {}", membPatch.getGroup());
} else if (anyObject.getRealm().getFullPath().startsWith(group.getRealm().getFullPath())) {
AMembership newMembership = entityFactory.newEntity(AMembership.class);
newMembership.setRightEnd(group);
newMembership.setLeftEnd(anyObject);
anyObject.add(newMembership);
membPatch.getPlainAttrs().forEach(attrTO -> {
PlainSchema schema = getPlainSchema(attrTO.getSchema());
if (schema == null) {
LOG.debug("Invalid " + PlainSchema.class.getSimpleName() + "{}, ignoring...", attrTO.getSchema());
} else {
Optional<? extends APlainAttr> attr = anyObject.getPlainAttr(schema.getKey(), newMembership);
if (!attr.isPresent()) {
LOG.debug("No plain attribute found for {} and membership of {}", schema, newMembership.getRightEnd());
APlainAttr newAttr = anyUtils.newPlainAttr();
newAttr.setOwner(anyObject);
newAttr.setMembership(newMembership);
newAttr.setSchema(schema);
anyObject.add(newAttr);
AttrPatch patch = new AttrPatch.Builder().attrTO(attrTO).build();
processAttrPatch(anyObject, patch, schema, newAttr, anyUtils, resources, propByRes, invalidValues);
}
}
});
if (!invalidValues.isEmpty()) {
scce.addException(invalidValues);
}
toBeProvisioned.addAll(groupDAO.findAllResourceKeys(group.getKey()));
} else {
LOG.error("{} cannot be assigned to {}", group, anyObject);
SyncopeClientException unassignabled = SyncopeClientException.build(ClientExceptionType.InvalidMembership);
unassignabled.getElements().add("Cannot be assigned: " + group);
scce.addException(unassignabled);
}
}
});
// finalize resource management
reasons.entrySet().stream().filter(entry -> entry.getValue().isEmpty()).forEach(entry -> toBeDeprovisioned.add(entry.getKey()));
propByRes.addAll(ResourceOperation.DELETE, toBeDeprovisioned);
propByRes.addAll(ResourceOperation.UPDATE, toBeProvisioned);
// attribute values.
if (!toBeDeprovisioned.isEmpty() || !toBeProvisioned.isEmpty()) {
currentResources.removeAll(toBeDeprovisioned);
propByRes.addAll(ResourceOperation.UPDATE, currentResources);
}
// check if some connObjectKey was changed by the update above
Map<String, String> newcCnnObjectKeys = getConnObjectKeys(anyObject, anyUtils);
oldConnObjectKeys.entrySet().stream().filter(entry -> newcCnnObjectKeys.containsKey(entry.getKey()) && !entry.getValue().equals(newcCnnObjectKeys.get(entry.getKey()))).forEach(entry -> {
propByRes.addOldConnObjectKey(entry.getKey(), entry.getValue());
propByRes.add(ResourceOperation.UPDATE, entry.getKey());
});
Pair<Set<String>, Set<String>> dynGroupMembs = anyObjectDAO.saveAndGetDynGroupMembs(anyObject);
// finally check if any resource assignment is to be processed due to dynamic group membership change
dynGroupMembs.getLeft().stream().filter(group -> !dynGroupMembs.getRight().contains(group)).forEach(delete -> {
groupDAO.find(delete).getResources().stream().filter(resource -> !propByRes.contains(resource.getKey())).forEach(resource -> {
propByRes.add(ResourceOperation.DELETE, resource.getKey());
});
});
dynGroupMembs.getLeft().stream().filter(group -> dynGroupMembs.getRight().contains(group)).forEach(update -> {
groupDAO.find(update).getResources().stream().filter(resource -> !propByRes.contains(resource.getKey())).forEach(resource -> {
propByRes.add(ResourceOperation.UPDATE, resource.getKey());
});
});
dynGroupMembs.getRight().stream().filter(group -> !dynGroupMembs.getLeft().contains(group)).forEach(create -> {
groupDAO.find(create).getResources().stream().filter(resource -> !propByRes.contains(resource.getKey())).forEach(resource -> {
propByRes.add(ResourceOperation.CREATE, resource.getKey());
});
});
// Throw composite exception if there is at least one element set in the composing exceptions
if (scce.hasExceptions()) {
throw scce;
}
return propByRes;
}
use of org.apache.syncope.core.persistence.api.entity.PlainSchema in project syncope by apache.
the class SchemaLogic method resolveReference.
@Override
protected SchemaTO resolveReference(final Method method, final Object... args) throws UnresolvedReferenceException {
String key = null;
if (ArrayUtils.isNotEmpty(args)) {
for (int i = 0; key == null && i < args.length; i++) {
if (args[i] instanceof String) {
key = (String) args[i];
} else if (args[i] instanceof SchemaTO) {
key = ((SchemaTO) args[i]).getKey();
}
}
}
if (key != null) {
try {
SchemaTO result = null;
PlainSchema plainSchema = plainSchemaDAO.find(key);
if (plainSchema == null) {
DerSchema derSchema = derSchemaDAO.find(key);
if (derSchema == null) {
VirSchema virSchema = virSchemaDAO.find(key);
if (virSchema != null) {
result = binder.getVirSchemaTO(virSchema);
}
} else {
result = binder.getDerSchemaTO(derSchema);
}
} else {
result = binder.getPlainSchemaTO(plainSchema);
}
return result;
} catch (Throwable ignore) {
LOG.debug("Unresolved reference", ignore);
throw new UnresolvedReferenceException(ignore);
}
}
throw new UnresolvedReferenceException();
}
use of org.apache.syncope.core.persistence.api.entity.PlainSchema in project syncope by apache.
the class SchemaLogic method update.
@PreAuthorize("hasRole('" + StandardEntitlement.SCHEMA_UPDATE + "')")
public <T extends SchemaTO> void update(final SchemaType schemaType, final T schemaTO) {
if (!doesSchemaExist(schemaType, schemaTO.getKey())) {
throw new NotFoundException(schemaType + "/" + schemaTO.getKey());
}
switch(schemaType) {
case VIRTUAL:
VirSchema virSchema = virSchemaDAO.find(schemaTO.getKey());
if (virSchema == null) {
throw new NotFoundException("Virtual Schema '" + schemaTO.getKey() + "'");
}
virSchemaDAO.save(binder.update((VirSchemaTO) schemaTO, virSchema));
break;
case DERIVED:
DerSchema derSchema = derSchemaDAO.find(schemaTO.getKey());
if (derSchema == null) {
throw new NotFoundException("Derived schema '" + schemaTO.getKey() + "'");
}
derSchemaDAO.save(binder.update((DerSchemaTO) schemaTO, derSchema));
break;
case PLAIN:
default:
PlainSchema plainSchema = plainSchemaDAO.find(schemaTO.getKey());
if (plainSchema == null) {
throw new NotFoundException("Schema '" + schemaTO.getKey() + "'");
}
plainSchemaDAO.save(binder.update((PlainSchemaTO) schemaTO, plainSchema));
}
}
use of org.apache.syncope.core.persistence.api.entity.PlainSchema in project syncope by apache.
the class SchemaLogic method read.
@PreAuthorize("isAuthenticated()")
@SuppressWarnings("unchecked")
public <T extends SchemaTO> T read(final SchemaType schemaType, final String schemaKey) {
T read;
switch(schemaType) {
case VIRTUAL:
VirSchema virSchema = virSchemaDAO.find(schemaKey);
if (virSchema == null) {
throw new NotFoundException("Virtual Schema '" + schemaKey + "'");
}
read = (T) binder.getVirSchemaTO(virSchema);
break;
case DERIVED:
DerSchema derSchema = derSchemaDAO.find(schemaKey);
if (derSchema == null) {
throw new NotFoundException("Derived schema '" + schemaKey + "'");
}
read = (T) binder.getDerSchemaTO(derSchema);
break;
case PLAIN:
default:
PlainSchema schema = plainSchemaDAO.find(schemaKey);
if (schema == null) {
throw new NotFoundException("Schema '" + schemaKey + "'");
}
read = (T) binder.getPlainSchemaTO(schema);
}
return read;
}
use of org.apache.syncope.core.persistence.api.entity.PlainSchema in project syncope by apache.
the class ConfigurationLogic method get.
@PreAuthorize("hasRole('" + StandardEntitlement.CONFIGURATION_GET + "')")
@Transactional(readOnly = true)
public AttrTO get(final String schema) {
AttrTO result;
Optional<? extends CPlainAttr> conf = confDAO.find(schema);
if (conf.isPresent()) {
result = binder.getAttrTO(conf.get());
} else {
PlainSchema plainSchema = plainSchemaDAO.find(schema);
if (plainSchema == null) {
throw new NotFoundException("Configuration schema " + schema);
}
result = new AttrTO();
result.setSchema(schema);
}
return result;
}
Aggregations