@c8o/nimbus-core
Advanced tools
Comparing version 0.0.5 to 1.0.0
@@ -1,10 +0,11 @@ | ||
const Roles = require('../Models/Roles') | ||
const JwtHandler = require('./JwtHandler') | ||
const Permissions = require('../Models/Permissions') | ||
const RolesPermissions = require('../Models/RolesPermissions') | ||
class AccessHandler { | ||
/** | ||
* Determines if the user has the role needed to perform the action. | ||
* Determines if the user has the permission needed to perform the action. | ||
* | ||
* @param {string} token | ||
* @param {string} requiredRoleName | ||
* @param {string} permission | ||
* | ||
@@ -15,13 +16,13 @@ * @throws {AccessControlException} | ||
*/ | ||
static async hasRole(token, requiredRoleName) { | ||
static async hasAccess(token, permission) { | ||
try { | ||
// const tokenObject = await JwtHandler.validate(token) | ||
// const requiredRoleId = await this.getRoleIdFromRoleName(requiredRoleName) | ||
// const userRolesObject = await this.getAllUserRoles(tokenObject.user.id) | ||
const tokenObject = await JwtHandler.validate(token) | ||
const userPermissions = await this.getPermissions(tokenObject.user.role) | ||
const requiredPermission = await this.getPermissionIdFromName(permission) | ||
// for (let i in userRolesObject) { | ||
// if (userRolesObject[i].role_id === requiredRoleId._id) { | ||
// return true | ||
// } | ||
// } | ||
for (let i in userPermissions) { | ||
if (userPermissions[i].permission_id === requiredPermission._id) { | ||
return true | ||
} | ||
} | ||
} catch (error) { | ||
@@ -35,3 +36,3 @@ return error.type | ||
/** | ||
* Gets all the roles assigned to the user. | ||
* Gets all the permissions assigned to the users role. | ||
* | ||
@@ -44,14 +45,14 @@ * @param {string} id | ||
*/ | ||
// static async getAllUserRoles(id) { | ||
// try { | ||
// return await UserRoles.find({ user_id: id }) | ||
// } catch (error) { | ||
// throw new Error(error) | ||
// } | ||
// } | ||
static async getPermissions(id) { | ||
try { | ||
return await RolesPermissions.find({ role_id: id }) | ||
} catch (error) { | ||
throw new Error(error) | ||
} | ||
} | ||
/** | ||
* Gets the role id from the role name. | ||
* Gets the permission id from the permission name. | ||
* | ||
* @param {string} roleName | ||
* @param {string} name | ||
* | ||
@@ -62,11 +63,11 @@ * @throws {Error} | ||
*/ | ||
// static async getRoleIdFromRoleName(roleName) { | ||
// try { | ||
// return await Roles.findOne({ name: roleName }) | ||
// } catch (error) { | ||
// throw new Error(error) | ||
// } | ||
// } | ||
static async getPermissionIdFromName(name) { | ||
try { | ||
return await Permissions.findOne({ name: name }) | ||
} catch (error) { | ||
throw new Error(error) | ||
} | ||
} | ||
}; | ||
module.exports = AccessHandler |
@@ -33,3 +33,3 @@ const Utils = require('./Utilities') | ||
return await controller[method]( | ||
return await (new controller)[method]( | ||
request, response, | ||
@@ -36,0 +36,0 @@ ) |
const AccessHandler = require('./Auth/AccessHandler') | ||
const RoleMapping = { | ||
GET: process.env.ROLE_MAPPING_GET, | ||
POST: process.env.ROLE_MAPPING_POST, | ||
PATCH: process.env.ROLE_MAPPING_PATCH, | ||
DELETE: process.env.ROLE_MAPPING_DELETE, | ||
const PermissionMapping = { | ||
GET: process.env.PERMISSION_GET, | ||
POST: process.env.PERMISSION_POST, | ||
PATCH: process.env.PERMISSION_PATCH, | ||
DELETE: process.env.PERMISSION_DELETE, | ||
} | ||
@@ -32,5 +32,5 @@ | ||
let accessControlResult = await AccessHandler.hasRole( | ||
let accessControlResult = await AccessHandler.hasAccess( | ||
Request.headers.Authorization, | ||
RoleMapping[Request.method], | ||
PermissionMapping[Request.method], | ||
) | ||
@@ -37,0 +37,0 @@ |
{ | ||
"name": "@c8o/nimbus-core", | ||
"version": "0.0.5", | ||
"version": "1.0.0", | ||
"description": "Nimbus core package", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 4 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 4 instances in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
12035
16
475