expressa 2.0.14 → 2.0.15

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.
@@ -2,15 +2,9 @@ const util = require('../util')
2
2
  let authenticatedRoleExists
3
3
 
4
4
  async function addRolePermissions (req, roles) {
5
- req.permissions = req.permissions || {}
6
- const roleDocs = await Promise.all(roles.map((name) => req.db.role.get(name)))
7
- roleDocs.forEach((roleDoc) => {
8
- for (const permission in roleDoc.permissions) {
9
- if (roleDoc.permissions[permission]) {
10
- req.permissions[permission] = true
11
- }
12
- }
13
- })
5
+ const rolePermissions = await util.getEffectivePermissionsForRoles(roles, req.db)
6
+ req.permissions ??= {}
7
+ req.permissions = { ...req.permissions, ...rolePermissions }
14
8
  }
15
9
 
16
10
  async function doesAuthenticatedRoleExist(req) {
@@ -31,7 +25,7 @@ module.exports.addRolePermissionsAsync = async function addRolePermissionsMiddle
31
25
  req.hasPermission = () => true
32
26
  return
33
27
  }
34
- req.hasPermission = (permission) => req.permissions && req.permissions[permission]
28
+ req.hasPermission = (permission) => util.hasPermission(req.permissions, permission)
35
29
  let roles = ['Anonymous']
36
30
  const isAuthenticatedRole = await doesAuthenticatedRoleExist(req)
37
31
  if (req.uid) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expressa",
3
- "version": "2.0.14",
3
+ "version": "2.0.15",
4
4
  "description": "API framework using JSON schemas",
5
5
  "main": "index.js",
6
6
  "repository": "https://github.com/thomas4019/expressa",
package/util.js CHANGED
@@ -499,6 +499,23 @@ exports.getDatabaseOffset = function getDatabaseOffset(page, itemsPerPage) {
499
499
  return (page - 1) * itemsPerPage
500
500
  }
501
501
 
502
+ exports.hasPermission = function hasPermission(permissions, permission) {
503
+ return permissions && permissions[permission]
504
+ }
505
+
506
+ exports.getEffectivePermissionsForRoles = async function getEffectivePermissionsForRoles(roles, db) {
507
+ const permissions = {}
508
+ const roleDocs = await Promise.all(roles.map((name) => db.role.get(name)))
509
+ roleDocs.forEach((roleDoc) => {
510
+ for (const permission in roleDoc.permissions) {
511
+ if (roleDoc.permissions[permission]) {
512
+ permissions[permission] = true
513
+ }
514
+ }
515
+ })
516
+ return permissions
517
+ }
518
+
502
519
  exports.createHash = auth.createHash
503
520
  exports.isHashed = auth.isHashed
504
521
  exports.doLogin = auth.doLogin