Git Product home page Git Product logo

role-provider's Introduction

role-provider

A validator based role-management. Define some role-validators and validate permission-objects agains them.

Build Status Current Version

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.

  • role Name of role.
  • validator Function which returns true or some other value to confirm permission or false to deny. Could also return a Promise which could resolve with true, any value or false.

validateRole(role, [params])

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 into validator function.

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.

  • roles Array of role names.
  • params Optional array of parameters. Will be injected into validator function.

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.

  • roles Array of role names.
  • params Optional array of parameters. Will be injected into validator function.

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.

  • roles Array of role names.
  • params Optional array of parameters. Will be injected into validator function.

validate(permissions, [params])

Validates given permissions-object and returns promise which resolves if all permissions are given.

  • permissions Object which should be validated.

    • every Array of role-names which need all to be valid. See expectEvery
    • none Array of role-names which must not be valid. See expectNone
    • some Array of role-names which should contain at least one valid role. See exepctSome
  • params Optional array of parameters. Will be injected into validator function.




Run tests

  • One time: npm test
  • Watch: npm run test-watch

Author

@platdesign

role-provider's People

Contributors

platdesign avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.