@@ -315,6 +315,11 @@ const extractFields = function (params) {
315
315
return fieldRules
316
316
}
317
317
318
+ /**
319
+ * Parse a string or array of IDs into an array of strings or undefined.
320
+ * @param {string | string[] | undefined } ids - The IDs parameter to parse
321
+ * @returns {string[] | undefined } Parsed array of ID strings or undefined
322
+ */
318
323
const parseIds = function ( ids ) {
319
324
let idsRules
320
325
if ( ids ) {
@@ -337,14 +342,28 @@ const extractIds = function (params) {
337
342
338
343
const extractAllowedCollectionIds = function ( params ) {
339
344
return process . env [ 'ENABLE_COLLECTIONS_AUTHX' ] === 'true'
340
- ? parseIds ( params . _collections )
345
+ ? parseIds ( params . _collections ) || [ ]
341
346
: undefined
342
347
}
343
348
344
349
const extractCollectionIds = function ( params ) {
345
350
return parseIds ( params . collections )
346
351
}
347
352
353
+ const filterAllowedCollectionIds = function ( allowedCollectionIds , specifiedCollectionIds ) {
354
+ return (
355
+ Array . isArray ( allowedCollectionIds ) && ! allowedCollectionIds . includes ( '*' )
356
+ ) ? allowedCollectionIds . filter (
357
+ ( x ) => ! specifiedCollectionIds || specifiedCollectionIds . includes ( x )
358
+ ) : specifiedCollectionIds
359
+ }
360
+
361
+ const isCollectionIdAllowed = function ( allowedCollectionIds , collectionId ) {
362
+ return ! Array . isArray ( allowedCollectionIds )
363
+ || allowedCollectionIds . includes ( collectionId )
364
+ || allowedCollectionIds . includes ( '*' )
365
+ }
366
+
348
367
export const parsePath = function ( inpath ) {
349
368
const searchFilters = {
350
369
root : false ,
@@ -577,9 +596,7 @@ const searchItems = async function (collectionId, queryParameters, backend, endp
577
596
const ids = extractIds ( queryParameters )
578
597
const allowedCollectionIds = extractAllowedCollectionIds ( queryParameters )
579
598
const specifiedCollectionIds = extractCollectionIds ( queryParameters )
580
- const collections = allowedCollectionIds ? allowedCollectionIds . filter (
581
- ( x ) => ! specifiedCollectionIds || specifiedCollectionIds . includes ( x )
582
- ) : specifiedCollectionIds
599
+ const collections = filterAllowedCollectionIds ( allowedCollectionIds , specifiedCollectionIds )
583
600
const limit = extractLimit ( queryParameters ) || 10
584
601
const page = extractPage ( queryParameters )
585
602
@@ -715,9 +732,28 @@ const aggregate = async function (
715
732
const ids = extractIds ( queryParameters )
716
733
const allowedCollectionIds = extractAllowedCollectionIds ( queryParameters )
717
734
const specifiedCollectionIds = extractCollectionIds ( queryParameters )
718
- const collections = allowedCollectionIds ? allowedCollectionIds . filter (
719
- ( x ) => ! specifiedCollectionIds || specifiedCollectionIds . includes ( x )
720
- ) : specifiedCollectionIds
735
+ const collections = filterAllowedCollectionIds ( allowedCollectionIds , specifiedCollectionIds )
736
+
737
+ if ( Array . isArray ( collections ) && ! collections . length ) {
738
+ if ( collectionId ) {
739
+ return new NotFoundError ( )
740
+ }
741
+
742
+ return {
743
+ aggregations : [ ] ,
744
+ links : [
745
+ {
746
+ rel : 'self' ,
747
+ type : 'application/json' ,
748
+ href : `${ endpoint } /aggregate`
749
+ } ,
750
+ {
751
+ rel : 'root' ,
752
+ type : 'application/json' ,
753
+ href : `${ endpoint } `
754
+ } ]
755
+ }
756
+ }
721
757
722
758
const searchParams = pickBy ( {
723
759
datetime,
@@ -991,7 +1027,7 @@ const validateAdditionalProperties = (queryables) => {
991
1027
992
1028
const getCollectionQueryables = async ( collectionId , backend , endpoint , queryParameters ) => {
993
1029
const allowedCollectionIds = extractAllowedCollectionIds ( queryParameters )
994
- if ( allowedCollectionIds && ! allowedCollectionIds . includes ( collectionId ) ) {
1030
+ if ( ! isCollectionIdAllowed ( allowedCollectionIds , collectionId ) ) {
995
1031
return new NotFoundError ( )
996
1032
}
997
1033
@@ -1008,8 +1044,7 @@ const getCollectionQueryables = async (collectionId, backend, endpoint, queryPar
1008
1044
}
1009
1045
1010
1046
const getCollectionAggregations = async ( collectionId , backend , endpoint , queryParameters ) => {
1011
- const allowedCollectionIds = extractAllowedCollectionIds ( queryParameters )
1012
- if ( allowedCollectionIds && ! allowedCollectionIds . includes ( collectionId ) ) {
1047
+ if ( ! isCollectionIdAllowed ( extractAllowedCollectionIds ( queryParameters ) , collectionId ) ) {
1013
1048
return new NotFoundError ( )
1014
1049
}
1015
1050
@@ -1155,7 +1190,7 @@ const getCollections = async function (backend, endpoint, queryParameters) {
1155
1190
1156
1191
const allowedCollectionIds = extractAllowedCollectionIds ( queryParameters )
1157
1192
const collections = collectionsOrError . filter (
1158
- ( c ) => ! allowedCollectionIds || allowedCollectionIds . includes ( c . id )
1193
+ ( c ) => isCollectionIdAllowed ( allowedCollectionIds , c . id )
1159
1194
)
1160
1195
1161
1196
for ( const collection of collections ) {
@@ -1194,8 +1229,7 @@ const getCollections = async function (backend, endpoint, queryParameters) {
1194
1229
}
1195
1230
1196
1231
const getCollection = async function ( collectionId , backend , endpoint , queryParameters ) {
1197
- const allowedCollectionIds = extractAllowedCollectionIds ( queryParameters )
1198
- if ( allowedCollectionIds && ! allowedCollectionIds . includes ( collectionId ) ) {
1232
+ if ( ! isCollectionIdAllowed ( extractAllowedCollectionIds ( queryParameters ) , collectionId ) ) {
1199
1233
return new NotFoundError ( )
1200
1234
}
1201
1235
@@ -1224,8 +1258,7 @@ const createCollection = async function (collection, backend) {
1224
1258
}
1225
1259
1226
1260
const getItem = async function ( collectionId , itemId , backend , endpoint , queryParameters ) {
1227
- const allowedCollectionIds = extractAllowedCollectionIds ( queryParameters )
1228
- if ( allowedCollectionIds && ! allowedCollectionIds . includes ( collectionId ) ) {
1261
+ if ( ! isCollectionIdAllowed ( extractAllowedCollectionIds ( queryParameters ) , collectionId ) ) {
1229
1262
return new NotFoundError ( )
1230
1263
}
1231
1264
@@ -1279,8 +1312,7 @@ const deleteItem = async function (collectionId, itemId, backend) {
1279
1312
}
1280
1313
1281
1314
const getItemThumbnail = async function ( collectionId , itemId , backend , queryParameters ) {
1282
- const allowedCollectionIds = extractAllowedCollectionIds ( queryParameters )
1283
- if ( allowedCollectionIds && ! allowedCollectionIds . includes ( collectionId ) ) {
1315
+ if ( ! isCollectionIdAllowed ( extractAllowedCollectionIds ( queryParameters ) , collectionId ) ) {
1284
1316
return new NotFoundError ( )
1285
1317
}
1286
1318
0 commit comments