js-jakkel

JavaScript ACL Implementation


Keywords
acl
License
MIT
Install
bower install js-jakkel

Documentation

Build Status Bower Version NPM Version

JS-Jakkel - JavaScript ACL Implementation

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.

Installation


$ bower install --save js-jakkel
$ npm install --save js-jakkel

How to use


Jakkel 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
});

Api


Jakkel(options)

Creates a new Jakkel instance

  • The options argument is optional and of type Json.
// Without options
var acl = new Jakkel;
// With options
var acl = new Jakkel( {strict: false} );

version()

Returns the library version of JS-Jakkel

var version = acl.version();

roles()

Returns the list of currently defined roles.

  • Return type is an Array of Role objects.
var roles = acl.roles();

role(roleName)

Returns a role from the currently defined roles.

  • The name argument is of type String.
  • Return type is a Role object.
var role = acl.role('admin');

addRole(roleName, parent)

Adds a role to the roles.

  • The name argument is of type String.
  • The parent argument is optional and of type String.
  • Returns true if succeeded.
acl.addRole('user');
acl.addRole('admin', 'user');

resources()

Returns the list of currently defined resources.

  • Return type is an Array of Resource objects.
var resources = acl.resources();

resource(resourceName)

Returns a resource from the currently defined resources.

  • The resourceName argument is of type String.
  • Return type is a Resource object.
var resource = acl.resource('products');

addResource(resourceName, actions)

Adds a resources to the resources. Actions are optional but can be used to have more control.

  • The resourceName argument is of type String.
  • The actions argument 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']);

allow(roleName, resourceName, actions)

Adds permission for a role to a resource

  • The roleName argument is of type String.
  • The resourceName argument is of type String.
  • The actions argument 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(roleName, resourceName, actions)

Deny a resource for a role

  • The roleName argument is of type String.
  • The resourceName argument is of type String.
  • The actions argument 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']);

isAllowed(roleName, resourceName, actions)

Returns true or false depending if the permission is found.

  • The roleName argument is of type String or Array of Strings.
  • The resourceName argument is of type String.
  • The actions argument 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']);

ifAllowed(roleName, resourceName, actions, onTrue, onFalse)

Executes the callback onTrue method if the role is allowed to the resource, otherwise callback onFalse is called.

  • The roleName argument is of type String or Array of Strings.
  • The resourceName argument is of type String.
  • The actions argument is optional and of type Array or String.
  • The onTrueargument is of type Function (callback)
  • The onFalse argument 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
});

config()

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"] }
            ] 
        }
    ]
}

setConfig(config)

Set the Jakkel configuration from a json object.

  • The config argument is of type Json.
  • Returns true if succeeded and the config is validated
// equivalents
var config = {...};

acl.setConfig(config);

Contributing


Found a typo or a bug? Send a pull request.

Todo


  • 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.

License


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.