Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@c8o/nimbus-core

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@c8o/nimbus-core - npm Package Compare versions

Comparing version 0.0.5 to 1.0.0

59

dist/Auth/AccessHandler.js

@@ -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",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc