UNPKG

2.21 kBMarkdownView Raw
1[![Build Status](https://travis-ci.org/digitalsadhu/admittance.png?branch=master)](https://travis-ci.org/digitalsadhu/admittance) [![Coverage Status](https://coveralls.io/repos/digitalsadhu/admittance/badge.png)](https://coveralls.io/r/digitalsadhu/admittance)
2
3# Admittance (Version 2)
4
5This is a rewrite of the original incomplete V1 version of admittance. I decided that V1 was trying to do too much and that V2 should be as simple as possible, both in API and in what it actually does under the hood.
6
7Admittance now reads permissions from plain old javascript objects. This, I think helps to keep the module doing just one thing. To load data you just need create javascript objects and store them somewhere. You could simply require a json file and load it. This also makes it very easy to work with a nosql db. Just get and set your permissions to the db.
8
9## Basic usage
10
11```js
12var admittance = require('admittance')
13
14admittance.load({1: ['admin', 'subscriber'], 2: 'subscriber'})
15
16admittance(1).is('admin') //true!
17
18admittance(1).is('subscriber') //true!
19
20admittance(2).is('subscriber') //true!
21
22admittance(2).is('admin') //false!
23
24```
25
26## Permission hierarchy usage
27
28```js
29var permissions = {
30 'admin': ['subscriber', 'editor'], //any userid assigned admin will also pass a subscriber or editor check
31 'editor': 'blogger', //any userid assigned editor will also pass a blogger check
32 1: 'admin'
33}
34
35```
36
37```js
38admittance(1).is('admin') //true
39
40admittance(1).is('subscriber') //true
41
42admittance(1).is('editor') //true
43
44admittance(1).is('blogger') //true
45```
46
47## Permissions format
48
49Admittance expects a simple map from userids to permissions. Permissions are strings or array of strings. The strings are simply permission names that make sense for your application context.
50
51example:
52
53```js
54var permissions = {
55 1: 'admin',
56 2: ['admin', 'subscriber', 'editor'],
57 3: 'editor'
58}
59```
60
61You can define nested hierarchies as well
62
63```js
64var permissions = {
65 'admin': ['subscriber', 'editor'], //any userid assigned admin will also pass a subscriber or editor check
66 'editor': 'blogger', //any userid assigned editor will also pass a blogger check
67 1: 'admin'
68}
69```
70
71## Tests
72
73```js
74npm test
75```