Socket
Socket
Sign inDemoInstall

@cobuildlab/rbac

Package Overview
Dependencies
0
Maintainers
4
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @cobuildlab/rbac

Role-based access control (RBAC) refers to the idea of assigning permissions to users based on their role within an organization. It offers a simple, manageable approach to access management that is less prone to error than assigning permissions to users


Version published
Weekly downloads
396
increased by45.59%
Maintainers
4
Created
Weekly downloads
 

Readme

Source

Role-Based Access Control

Role-based access control (RBAC) refers to the idea of assigning permissions to users based on their role within an organization. It offers a simple, manageable approach to access management that is less prone to error than assigning permissions to users individually.

RBAC architecture conventions discussion

https://github.com/cobuildlab/conventions/issues/24


Goals:

  • create systematic, repeatable assignment of permissions

  • easily audit user privileges and correct identified issues

  • quickly add and change roles, as well as implement them across APIs

  • cut down on the potential for error when assigning user permissions

  • integrate third-party users by giving them pre-defined roles

  • more effectively comply with regulatory and statutory requirements for confidentiality and privacy

  • RBAC Model

Installation

  1. Run on your terminal the following command:
$ npm i @cobuildlab/rbac

Example

Basic use

  const RBACproject = new RBAC();

  // defined the rules
  RBACproject.createRule('admin', 'dashboard', true, 'Access granted');
  RBACproject.createRule('manager', 'dashboard', false, 'Access denied');

 
  RBACproject.check('admin', 'dashboard') // [true, 'Access granted']

Declaring dynamic rules

  const RBACdynamic = new RBAC();

  // defined dynamic rules
  RBACdynamic.createRule('admin', 'dashboard', (data: any) => {
    const result = data.id === testData.id;
    const message = result ? 'Access granted' : 'Access denied';
    return [result, message];
  });

  const testData = { id: 'test' }; // fake data
  RBACdynamic.check('admin', 'dashboard', testData)

Set default role

  const defaultRole = new RBAC();

  defaultRole.createRule('admin', 'dashboard', false, 'Access granted');
  defaultRole.setDefaultRole('admin');

  rule.check(null, 'dashboard') // [false, 'Access granted']

Strict Type

  enum Roles {
    ADMIN = 'ADMIN',
    MANAGER = 'MANAGER',
  }

  enum Permissions {
    DASHBOARD = 'DASHBOARD',
  }

  const defaultRole = new RBAC(Roles, Permissions);
  defaultRole.createRule(Roles.ADMIN, Permissions.DASHBOARD, false, 'Access granted');

  rule.check(Roles.ADMIN, 'dashboard') // [false, 'Access granted']

Integration with React

const RBAC = new RBAC();
RBAC.createRule('admin', 'AGENT_ADMIN_USER_DETAILS', false, 'Access granted');

const RoleAuthorization = ({
  render,
  error,
  permission,
}) => {
  const [canRender, message] = RBAC.check('admin', permission, data);
  if (canRender) {
    return render(message);
  }
  return error ? error(message) : null;
};

const MyComponent = () => (
  <RoleAuthorization
    permission={'AGENT_ADMIN_USER_DETAILS'}
    render={() => <UserDetialsView />}
    error={() => <div>You dont have permission</div>}
  />
);

FAQs

Last updated on 27 May 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc