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 |
|
5 | This 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 |
|
7 | Admittance 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
|
12 | var admittance = require('admittance')
|
13 |
|
14 | admittance.load({1: ['admin', 'subscriber'], 2: 'subscriber'})
|
15 |
|
16 | admittance(1).is('admin') //true!
|
17 |
|
18 | admittance(1).is('subscriber') //true!
|
19 |
|
20 | admittance(2).is('subscriber') //true!
|
21 |
|
22 | admittance(2).is('admin') //false!
|
23 |
|
24 | ```
|
25 |
|
26 | ## Permission hierarchy usage
|
27 |
|
28 | ```js
|
29 | var 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
|
38 | admittance(1).is('admin') //true
|
39 |
|
40 | admittance(1).is('subscriber') //true
|
41 |
|
42 | admittance(1).is('editor') //true
|
43 |
|
44 | admittance(1).is('blogger') //true
|
45 | ```
|
46 |
|
47 | ## Permissions format
|
48 |
|
49 | Admittance 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 |
|
51 | example:
|
52 |
|
53 | ```js
|
54 | var permissions = {
|
55 | 1: 'admin',
|
56 | 2: ['admin', 'subscriber', 'editor'],
|
57 | 3: 'editor'
|
58 | }
|
59 | ```
|
60 |
|
61 | You can define nested hierarchies as well
|
62 |
|
63 | ```js
|
64 | var 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
|
74 | npm test
|
75 | ```
|