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-fast-n-zero-dependency

v2.0.3

Published

A data storage agnostic Role-Based-Access-Control (RBAC) manager with Big O(1) complexity and zero dependencies

Downloads

472

Readme

rbac-fast-n-zero-dependency

A data storage agnostic Role-Based-Access-Control (RBAC) manager with Big O(1) complexity and zero dependencies.

How to Use

Concept

  1. Add a role or use an existing role.
  2. Enable endpoints by defining a storage context: "root" | "group" | "user".
  3. Explicitly set (whitelist) permissions for each role per endpoint.
  4. Use your authorized user's groupId to query his/her permissions for the requested endpoint.
  5. If you want to use a DB for storing RBAC, then load your entries at app-initialization and update them at the same time as your DB updates.
  6. Optionally interpret the endpoint storage and access context if you have a multi-tenant system.

Installation

npm install rbac-fast-n-zero-dependency

Basic Usage


import RBAC from 'rbac-fast-n-zero-dependency'; //import the global singleton

// Add non-default roles / groups
RBAC.setRoleOnce('vendors');
RBAC.setRoleOnce('employees');

//choose an endpoint
const endpoint1 = "/users";

// enable endpoints by adding a storage context for the endpoint 
RBAC.setEndpointStorageContext(endpoint1, "root"); //"root" | "group" | "user"

// whitelist permissions per endpoint per role and define a selector a: "grant" | "mng" | "gid" | "uid" for related entries
RBAC.setPermissions("registered", endpoint1, {c:1, r:1, u:1, d:1, x:1, a:"uid"});

// getPermissions
RBAC.getPermissions("owner", endpoint1); //owner always has all permissions if the endpoint was enabled

const gid = RBAC.getGidOfRoleName("registered");
const permissions = RBAC.getPermissions(RBAC.getRoleNameOfGid(gid), endpoint1); //registered role permissions

// get multi-tenant options
const storageContext = RBAC.getEndpointStorageContext(endpoint1); //where to find the data-hierarchy of the endpoint

const canGrantAndManageEndpointData = (permissions.a  == "grant") ? true : false;
const canManageEndpointData = (permissions.a  == "mng") ? true : false;
const mustFilterAccessByUID = (permissions.a == "uid") ? true : false;
const mustFilterAccessByGID = (permissions.a  == "gid") ? true : false;

default roles

  • guest
  • owner (system owner)
  • admin (tech support)
  • contributor (content creator)
  • reader (content reader but not creator)
  • registered (self registered user)
  • tier1 (customer)
  • tier2 (customer)
  • tier3 (customer)
  • tier4 (customer)

API

RBAC

Imports the RBAC singleton.

setRoleOnce(role)

Adds a new role if it doesn't already exist.

  • role (string): The name of the role to add.

setEndpointStorageContext(endpoint, storageContext)

Sets the storage context for a specific endpoint.

  • endpoint (string): The endpoint to set the storageContext for.
  • storageContext (string): The type of storageContext ("root", "group", "user").

getEndpointStorageContext(endpoint)

Gets the storage context for a specific endpoint.

  • endpoint (string): The endpoint to get the storageContext for.

getPermissions(role, endpoint)

Gets the permissions for a specific role and endpoint.

  • role (string): The role to get permissions for.
  • endpoint (string): The endpoint to get permissions for.

setPermissions(role, endpoint, permissions)

Sets the permissions for a specific role and endpoint.

  • role (string): The role to set permissions for.
  • endpoint (string): The endpoint to set permissions for.
  • permissions (object): An object representing the permissions (c: create, r: read, u: update, d: delete, x: execute, a: accessSelector).

getGidOfRole(role)

Gets the group ID (GID) of a specific role.

  • role (string): The role to get the GID for.

getRoleNameOfGid(gid)

Gets the role name associated with a specific group ID (GID).

  • gid (number): The GID to get the role name for.

removeRole(role)

Removes a role from the RBAC system.

  • role (string): The role to remove.

removeEndpoint(endpoint)

Removes an endpoint from the RBAC system.

  • endpoint (string): The endpoint to remove.

Recent Updates

Version 2.0.0

  • Clarified multi-tenant functionality.

Version 1.0.3

  • Core RBAC functionality.