From 94388d9a36a967d6ed274ca619a4e4dfd8ec3d37 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 2 Mar 2021 10:58:23 +0100 Subject: [PATCH] SanityCheck on Resource for non safe operation made on Facet/IsRealtedTo --- .../instances/model/Operation.java | 2 +- .../model/entities/FacetManagement.java | 60 +++++++++++++++-- .../model/relations/ConsistsOfManagement.java | 64 ++++++++++++++++++- 3 files changed, 119 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/Operation.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/Operation.java index af33dda..f4f45b9 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/Operation.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/Operation.java @@ -11,7 +11,7 @@ public enum Operation { REMOVE_FROM_CONTEXT, QUERY(true), // GET_METADATA e.g. getinstanceContexts - GET_METADATA; + GET_METADATA(true); private final boolean safe; diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/FacetManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/FacetManagement.java index e895715..99230fc 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/FacetManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/FacetManagement.java @@ -9,8 +9,12 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet. import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException; +import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode; +import org.gcube.informationsystem.resourceregistry.instances.model.Operation; import org.gcube.informationsystem.types.reference.entities.FacetType; +import com.orientechnologies.orient.core.db.document.ODatabaseDocument; +import com.orientechnologies.orient.core.record.ODirection; import com.orientechnologies.orient.core.record.OVertex; /** @@ -61,13 +65,61 @@ public class FacetManagement extends EntityManagement { return true; } + protected void checkResource() throws SchemaViolationException, ResourceRegistryException { + if(!entryPoint) { + return; + } + if(operation.isSafe()) { + /* You should not be here. + * The sanity check should not be triggered for a safety operation. + * Anyway, using this code as guard. + */ + logger.warn("sanityCheck should not be triggered for a safe method (i.e. {}). It is not an error but it slow down the performace. Please contact the developer", operation.toString()); + return; + } + + if(operation == Operation.UPDATE) { + // an update to the Facet only modify the Facet properties, no need to check the source resource + return; + } + ODatabaseDocument targetSecurityContextODatabaseDocument = null; + try { + OVertex oVertex = getElement().getVertices(ODirection.IN).iterator().next(); + ResourceManagement resourceManagement = new ResourceManagement(); + resourceManagement.setElement(oVertex); + switch (operation) { + case CREATE: case DELETE: + resourceManagement.setWorkingContext(getWorkingContext()); + resourceManagement.setODatabaseDocument(oDatabaseDocument); + break; + + case ADD_TO_CONTEXT: case REMOVE_FROM_CONTEXT: + resourceManagement.setWorkingContext(targetSecurityContext); + targetSecurityContextODatabaseDocument = targetSecurityContext.getDatabaseDocument(PermissionMode.READER); + resourceManagement.setODatabaseDocument(targetSecurityContextODatabaseDocument); + break; + + default: + // You should not be here + return; + } + + resourceManagement.sanityCheck(); + + }catch (Exception e) { + throw new ResourceRegistryException(e); + }finally { + if(targetSecurityContextODatabaseDocument!=null) { + targetSecurityContextODatabaseDocument.close(); + oDatabaseDocument.activateOnCurrentThread(); + } + } + } + @Override public void sanityCheck() throws SchemaViolationException, ResourceRegistryException { super.sanityCheck(); - if(entryPoint) { - // We need to check the Resource - } - + checkResource(); } } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/ConsistsOfManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/ConsistsOfManagement.java index c645f5a..5f051fe 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/ConsistsOfManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/ConsistsOfManagement.java @@ -12,9 +12,16 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.cons import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.consistsOf.ConsistsOfAvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.consistsOf.ConsistsOfNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException; +import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode; +import org.gcube.informationsystem.resourceregistry.instances.model.Operation; import org.gcube.informationsystem.resourceregistry.instances.model.entities.FacetManagement; +import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement; import org.gcube.informationsystem.types.reference.entities.FacetType; +import com.orientechnologies.orient.core.db.document.ODatabaseDocument; +import com.orientechnologies.orient.core.record.ODirection; +import com.orientechnologies.orient.core.record.OVertex; + /** * @author Luca Frosini (ISTI - CNR) */ @@ -56,10 +63,63 @@ public class ConsistsOfManagement extends RelationManagement