Skip to content

Commit

Permalink
Feat: refactor checkIfAllowedForOrganisations
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbet committed Feb 26, 2025
1 parent c0fc153 commit 33a2f10
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,18 @@ public List<CollectionReference> getAllCollectionReferences(Optional<String> col
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,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 +145,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 +404,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 +436,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 +454,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 +467,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());
LOGGER.warn("The collection not available with organisation header: " + organisations.get());
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

0 comments on commit 33a2f10

Please sign in to comment.