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

@nicolabello/js-acl

v2.0.13

Published

Provides a lightweight and flexible access control list (ACL) implementation for privileges management.

Downloads

116

Readme

JS ACL

Provides a lightweight and flexible access control list (ACL) implementation for privileges management.

Inspired by Zend ACL.

Installation

Using npm

npm install @nicolabello/js-acl

Using yarn

yarn add @nicolabello/js-acl

Import

Using import (ES6 way, e.g. Angular, React)

import { Acl } from '@nicolabello/js-acl';

Using require (CommonJS way, e.g. Node.js)

const { Acl } = require('@nicolabello/js-acl');

Usage example

Create a new ACL

const acl = new Acl();

Add the roles to match the following structure

guest
authenticated
├── editor
└── admin
acl.addRole('guest');
acl.addRole('authenticated');
acl.addRole('editor', 'authenticated');
acl.addRole('admin', 'authenticated');

Add the resources to match the following structure

restricted-area
├── admin-area
└── articles
    └── created-articles
acl.addResource('restricted-area');
acl.addResource('admin-area', 'restricted-area');
acl.addResource('articles', 'restricted-area');
acl.addResource('created-articles', 'articles');

Allow/deny privileges

// Authenticated users can navigate to restricted area
acl.allow('authenticated', 'restricted-area', 'navigate');

// Admin only can navigate to admin area
acl.deny('authenticated', 'admin-area', 'navigate');
acl.allow('admin', 'admin-area', 'navigate');

// Editors can add articles and edit their own
acl.allow('editor', 'articles', 'add');
acl.allow('editor', 'created-articles', 'edit');

// Authenticated users can like all articles,
// but they can't like their own articles
acl.allow('authenticated', 'articles', 'like');
acl.deny('authenticated', 'created-articles', 'like');

Query privileges

acl.isAllowed('authenticated', 'restricted-area', 'navigate'); // true
acl.isAllowed('editor', 'restricted-area', 'navigate'); // true

acl.isAllowed('authenticated', 'admin-area', 'navigate'); // false
acl.isAllowed('admin', 'admin-area', 'navigate'); // true
acl.isAllowed('editor', 'admin-area', 'navigate'); // false

acl.isAllowed('authenticated', 'articles', 'add'); // false
acl.isAllowed('editor', 'articles', 'add'); // true

acl.isAllowed('authenticated', 'articles', 'like'); // true
acl.isAllowed('authenticated', 'created-articles', 'like'); // false
acl.isAllowed('editor', 'created-articles', 'like'); // false

Documentation

Create a new ACL

const acl = new Acl();

By default, you can use string or number types as roles, resources and privileges.

If you are using typescript then you can define your own types.

enum Roles {
  Guest,
  Authenticated,
  Editor,
  Admin,
}
enum Resources {
  RestrictedArea,
  AdminArea,
  Articles,
  CreatedArticles,
}
enum Privileges {
  Navigate,
  Add,
  Like,
}

const acl = new Acl<Roles, Resources, Privileges>();

Add roles

acl.addRole('guest');

Add a parent as second parameter to inherit all the parent's privileges

acl.addRole('editor', 'authenticated');

Use an array to add multiple roles at once, with or without parent

acl.addRole(['guest', 'authenticated']);
acl.addRole(['editor', 'publisher'], 'authenticated');

Add resources

acl.addResource('restricted-area');

Add a parent as second parameter to inherit all the parent's privileges

acl.addResource('created-articles', 'articles');

Use an array to add multiple resources at once, with or without parent

acl.addResource(['restricted-area', 'personal-area']);
acl.addResource(['admin-area', 'articles'], 'restricted-area');

Allow/deny privileges

By default, all privileges are denied if not explicitly allowed.

Allow/deny all privileges on all resources for all roles

acl.allow(null, null);
acl.deny(null, null);

Allow/deny a privilege on all resources for all roles

acl.allow(null, null, 'view');
acl.deny(null, null, 'view');

Allow/deny all privileges on all resources for a role

acl.allow('admin', null);
acl.deny('admin', null);

Allow/deny a privilege on all resources for a role

acl.allow('admin', null, 'edit');
acl.deny('admin', null, 'edit');

Allow/deny all privileges on a resource for all role

acl.allow(null, 'articles');
acl.deny(null, 'articles');

Allow/deny a privilege on a resource for all roles

acl.allow(null, 'articles', 'view');
acl.deny(null, 'articles', 'view');

Allow/deny all privileges on a resource for a role

acl.allow('admin', 'admin-area');
acl.deny('admin', 'admin-area');

Allow/deny a privilege on a resource for a role

acl.allow('admin', 'admin-area', 'navigate');
acl.deny('admin', 'admin-area', 'navigate');

Arrays can be used in any of the above to allow/deny multiple roles, resources and privileges

acl.allow(['admin', 'publisher'], null, ['add', 'remove']);
acl.deny(null, ['article', 'category'], ['add', 'remove']);

Query privileges

Query all privileges on all resources for all roles

acl.isAllowed(null, null);

Query a privilege on all resources for all roles

acl.isAllowed(null, null, 'view');

Query all privileges on all resources for a role

acl.isAllowed('admin', null);

Query a privilege on all resources for a role

acl.isAllowed('admin', null, 'edit');

Query all privileges on a resource for all roles

acl.isAllowed(null, 'articles');

Query a privilege on a resource for all roles

acl.isAllowed(null, 'articles', 'view');

Query all privileges on a resource for a role

acl.isAllowed('admin', 'admin-area');

Query a privilege on a resource for a role

acl.isAllowed('admin', 'admin-area', 'navigate');

License

Licensed under the terms of GNU General Public License Version 2 or later.