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

express-dynacl

v2.1.1

Published

Express dynamic access control list, that allows to grant access to queries based on request details

Downloads

10

Readme

express-dynacl

express-dynacl is a simple ExpressJS dynamic access control list middleware, that allows to grant access to queries based on request details.

Using express-dynacl

roles.js:

module.exports = {
  "guest": {
    can: {
      "posts:list": true,
      "posts:edit": false
    }
  },

  "user": {
    can: {
      "posts:create": true,
      "posts:edit": (req,params) => Post.findOne({_id:params.post.id}).then(post => post.owner === req.user.id)
    },
    inherits: ["guest"]
  },

  "moderator":{
    can: {
      "posts:edit": true
    },
    inherits: ["user"]
  },

  "admin": {
    admin: true
  }
}

config.js:


var acl = require("express-dynacl");

var Post = require("./models/post");

var roles = require("./roles.js");

var options = {

  roles: roles,
  
  userRoles: req => req.user ? req.user.roles : [], // get user roles
  
  // set some of the roles as default - each request will expect that user has these roles (default is none)
  defaultRole: "guest",
  
  logString: (event) => `DynACL ${event.permission ? "OK" : "XX"} (action: ${event.action}${event.role ? ", role: " + event.role : ""}${Object.keys(event.params) > 0 ? ", params: " + JSON.stringify(event.params) : ""})`,
  logConsole: true, // enable logging to console (default is false)
  
  authorized: (req,res,next) => next(), // middleware to use when authorized (default is send to next middleware)
  unauthorized: (req,res,next) => res.sendStatus(401) // middleware to use when unauthorized (default is to respond with 401
}

Use as middleware:

var express = require('express');
var app = express();

var acl = require("express-dynacl");
var aclConfig = require("./config.js");

acl.config(aclConfig);

app.get("/posts", acl("posts:list"), (req,res) => {
	// list posts
});

app.post("/posts", acl("posts:create"), (req,res) => {
	// create post
});

app.put("/posts/1", acl("posts:edit"), (req,res) => {
	// edit post
});

Use inside request:

var express = require('express');
var app = express();

var acl = require("express-dynacl");

app.put("/posts/:id", (req,res) => {
	if(acl.can("posts:edit", req, {post: {id: req.params.id}})) {
   // edit post
  }

});

Inspect function

node node_modules/express-dynacl inspect roles.js

Running this will show a tree of actions split by colon with colored names of roles

TODO

  • logging to file