Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: refactor checkIfAllowedForOrganisations #1008

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,19 @@ public List<CollectionReference> getAllCollectionReferences(Optional<String> col
}
hits = client.search(requestBuilder.build(), CollectionReferenceParameters.class).hits().hits();
for (Hit<CollectionReferenceParameters> hit : hits) {
try {
CollectionReference colRef = new CollectionReference(hit.id(), hit.source());
checkIfAllowedForOrganisations(colRef, organisations);
if (CollectionUtil.isCollectionPublic(colRef)) {
collections.add(colRef);
} else {
for (String c : allowedCollections) {
if (CollectionUtil.matches(c, hit.id())) {
collections.add(colRef);
break;
if (checkIfAllowedForOrganisations(colRef, organisations)) {
if (CollectionUtil.isCollectionPublic(colRef)) {
collections.add(colRef);
} else {
for (String c : allowedCollections) {
if (CollectionUtil.matches(c, hit.id())) {
collections.add(colRef);
break;
}
}
}
}
} catch (CollectionUnavailableException e) {
LOGGER.warn(String.format("Collection %s not available for this organisation %s",
hit.id(), organisations));
}
}
} while (!hits.isEmpty());
} catch (NotFoundException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,16 @@ public CollectionReference getCollectionReference(String ref, Optional<String> o
collectionReference = getCollectionReferenceFromDao(ref);
cacheManager.putCollectionReference(ref, collectionReference);
}
checkIfAllowedForOrganisations(collectionReference, organisations);
if (!getMapping(collectionReference.params.indexName).isEmpty()){
return collectionReference;
if (checkIfAllowedForOrganisations(collectionReference, organisations)) {
if (!getMapping(collectionReference.params.indexName).isEmpty()) {
return collectionReference;
} else {
throw new ArlasException("Collection " + ref + " exists but can not be described. Check if index or template ".concat(collectionReference.params.indexName).concat(" exists"));
}
} else {
throw new ArlasException("Collection " + ref + " exists but can not be described. Check if index or template ".concat(collectionReference.params.indexName).concat(" exists"));
throw new CollectionUnavailableException("The collection not available with organisation header: " + Optional.ofNullable(organisations).get());
}

}

protected Map<String, Map<String, Object>> getMapping(String indexName) throws ArlasException {
Expand Down Expand Up @@ -118,20 +122,23 @@ public CollectionReference updateDisplayNamesCollectionReference(String collecti
throws ArlasException {
CollectionReference collectionReference = getCollectionReference(collection, Optional.ofNullable(organisations));
ColumnFilterUtil.assertCollectionsAllowed(Optional.ofNullable(columnFilter), List.of(collectionReference));
checkIfAllowedForOrganisations(collectionReference, Optional.ofNullable(organisations), true);
if (collectionReference.params.collectionDisplayNames == null) {
collectionReference.params.collectionDisplayNames = new CollectionDisplayNames();
}
if(fieldsDisplayNames != null){
collectionReference.params.collectionDisplayNames.fields = fieldsDisplayNames;
}
if(shapeColumnsDisplayNames != null){
collectionReference.params.collectionDisplayNames.shapeColumns = shapeColumnsDisplayNames;
}
if(collectionDisplayName != null){
collectionReference.params.collectionDisplayNames.collection = collectionDisplayName;
if (checkIfAllowedForOrganisations(collectionReference, Optional.ofNullable(organisations), true)) {
if (collectionReference.params.collectionDisplayNames == null) {
collectionReference.params.collectionDisplayNames = new CollectionDisplayNames();
}
if(fieldsDisplayNames != null){
collectionReference.params.collectionDisplayNames.fields = fieldsDisplayNames;
}
if(shapeColumnsDisplayNames != null){
collectionReference.params.collectionDisplayNames.shapeColumns = shapeColumnsDisplayNames;
}
if(collectionDisplayName != null){
collectionReference.params.collectionDisplayNames.collection = collectionDisplayName;
}
return putCollectionReference(collectionReference, true);
} else {
throw new CollectionUnavailableException("The collection not available with organisation header: " + Optional.ofNullable(organisations).get());
}
return putCollectionReference(collectionReference, true);
}

public CollectionReference updateOrganisationsParamsCollectionReference(String collection,
Expand All @@ -142,10 +149,13 @@ public CollectionReference updateOrganisationsParamsCollectionReference(String c
throws ArlasException {
CollectionReference collectionReference = getCollectionReference(collection, Optional.ofNullable(organisations));
ColumnFilterUtil.assertCollectionsAllowed(Optional.ofNullable(columnFilter), List.of(collectionReference));
checkIfAllowedForOrganisations(collectionReference, Optional.ofNullable(organisations), true);
collectionReference.params.collectionOrganisations.isPublic = isPublic;
collectionReference.params.collectionOrganisations.sharedWith = sharedWith;
return putCollectionReference(collectionReference, true);
if (checkIfAllowedForOrganisations(collectionReference, Optional.ofNullable(organisations), true)) {
collectionReference.params.collectionOrganisations.isPublic = isPublic;
collectionReference.params.collectionOrganisations.sharedWith = sharedWith;
return putCollectionReference(collectionReference, true);
} else {
throw new CollectionUnavailableException("The collection not available with organisation header: " + Optional.ofNullable(organisations).get());
}
}

public List<CollectionReferenceDescription> describeAllCollections(List<CollectionReference> collectionReferenceList,
Expand Down Expand Up @@ -398,10 +408,10 @@ private FieldType getFieldType(String field, String index) throws ArlasException
return ret.get();
}

protected void checkIfAllowedForOrganisations(CollectionReference collection,
protected Boolean checkIfAllowedForOrganisations(CollectionReference collection,
Optional<String> organisations)
throws CollectionUnavailableException {
checkIfAllowedForOrganisations(collection, organisations, false);
return checkIfAllowedForOrganisations(collection, organisations, false);
}

public void checkIfIndexAllowedForOrganisations(CollectionReference collection,
Expand Down Expand Up @@ -430,14 +440,14 @@ public void checkIfIndexAllowedForOrganisations(CollectionReference collection,
}
}

public void checkIfAllowedForOrganisations(CollectionReference collection,
public Boolean checkIfAllowedForOrganisations(CollectionReference collection,
Optional<String> organisations,
boolean ownerOnly)
throws CollectionUnavailableException {
if (organisations.isEmpty()) {
// no header, we'll trust the column filter if any
LOGGER.debug("No organisation header");
return;
return true;
}

if (collection.params.collectionOrganisations == null) {
Expand All @@ -448,7 +458,7 @@ public void checkIfAllowedForOrganisations(CollectionReference collection,

if (!ownerOnly && collection.params.collectionOrganisations.isPublic) {
LOGGER.debug(String.format("Collection %s organisation is public.", collection.collectionName));
return;
return true;
}

List<String> o = new ArrayList<>();
Expand All @@ -461,7 +471,9 @@ public void checkIfAllowedForOrganisations(CollectionReference collection,
o.retainAll(Arrays.stream(organisations.get().split(",")).toList());
LOGGER.debug("allowed org=" + o);
if (o.isEmpty()) {
throw new CollectionUnavailableException("The collection not available with organisation header: " + organisations.get());
// the collection not available with the provided organisation header
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,12 @@ public CollectionReference save(String collection, CollectionReferenceParameters
CheckParams.checkInvalidDublinCoreElementsForInspire(collectionReference);
}
CheckParams.checkInvalidInspireParameters(collectionReference);
collectionReferenceService.checkIfAllowedForOrganisations(collectionReference, Optional.ofNullable(organisations), true);
collectionReferenceService.checkIfIndexAllowedForOrganisations(collectionReference, Optional.ofNullable(organisations), Optional.ofNullable(configuration.arlasAuthPolicyClass));
return collectionReferenceService.putCollectionReference(collectionReference, checkFields);
if (collectionReferenceService.checkIfAllowedForOrganisations(collectionReference, Optional.ofNullable(organisations), true)) {
collectionReferenceService.checkIfIndexAllowedForOrganisations(collectionReference, Optional.ofNullable(organisations), Optional.ofNullable(configuration.arlasAuthPolicyClass));
return collectionReferenceService.putCollectionReference(collectionReference, checkFields);
} else {
throw new CollectionUnavailableException("The collection not available with organisation header: " + Optional.ofNullable(organisations).get());
}
}

@Timed
Expand Down Expand Up @@ -551,9 +554,12 @@ public Response delete(
throw new NotAllowedException("Forbidden operation on '" + META_COLLECTION_NAME + "'");
}
CollectionReference collectionReference = collectionReferenceService.getCollectionReference(collection,Optional.ofNullable(organisations));
collectionReferenceService.checkIfAllowedForOrganisations(collectionReference, Optional.ofNullable(organisations), true);
collectionReferenceService.deleteCollectionReference(collection);
return ResponseFormatter.getSuccessResponse("Collection " + collection + " deleted.");
if (collectionReferenceService.checkIfAllowedForOrganisations(collectionReference, Optional.ofNullable(organisations), true)) {
collectionReferenceService.deleteCollectionReference(collection);
return ResponseFormatter.getSuccessResponse("Collection " + collection + " deleted.");
} else {
throw new CollectionUnavailableException("The collection not available with organisation header: " + Optional.ofNullable(organisations).get());
}
}

private void removeMetacollection(List<CollectionReference> collectionReferences) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ public void loadCsw(long sleepAfter) throws IOException {
}

public void delete() throws IOException, ArlasException {
DataSetTool.clearDataSet();
//DELETE collection
when().delete(getUrlPath()).then().statusCode(200);
when().delete(arlasPath + "collections/" + COLLECTION_NAME_ACTOR).then().statusCode(200);
DataSetTool.clearDataSet();
}

public void deleteCsw() throws IOException, ArlasException {
Expand Down
Loading