A validator
based role-management. Define some role-validators
and validate permission
-objects agains them.
npm install --save role-provider
const RoleProvider = require('role-provider');
// Create instance
const roles = new RoleProvider();
// Exemplary user object
const user = {
id: 1,
active: true,
confirmedSince: '11-12-2013'
};
// Define role validators
roles.define('active', () => user.active);
roles.define('confirmed', () => user. confirmedSince);
// Validate a permission demand (success)
roles.validate({
every: ['active', 'confirmed']
}).then((res) => expect(res).to.equal({
active: true,
confirmed: '11-12-2013'
}));
// Validate a permission demand (deny)
roles.validate({
every: ['active'],
none: ['confirmed']
}).then(() => { throw new Error('Should not resolve'); }, (err) => {
expect(err).to.be.an.error('Permission for role \'confirmed\' should not be available')
});
Defines a validator
on a specific role name.
role
Name of role.validator
Function which returnstrue
or some other value to confirm permission orfalse
to deny. Could also return a Promise which could resolve withtrue
, any value orfalse
.
Executes a defined validator
and returns a promise which resolves with result or rejects if invalid.
role
Name of role.params
Optional array of parameters. Will be injected intovalidator
function.
Validates given roles
and returns a promise which resolves if all validations resolve. In case of first rejection result promise will reject. Every given role
needs to be valid.
roles
Array of role names.params
Optional array of parameters. Will be injected intovalidator
function.
Validates given roles
and returns a promise which resolves on the first resolving validator
result. It will reject if no role validator
resolves. At least one of the roles
needs to be valid.
roles
Array of role names.params
Optional array of parameters. Will be injected intovalidator
function.
Validates given roles
and returns a promise which resolves if no validator resolves. It will reject on first resolving validator
. No given role
needs to be valid.
roles
Array of role names.params
Optional array of parameters. Will be injected intovalidator
function.
Validates given permissions
-object and returns promise which resolves if all permissions
are given.
-
permissions
Object which should be validated.every
Array ofrole
-names which need all to be valid. See expectEverynone
Array ofrole
-names which must not be valid. See expectNonesome
Array ofrole
-names which should contain at least one valid role. See exepctSome
-
params
Optional array of parameters. Will be injected intovalidator
function.
- One time:
npm test
- Watch:
npm run test-watch