Coder Social home page Coder Social logo

kontrolle's Introduction

Overview

Coverage

This module works by defining a set of roles, permissions and features single user has and checking to see if a said user possesses a certain capability, either by checking if specific role exists or feature is available. This allows you to determine whether a user should have access to a given resource or operation. Features can be extended with specific variations (like alpha or beta) that can be further checked at evaluation. The module is written in Typescript and compiles to commonjs.

Check the documentation for extended information about methods and usage.

Table of Contents

Instalation

This module is distributed via npm which is bundled with node and should be installed as one of your project's dependencies:

npm install --save kontrolle

Test

npm run test

Build

npm run build

Document

npm run doc

Usage

Initialization

Here's a simple example of library initialization:

import * as kontrolle from 'kontrolle';

const roles = ['userAdmin', 'agencyAdmin'];
const permissions = {
	agencies: {
		basic: {
			action: ['view', 'update']
		},
	},
	users: {
		manage: {
			action: ['create', 'read', 'update']
		}
	}
}
const features = {
	"agencyAdmin": {
	   "agencies": [
	      "basic"
	   ]
	},
	"usersAdmin": {
	   "users": [
	      "manage"
	   ]
	 }
}

kontrolle.init({
	roles,
	permissions,
	features
})

Evaluation

Here are few examples on basic role and permission checks.

Permissions

can

Evaluate if user can do an action over specified feature.

import * as kontrolle from 'kontrolle';

const roles = ...
const permissions = {
	users: {
		manage: {
			action: ['create', 'read', 'update']
		}
	}
}
const features = ...

kontrolle.init({ roles, permissions, features })

const res = kontrolle.can('users', 'manage', 'create')
// true

const res = kontrolle.can('users', 'manage', 'delete')
//false

 

canAny

Evaluate if user can do any of the provided evaluations.

import * as kontrolle from 'kontrolle';

const roles = ...
const permissions = {
	users: {
		manage: {
			action: ['create', 'read', 'update']
		},
		licence: {
			action: ['view']
		}
	}
}
const features = ...

kontrolle.init({ roles, permissions, features })

const res = kontrolle.canAny(['users', 'manage', 'read'], ['users', 'licence', 'view'])
// true

const res = kontrolle.canAny(['users', 'manage', '*'], ['users', 'licence', 'view'])
// true

const res = kontrolle.canAny(['users', 'manage', '*'], ['users', 'licence', 'update'])
// false

 

canAll

Evaluate if user can do all of the provided evaluations.

import * as kontrolle from 'kontrolle';

const roles = ...
const permissions = {
	users: {
		manage: {
			action: ['create', 'read', 'update']
		},
		licence: {
			action: ['view']
		}
	}
}
const features = ...

kontrolle.init({ roles, permissions, features })

const res = kontrolle.canAny(['users', 'manage', 'read'], ['users', 'licence', 'view'])
// true

const res = kontrolle.canAny(['users', 'manage', '*'], ['users', 'licence', 'view'])
// false

 

canAnyAction

Evalute if user can do any of the provided actions over the feature

import * as kontrolle from 'kontrolle';

const roles = ...
const permissions = {
	users: {
		manage: {
			action: ['create', 'read', 'update']
		},
		licence: {
			action: ['view']
		}
	}
}
const features = ...

kontrolle.init({ roles, permissions, features })

const res = kontrolle.canAnyAction('users', 'manage', ['create', 'read'])
// true

const res = kontrolle.canAnyAction('users', 'manage', ['delete', 'read'])
// true

const res = kontrolle.canAnyAction('users', 'manage', ['delete', 'assign'])
// false

 

canAllActions

Evalute if user can do all of the provided actions over the feature

import * as kontrolle from 'kontrolle';

const roles = ...
const permissions = {
	users: {
		manage: {
			action: ['create', 'read', 'update']
		},
		licence: {
			action: ['view']
		}
	}
}
const features = ...

kontrolle.init({ roles, permissions, features })

const res = kontrolle.canAllActions('users', 'manage', ['create', 'read'])
// true

const res = kontrolle.canAllActions('users', 'manage', ['delete', 'read'])
// false

Roles

hasRole

Check if user has a provided role

import * as kontrolle from 'kontrolle';

const roles = ['userAdmin', 'agencyAdmin'];
const permissions = ...
const features = ...

kontrolle.init({ roles, permissions, features })

const res = kontrolle.hasRole('userAdmin')
// role object { name: 'userAdmin' }

const res = kontrolle.hasRole('superadmin')
// false

 

hasAnyRole

Check if user has any of the provided roles

import * as kontrolle from 'kontrolle';

const roles = ['userAdmin', 'agencyAdmin'];
const permissions = ...
const features = ...

kontrolle.init({ roles, permissions, features })

const res = kontrolle.hasAnyRole(['userAdmin', 'superadmin'])
// true

const res = kontrolle.hasAnyRole(['marketing', 'qa']);
// false

 

hasAllRoles

Check if user has all of the provided roles

import * as kontrolle from 'kontrolle';

const roles = ['userAdmin', 'agencyAdmin'];
const permissions = ...
const features = ...

kontrolle.init({ roles, permissions, features })

const res = kontrolle.hasAllRoles(['userAdmin', 'agencyAdmin'])
// true

const res = kontrolle.hasAnyRole(['userAdmin', 'superadmin']);
// false

Extras

kontrolle's People

Contributors

stoles avatar

Stargazers

Aleksej Dix avatar

Watchers

 avatar

Forkers

aleksejdix padcom

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.