JS-Jakkel is a JavaScript ACL implementation inspired by the Zend Framework. Its goal is to provide a consitent implemtation towards Node.js and Angular.js.
$ bower install --save js-jakkel$ npm install --save js-jakkelJakkel works with Roles and Resources.
// Create a jakkel instance
var acl = new Jakkel;
// Define some roles (that inherit each other)
acl.addRole('anonymous');
acl.addRole('user', 'anonymous');
acl.addRole('admin', 'user');
// Define some resources & actions
acl.addResource('products', ['view', 'add', 'update', 'delete']);
acl.addResource('comments', ['view', 'add', 'update', 'delete']);
// Set the permissions
acl.allow('anonymous', 'products', ['view']);
acl.allow('user', 'comments', ['view', 'add']);
acl.allow('admin', 'products', ['add', 'update', 'delete']);
acl.allow('admin', 'comments', ['update', 'delete']);
// Test permissions
if(acl.isAllowed('user', 'products', ['view'])) {
// render products...
};
// Test permissions
acl.ifAllowed('admin', 'comments', ['delete'], function() {
// show comment delete button
});Creates a new Jakkel instance
- The
optionsargument is optional and of type Json.
// Without options
var acl = new Jakkel;// With options
var acl = new Jakkel( {strict: false} );Returns the library version of JS-Jakkel
var version = acl.version();Returns the list of currently defined roles.
- Return type is an Array of Role objects.
var roles = acl.roles();Returns a role from the currently defined roles.
- The
nameargument is of type String. - Return type is a Role object.
var role = acl.role('admin');Adds a role to the roles.
- The
nameargument is of type String. - The
parentargument is optional and of type String. - Returns true if succeeded.
acl.addRole('user');
acl.addRole('admin', 'user');Returns the list of currently defined resources.
- Return type is an Array of Resource objects.
var resources = acl.resources();Returns a resource from the currently defined resources.
- The
resourceNameargument is of type String. - Return type is a Resource object.
var resource = acl.resource('products');Adds a resources to the resources. Actions are optional but can be used to have more control.
- The
resourceNameargument is of type String. - The
actionsargument is optional and of type String or Array. - Returns true if succeeded.
// equivalents
acl.addResource('profile');
acl.addResource('profile', '*');
acl.addResource('profile', ['*']);acl.addResource('products', ['list', 'detail', 'edit', 'delete', 'update']);Adds permission for a role to a resource
- The
roleNameargument is of type String. - The
resourceNameargument is of type String. - The
actionsargument is optional and of type String or Array. - Returns true if succeeded.
acl.allow('anonymous', 'products', 'list');
acl.allow('user', 'products', ['detail', 'comments']);
acl.allow('admin', 'products', ['add', 'delete', 'update']);// equivalents
acl.allow('user', 'profile');
acl.allow('user', 'profile', '*');
acl.allow('user', 'profile', ['*']);Deny a resource for a role
- The
roleNameargument is of type String. - The
resourceNameargument is of type String. - The
actionsargument is optional and of type String or Array. - Returns true if succeeded.
// user inherits from anonymous
acl.addRole('anonymous');
acl.addRole('user', 'anonymous');
// anonymous can login, user can logout
acl.allow('anonymous', 'auth', ['login','signup']);
acl.allow('user', 'auth', ['logout']);
// user can't acces login or signup
acl.deny('user', 'auth', ['login','signup']);Returns true or false depending if the permission is found.
- The
roleNameargument is of type String or Array of Strings. - The
resourceNameargument is of type String. - The
actionsargument is optional and of type Array or String. - Return true or false.
// equivalent
var state = acl.isAllowed('user', 'products', 'list');
var state = acl.isAllowed('user', 'products', ['list']);var state = acl.isAllowed('admin', 'products', ['list','detail']);// equivalent
var state = acl.isAllowed('anonymous', 'auth');
var state = acl.isAllowed('anonymous', 'auth', '*');
var state = acl.isAllowed('anonymous', 'auth', ['*']);// Array of roles
var state = acl.isAllowed(['user','admin'], 'products', ['list','detail']);Executes the callback onTrue method if the role is allowed to the resource, otherwise callback onFalse is called.
- The
roleNameargument is of type String or Array of Strings. - The
resourceNameargument is of type String. - The
actionsargument is optional and of type Array or String. - The
onTrueargument is of type Function (callback) - The
onFalseargument is optional and of type Function (callback)
acl.ifAllowed('user', 'products', 'list', function() {
// do something if allowed
});acl.ifAllowed('user', 'products', 'list',
function() {
// do something if allowed
},
function() {
// do something if not allowed
}
);// Array of roles
acl.ifAllowed(['user','admin'], 'products', 'list', function() {
// do something if allowed
});Return the Jakkel config
- Returns the config as Json
var config = acl.config();
// example contents of config variable
{
"options": [
{ "strict": true }
],
"roles": [
{ "name": "anonymous" },
{ "name": "user", "parent": "anonymous" },
{ "name": "admin", "parent": "user" }
],
"resources": [
{
"name": "auth",
"actions": [
{ "action": "login", "allow": ["anonymous"], "deny": ["user"] },
{ "action": "signup", "allow": ["anonymous"], "deny": ["user"] },
{ "action": "logout", "allow": ["user"] }
]
},
{
"name": "products",
"actions": [
{ "action": "list", "allow": ["user"] },
{ "action": "detail", "allow": ["user"] },
{ "action": "add", "allow": ["admin"] }
]
},
{
"name": "profile",
"actions": [
{ "action": "*", "allow": ["user"] }
]
}
]
}Set the Jakkel configuration from a json object.
- The
configargument is of type Json. - Returns true if succeeded and the config is validated
// equivalents
var config = {...};
acl.setConfig(config);Found a typo or a bug? Send a pull request.
- Add Options (strict,...)
- Add Strict mode
- Validate everything in strict mode
- Prevent roles and actions with the same name in strict mode
- Prevent adding a role if name is already used for a resource.
- Prevent adding a resource if name is already used for a role.
- Prevent adding a role if specified parent doesn't exist.
The MIT License (MIT)
Copyright (c) 2014 SoftBrewery
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.