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

node-access-control

v1.2.0

Published

This library provides a minimalistic ACL implementation with no connection to a database.

Downloads

4

Readme

Node ACL

This library provides a minimalistic ACL implementation with no connection to a database.

Installation

$ npm install node-access-control --save

Define the authorisations

It's recommended to deny access to all, then allows the routes for some specific roles. Note that an user is by default considered as authenticated if it contains an id.

const acl = require('node-access-control');  
  
acl.denyAll();
acl.add(['admin', 'developer'], 'any' , '.*', 'allow');
acl.add(['authenticated', 'marketing'], 'GET' , '/api/cases/.*', 'allow');
acl.add(['authenticated', 'marketing'], 'POST' , '/api/cases/.*', 'allow');
acl.add(['marketing'], 'DELETE' , '/api/cases/.*', 'deny');

Check the access

Checks the access by getting the user roles

const user = {
    ...
    roles: ['marketing'],
  };
acl.can(user, 'GET', '/api/cases/123-abc/comments/456-abc') // return true
acl.can(user, 'GET', '/api/cases/123-abc') // return true
acl.can(user, 'POST', '/api/cases/123-abc') // return true
acl.can(user, 'DELETE', '/api/cases/123-abc') // return false
//this user will be considered as authenticated because it contains an id
const user = {
    id: 34,
    roles: []
};
acl.can(user, 'GET', '/api/cases/123-abc') // return true
acl.can(user, 'POST', '/api/cases/123-abc') // return true
acl.can(user, 'DELETE', '/api/cases/123-abc') // return false
//this user will NOT be considered as authenticated because it does not contains any id
const user = {
    roles: []
};
acl.can(user, 'GET', '/api/cases/123-abc') // return false
acl.can(user, 'POST', '/api/cases/123-abc') // return false
acl.can(user, 'DELETE', '/api/cases/123-abc') // return false

Custom user structure


acl.setRolesGetter(user => {
    const roles = [];
    if (user.admin) {
      roles.push('admin');
    }
    if (user.developer) {
      roles.push('developer');
    }
    return roles;
});

const user = {
    ...
    admin: true,
    developer: true,
};

acl.can(user, 'GET', '/api/cases') // return true

Denies all

Checks the access by getting the user roles

acl.denyAll();
  
//this is the same as 
acl.add(['any'], 'any' , '.*', 'deny');

Documentation

add(roles, verb, url, permission)

Adds a specific access control.

roles {Array} Any roles that you want. The role 'any' and 'authenticated' already exist
verb {string} GET|POST|PATCH|PUT|DELETE|any (wildcard)
url {string} RegExp route
permission {string} allow|deny

can(user, verb, url)

Returns if the user have access to a specific route.

user {object} User containing the roles
verb {string} GET|POST|PATCH|PUT|DELETE|any (wildcard)
url {string} Route to test

setRolesGetter(method)

Defines a custom method to retrieve the user roles

method {Function} Function called when acl needs to retrieve the user roles

denyAll()

Denies all routes for all users