2.6k GitHub stars and counting — OptimalBits/node_acl is a JavaScript project TopGit is tracking across repositories on the platform. Access control lists for node applications
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
This module provides a minimalistic ACL implementation inspired by Zend_ACL.
When you develop a web site or application you will soon notice that sessions are not enough to protect all the
available resources. Avoiding that malicious users access other users content proves a much more
complicated task than anticipated. ACL can solve this problem in a flexible and elegant way.
Create roles and assign roles to users. Sometimes it may even be useful to create one role per user,
to get the finest granularity possible, while in other situations you will give the asterisk permission
for admin kind of functionality.
A Redis, MongoDB and In-Memory based backends are provided built-in in the module. There are other third party backends such as knex based, firebase and elasticsearch. There is also an alternative memory backend that supports regexps.
Follow manast for news and updates regarding this library.
Status
Features
Users
Roles
Hierarchies
Resources
Express middleware for protecting resources.
Robust implementation with good unit test coverage.
Installation
Using npm:
npm install acl
Documentation
addUserRoles
removeUserRoles
userRoles
roleUsers
hasRole
addRoleParents
removeRoleParents
removeRole
removeResource
allow
removeAllow
allowedPermissions
isAllowed
areAnyRolesAllowed
whatResources
middleware
backend
Examples
Create your acl module by requiring it and instantiating it with a valid backend instance:
var acl = require('acl');
// Using redis backend
acl = new acl(new acl.redisBackend(redisClient, prefix));
// Or Using the memory backend
acl = new acl(new acl.memoryBackend());
// Or Using the mongodb backend
acl = new acl(new acl.mongodbBackend(dbInstance, prefix));
All the following functions return a promise or optionally take a callback with
an err parameter as last parameter. We omit them in the examples for simplicity.
Create roles implicitly by giving them permissions:
// guest is allowed to view blogs
acl.allow('guest', 'blogs', 'view')
// allow function accepts arrays as any parameter
acl.allow('member', 'blogs', ['edit', 'view', 'delete'])
Users are likewise created implicitly by assigning them roles:
acl.addUserRoles('joed', 'guest')
Hierarchies of roles can be created by assigning parents to roles:
acl.addRoleParents('baz', ['foo', 'bar'])
Note that the order in which you call all the functions is irrelevant (you can add parents first and assign permissions to roles later)
Sometimes is necessary to set permissions on many different roles and resources. This would
lead to unnecessary nested callbacks for handling errors. Instead use the following:
The middleware will protect the resource named by req.url, pick the user from req.session.userId and check the permission for req.method, so the above would be equivalent to something like this:
The middleware accepts 3 optional arguments, that are useful in some situations. For example, sometimes we
cannot consider the whole url as the resource:
userId {String|Number} User id.
rolename {String|Number} role name.
callback {Function} Callback called when finished.
addRoleParents( role, parents, function(err) )
Adds a parent or parent list to role.
Arguments
role {String} Child role.
parents {String|Array} Parent role(s) to be added.
callback {Function} Callback called when finished.
removeRoleParents( role, parents, function(err) )
Removes a parent or parent list from role.
If parents is not specified, removes all parents.
Arguments
role {String} Child role.
parents {String|Array} Parent role(s) to be removed [optional].
callback {Function} Callback called when finished [optional].
removeRole( role, function(err) )
Removes a role from the system.
Arguments
role {String} Role to be removed
callback {Function} Callback called when finished.
### removeResource( resource, function(err) )
Removes a resource from the system
Arguments
resource {String} Resource to be removed
callback {Function} Callback called when finished.
Adds the given permissions to the given roles over the given resources.
Arguments
roles {String|Array} role(s) to add permissions to.
resources {String|Array} resource(s) to add permisisons to.
permissions {String|Array} permission(s) to add to the roles over the resources.
callback {Function} Callback called when finished.
allow( permissionsArray, function(err) )
Arguments
permissionsArray {Array} Array with objects expressing what permissions to give.
[{roles:{String|Array}, allows:[{resources:{String|Array}, permissions:{String|Array}]]
callback {Function} Callback called when finished.
Checks if the given user is allowed to access the resource for the given
permissions (note: it must fulfill all the permissions).
Arguments
userId {String|Number} User id.
resource {String} resource to ask permissions for.
permissions {String|Array} asked permissions.
callback {Function} Callback called with the result.
Returns true if any of the given roles have the right permissions.
Arguments
roles {String|Array} Role(s) to check the permissions for.
resource {String} resource to ask permissions for.
permissions {String|Array} asked permissions.
callback {Function} Callback called with the result.
To create a custom getter for userId, pass a function(req, res) which returns the userId when called (must not be async).
Arguments
numPathComponents {Number} number of components in the url to be considered part of the resource name.
userId {String|Number|Function} the user id for the acl system (defaults to req.session.userId)
permissions {String|Array} the permission(s) to check for (defaults to req.method.toLowerCase())
backend( db, [prefix] )
Creates a backend instance. All backends except Memory require driver or database instance. useSingle is only applicable to the MongoDB backend.
Arguments
db {Object} Database instance
prefix {String} Optional collection prefix
useSingle {Boolean} Create one collection for all resources (defaults to false)
var mongodb = require('mongodb');
mongodb.connect("mongodb://127.0.0.1:27017/acltest", function(error, db) {
var mongoBackend = new acl.mongodbBackend(db, 'acl_');
});
Creates a new MongoDB backend using database instance db.
var client = require('redis').createClient(6379, '127.0.0.1', {no_ready_check: true});
var redisBackend = new acl.redisBackend(client);
Creates a new Redis backend using Redis client client.
Tests
Run tests with npm (requires mocha):
npm test
Future work
Support for denials (deny a role a given permission)
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
TopGit's last sync did not record any GitHub topics for OptimalBits/node_acl. GitHub topics appear in the right sidebar of a repository page; that's the authoritative place to check.
How active is development on OptimalBits/node_acl?
The most recent commit recorded on OptimalBits/node_acl was 3.0 years ago, based on the GitHub push timestamp. The repository has 372 forks — one of the better signals of community interest.
How many stars does OptimalBits/node_acl have?
OptimalBits/node_acl has 2.6k GitHub stars — refresh the page for the live number, or check github.com/OptimalBits/node_acl. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
What is OptimalBits/node_acl?
OptimalBits/node_acl (OptimalBits/node_acl) is a JavaScript project on GitHub. From the project's own README: Access control lists for node applications
What language is OptimalBits/node_acl written in?
OptimalBits/node_acl is written primarily in JavaScript. GitHub's language field is based on the largest share of bytes in the default branch.
Read full README in the tab above.
Curious whether node_acl is right for you?
Let ChatGPT, Claude, or Perplexity look into it — click below and see what AI actually says about node_acl.