drone-acl
v0.1.5
Published
access control list module
Downloads
7
Maintainers
Readme
ACL
An access control list connected to a database via knex module.
How to install
npm install --save drone-acl
How to use
const ACL = require('drone-acl');
const knex = require('knex');
(async function() {
const knexConn = knex({
client: 'pg',
connection: {
host: '127.0.0.1',
database: 'database_name',
user: '',
password: ''
},
migrations: {
tableName: 'ACL_migrations'
}
});
const acl = new ACL(knexConn);
// run required database migrations
await acl.migrate();
// create a permission
await acl.permissions.create('view-users', 'Permission to view users');
// create a role
const admin = await acl.roles.create('administrator');
// if role is already created you can use:
// const admin = acl.role('administrator');
// assign permissions to a role
await admin.allow('view-users');
await admin.hasPermission('view-users'); // true
})();
API
ACL
migrate(): Promise
Create required DB tables
acl.migrate();
role(name: String): RolePermission
Get a role to work with
const role = acl.role('role-name');
RolePermission
allow(permission: String|String[]): Promise
Add a permission or array of permissions to the role access
acl.role('role-name').allow('permission-name');
disallow(permission: String|String[]): Promise
Remove permission or array of permissions from the role access
acl.role('role-name').disallow('permission-name');
hasPermission(permission: String): Promise -> boolean
Verifiy if role has access to the permission
acl.role('role-name').hasPermission('permission-name');
can(permission: String): Promise -> boolean
Alias of hasPermission()
acl.role('role-name').can('permission-name');
Roles
create(name): Promise -> RolePermission
Create a new role
acl.roles.create('role-name-here');
update(name, changes): Promise
Update a role data
acl.roles.update('role-name-here', { name: 'new-role-name' });
delete(name): Promise
Delete a role by its name
acl.roles.delete('role-name-here');
findByName(name): Promise
Find a role by its name
acl.roles.findByName('role-name-here');
list(): Promise
List all available roles
acl.roles.list();
clear(): Promise
Remove all roles
acl.roles.clear();
Permissions
create(name): Promise
Create a new permission
acl.permissions.create('permission name', 'permission description');
update(name, changes): Promise
Update a permission data
acl.permissions.update('role-name-here', {
name: 'new-permission-name',
description: 'new-permission description'
});
delete(name): Promise
Delete a permission by its name
acl.permissions.delete('permission-name-here');
findByName(name): Promise
Find a permission by its name
acl.permissions.findByName('permission-name-here');
list(): Promise
List all available permissions
acl.permissions.list();
clear(): Promise
Remove all permissions
acl.permissions.clear();