npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rbac-package

v1.0.1

Published

An RBAC (Role-Based Access Control) example using Express.js

Downloads

4

Readme

RBAC.js - Role-Based Access Control for Node.js

Table of Contents

  1. Introduction
  2. Installation
  3. Usage
  4. Core Concepts
    • Roles
    • Permissions
    • Resources
  5. Package Features
    • Role Management
    • Permission Management
    • Middleware for Access Control
  6. Examples
    • Basic Example
    • Advanced Example with Role Inheritance
  7. Extending RBAC
    • Dynamic Role and Permission Management
    • Database Integration
  8. License

Introduction

RBAC.js is a lightweight and flexible Role-Based Access Control (RBAC) system for Node.js. This package helps you manage access control based on user roles and permissions. Instead of assigning permissions directly to users, they are assigned to roles, and users are assigned to roles. This simplifies managing access control in large applications by centralizing the logic around roles.

Key Features:

  • Simple to use with an intuitive API.
  • Middleware to protect routes.
  • Role inheritance (e.g., Admin can inherit Moderator permissions).
  • Customizable permission checks.

Installation

To install RBAC.js, use npm:

npm install rbac-js

Usage

  1. Defining Roles and Permissions: Define roles and permissions centrally, as shown below:

    const roles = {
      Admin: { permissions: ['read', 'write', 'delete'] },
      User: { permissions: ['read'] },
      Moderator: { permissions: ['read', 'delete'] }
    };
  2. Assigning Roles to Users:

    const mockUsers = {
      admin: { roles: ['Admin'] },
      user: { roles: ['User'] },
      moderator: { roles: ['Moderator'] },
      multiRoleUser: { roles: ['User', 'Admin'] }
    };
  3. Protecting Routes: Use the rbacMiddleware to protect your routes:

    app.delete('/posts/:id', rbacMiddleware('delete', 'posts'), (req, res) => {
      res.send(`Post with ID ${req.params.id} deleted`);
    });

Core Concepts

  • Roles: Represent a user’s responsibilities (e.g., Admin, User).
  • Permissions: Define actions a role can perform (e.g., read, write).
  • Resources: Objects or data that permissions apply to (e.g., posts, users).

Package Features

  • Role Management: Create, update, and delete roles.
  • Permission Management: Assign specific permissions to roles based on actions like read, write, delete.
  • Middleware for Access Control: Protect routes using RBAC middleware.
  • Role Inheritance: Optional feature where roles can inherit permissions from other roles.

Examples

Basic Example

const roles = {
  Admin: { permissions: ['read', 'write', 'delete'] },
  User: { permissions: ['read'] }
};

app.delete('/posts/:id', rbacMiddleware('delete', 'posts'), (req, res) => {
  res.send('Post deleted');
});

Advanced Example with Role Inheritance

const roles = {
  SuperAdmin: { inherits: ['Admin'], permissions: ['manage-all'] },
  Admin: { inherits: ['User'], permissions: ['read', 'write', 'delete'] },
  User: { permissions: ['read'] }
};

app.post('/manage-users', rbacMiddleware('manage-all', 'users'), (req, res) => {
  res.send('User management actions');
});

Extending RBAC

  • Dynamic Role and Permission Management: Use a database to manage roles and permissions.
  • Database Integration: Store roles, users, and permissions in a database such as MongoDB or PostgreSQL.

License

This project is licensed under the MIT License.