nrbac
Easy to use generic RBAC(Role-Based Access Control) for node.
Inspired by nconf !
Install
$ npm install nrbac --save
Example
var rbac = ;var async = ; async;
API Documentation
The top-level of nrbac
is an instance of thenrbac.Provider
abstracts this all for you into a simple API.
nrbac.Permission.create(permission, callback)
Creates permissions, permission
param can be an object consists of an action
and a resource
, or an array of objects.
nrbacPermission; nrbacPermission;
nrbac.Permission.get(action, resource)
Gets permission with the specified action
and resource
, return an instance of nrbac.PermissionModel
.
var createPostPermission = nrbacPermission;
nrbac.Permission.list()
Lists all permissions.
var permissions = nrbacPermission;
nrbac.Permission.destroy()
Deletes all permissions.
nrbacPermission;nrbacPermissionshouldbeempty;
nrbac.Role.create(role, callback)
Creates roles, role
param can be an object consists of a unique name
, or an array of objects.
nrbacRole; nrbacRole;
nrbac.Role.get(name)
Gets role with the specified name
, return an instance of nrbac.RoleModel
.
var admin = nrbacRole;
nrbac.Role.list()
Lists all roles.
var roles = nrbacRole;
nrbac.Role.destroy()
Deletes all roles.
nrbacRole;nrbacRoleshouldbeempty;
nrbac.PermissionModel
permission.update(updateObj, [callback])
Updates the permission instance.
var permission = nrbacPermission;permission;
permission.remove([callback])
Deletes the permission instance.
var permission = nrbacPermission;permission;
nrbac.RoleModel
role.grant(permissions, callback)
Grants permissions to the role. permissions
param can be an instance of nrbac.PermissionModel
, or an array of objects.
var createPostPermission = nrbacPermission;var admin = nrbacRole;admin;
role.can(action, resource)
Check if the role has the specified permission.
var createPostPermission = nrbacPermission;var admin = nrbacRole;admin;
role.update(updateObj, [callback])
Updates the role instance.
var role = nrbacRole;role;
role.remove([callback])
Deletes the role instance.
var role = nrbacRole;role;
nrbac.use(storage)
Use the specified storage.
nrbac;
nrbac.sync(callback)
Synchronous data between nrbac
and storage engine you are using.
var memoryStorage = Permission: action: 'read' resource: 'post' Role: name: 'admin' ;nrbac; nrbac; // if you create permissions or roles, or grant permissions to roles// you must call the `sync` method to synchronous the data to storage.nrbacRole;nrbac;
nrbac.list(callback)
Lists all data.
nrbac;
Storage Engines
Memory
A simple in-memory storage engine that stores a literal Object representation of the RBAC data.
var memoryStorage = ;nrbac; // you can specify the memory storage initial datavar memoryStorage = Permission: action: 'read' resource: 'post' Role: name: 'admin' ;
File
File storage engine allow you to read your RBAC data from .json
file, and data will be persisted to disk when a call to nrbac.sync()
is made.
MongoDB
A MongoDB-based storage engine.
SQL
A SQL-based storage engine, you can use MySQL, PostgreSQL, and SQLite3.
Run Tests
$ npm install$ npm test