|
1 |
| -const { isSuperUser } = require('../models/members') |
2 |
| -module.exports = (req, res, next) => { |
3 |
| - try { |
4 |
| - const { username } = req.userData |
5 |
| - if (!isSuperUser(username)) { |
6 |
| - return res.boom.forbidden('You are not allowed to perform this action.') |
| 1 | +/** |
| 2 | + * Describe the priority order of roles. Any route requiring `x` role is allowed |
| 3 | + * for user having authority of role `x` or a higher authority. |
| 4 | + * For e.g |
| 5 | + * - Route requiring `superUser` role is only allowed for `super_user`. |
| 6 | + * - Route requiring `appOwner` role is allowed for `superUser` and `app_owner`. |
| 7 | + */ |
| 8 | +const REQUIRED_ROLES_PRIORITY = { |
| 9 | + superUser: ['super_user'], |
| 10 | + appOwner: ['app_owner', 'super_user'], |
| 11 | + default: ['default', 'super_user', 'app_owner'] |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Check if the user has enough authorization based on their role. |
| 16 | + * User would have following roles schema - |
| 17 | + * userRoles = {app_owner: true, super_user: true} |
| 18 | + * @param {String} requiredRole - Required role level to authorize request. |
| 19 | + * @param {Object} userRoles - Roles information of the current user. |
| 20 | + * @returns {Boolean} - Whether the current user is authorized for required role level. |
| 21 | + */ |
| 22 | +const userHasPermission = (requiredRole, userRoles) => { |
| 23 | + const allowedRoles = REQUIRED_ROLES_PRIORITY[`${requiredRole}`] || ['default'] |
| 24 | + return allowedRoles.some((role) => { |
| 25 | + return Boolean(userRoles[`${role}`]) |
| 26 | + }) |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Create an authorization middleware for a route based on the required role needed |
| 31 | + * for that route. Currently following roles are supported: |
| 32 | + * - `authorizeUser('superUser')` |
| 33 | + * - `authorizeUser('appOwner')` |
| 34 | + * Note: This must be added on routes after the `authenticate` middleware. |
| 35 | + * @param {String} requiredRole - The least role authority required for a route. |
| 36 | + * @returns {Function} - A middleware function that authorizes given role. |
| 37 | + */ |
| 38 | +const authorizeUser = (requiredRole) => { |
| 39 | + return (req, res, next) => { |
| 40 | + const { roles = {} } = req.userData |
| 41 | + // All users should have `default` role |
| 42 | + roles.default = true |
| 43 | + |
| 44 | + if (!userHasPermission(requiredRole, roles)) { |
| 45 | + return res.boom.unauthorized('You are not authorized for this action.') |
7 | 46 | }
|
8 | 47 | return next()
|
9 |
| - } catch (err) { |
10 |
| - logger.error(err) |
11 |
| - return res.boom.forbidden('You are not allowed to perform this action.') |
12 | 48 | }
|
13 | 49 | }
|
| 50 | + |
| 51 | +module.exports = { |
| 52 | + authorizeUser, |
| 53 | + userHasPermission |
| 54 | +} |
0 commit comments