adapt-authoring-api 1.1.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -28,5 +28,14 @@
28
28
  "NO_SCHEMA_DEF": {
29
29
  "description": "No json schema has been defined",
30
30
  "statusCode": 404
31
+ },
32
+ "TOO_MANY_RESULTS": {
33
+ "data": {
34
+ "actual": "Number of actual results",
35
+ "expected": "Number of expected results",
36
+ "query": "The query"
37
+ },
38
+ "description": "Too many results were returned",
39
+ "statusCode": 500
31
40
  }
32
41
  }
@@ -514,13 +514,15 @@ class AbstractApiModule extends AbstractModule {
514
514
  * @return {Promise} Rejects if access should be blocked
515
515
  */
516
516
  async checkAccess (req, data) {
517
+ if (req.auth.isSuper) {
518
+ return data
519
+ }
517
520
  const isArray = Array.isArray(data)
518
521
  const filtered = []
519
522
  let error
520
523
  await Promise.allSettled((isArray ? data : [data]).map(async r => {
521
524
  try {
522
- if (!this.accessCheckHook.hasObservers || req.auth.isSuper ||
523
- (await this.accessCheckHook.invoke(req, r)).some(Boolean)) {
525
+ if (!this.accessCheckHook.hasObservers || (await this.accessCheckHook.invoke(req, r)).some(Boolean)) {
524
526
  filtered.push(r)
525
527
  }
526
528
  } catch (e) {
@@ -566,6 +568,24 @@ class AbstractApiModule extends AbstractModule {
566
568
  return this.cache.get(q, options, mongoOptions)
567
569
  }
568
570
 
571
+ /**
572
+ * Retrieves a single document from the DB. If multiple results are returned from the query, an error is thrown.
573
+ * @param {Object} query Attributes to use to filter DB documents
574
+ * @param {FindOptions} options Function options
575
+ * @param {external:MongoDBFindOptions} mongoOptions Options to be passed to the MongoDB function
576
+ * @return {Promise} Resolves with the single DB document
577
+ */
578
+ async findOne (query, options = {}, mongoOptions = {}) {
579
+ const results = await this.find(query, options, mongoOptions)
580
+ if (results.length > 1) {
581
+ throw this.app.errors.TOO_MANY_RESULTS.setData({ actual: results.length, expected: 1, query })
582
+ }
583
+ if (options.strict !== false && !results.length) {
584
+ throw this.app.errors.NOT_FOUND.setData({ id: query, type: options.schemaName })
585
+ }
586
+ return results[0]
587
+ }
588
+
569
589
  /**
570
590
  * Updates an existing document in the DB
571
591
  * @param {Object} query Attributes to use to filter DB documents
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adapt-authoring-api",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "description": "Abstract module for creating APIs",
5
5
  "homepage": "https://github.com/adapt-security/adapt-authoring-api",
6
6
  "license": "GPL-3.0",