role-provider v0.0.1
role-provider
A validator based role-management. Define some role-validators and validate permission-objects agains them.
Install
npm install --save role-provider
Example
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')
});Api
define(role, validator)
Defines a validator on a specific role name.
roleName of role.validatorFunction which returnstrueor some other value to confirm permission orfalseto deny. Could also return a Promise which could resolve withtrue, any value orfalse.
validateRole(role, [params])
Executes a defined validator and returns a promise which resolves with result or rejects if invalid.
roleName of role.paramsOptional array of parameters. Will be injected intovalidatorfunction.
expectEvery(roles, [params])
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.
rolesArray of role names.paramsOptional array of parameters. Will be injected intovalidatorfunction.
expectSome(roles, [params])
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.
rolesArray of role names.paramsOptional array of parameters. Will be injected intovalidatorfunction.
expectNone(roles, [params])
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.
rolesArray of role names.paramsOptional array of parameters. Will be injected intovalidatorfunction.
validate(permissions, [params])
Validates given permissions-object and returns promise which resolves if all permissions are given.
permissionsObject which should be validated.- `every` Array of `role`-names which need all to be valid. See [expectEvery](#expecteveryroles-params) - `none` Array of `role`-names which must not be valid. See [expectNone](#expectnomeroles-params) - `some` Array of `role`-names which should contain at least one valid role. See [exepctSome](#expectsomeroles-params)paramsOptional array of parameters. Will be injected intovalidatorfunction.
Author
License
9 years ago