Skip to content

Commit

Permalink
Latest code from OptimalBits/master
Browse files Browse the repository at this point in the history
Pulling from master fork.
  • Loading branch information
leodutra authored Feb 13, 2017
2 parents 133b109 + e69042e commit 5ab4a80
Show file tree
Hide file tree
Showing 13 changed files with 591 additions and 149 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/*
.DS_Store
*~
.idea/
.idea/
.tags*
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
language: node_js
sudo: false

# test on two node.js versions: 0.10
node_js:
- 0.10
- '0.10'
- '0.12'
- 'stable'

services:
services:
- redis-server
- mongodb
58 changes: 39 additions & 19 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ Create roles and assign roles to users. Sometimes it may even be useful to creat
to get the finest granularity possible, while in other situations you will give the *asterisk* permission
for admin kind of functionality.

A Redis, MongoDB and In-Memory based backends are provided built-in in the module. There are other third party backends such as [*knex*](https://github.com/christophertrudel/node_acl_knex) based and [*firebase*](https://github.com/tonila/node_acl_firebase). There is also an alternative memory backend that supports [*regexps*](https://github.com/futurechan/node_acl-mem-regexp).
A Redis, MongoDB and In-Memory based backends are provided built-in in the module. There are other third party backends such as [*knex*](https://github.com/christophertrudel/node_acl_knex) based, [*firebase*](https://github.com/tonila/node_acl_firebase) and [*elasticsearch*](https://github.com/adnanesaghir/acl-elasticsearch-backend). There is also an alternative memory backend that supports [*regexps*](https://github.com/futurechan/node_acl-mem-regexp).

Follow [manast](http://twitter.com/manast) for news and updates regarding this library.

##Status

[![BuildStatus](https://secure.travis-ci.org/OptimalBits/node_acl.png?branch=master)](http://travis-ci.org/OptimalBits/node_acl)
[![Dependency Status](https://david-dm.org/OptimalBits/node_acl.svg)](https://david-dm.org/OptimalBits/node_acl)
[![devDependency Status](https://david-dm.org/OptimalBits/node_acl/dev-status.svg)](https://david-dm.org/OptimalBits/node_acl#info=devDependencies)

##Features

Expand All @@ -43,6 +45,7 @@ npm install acl
* [roleUsers](#roleUsers)
* [hasRole](#hasRole)
* [addRoleParents](#addRoleParents)
* [removeRoleParents](#removeRoleParents)
* [removeRole](#removeRole)
* [removeResource](#removeResource)
* [allow](#allow)
Expand Down Expand Up @@ -81,7 +84,7 @@ Create roles implicitly by giving them permissions:
acl.allow('guest', 'blogs', 'view')

// allow function accepts arrays as any parameter
acl.allow('member', 'blogs', ['edit','view', 'delete'])
acl.allow('member', 'blogs', ['edit', 'view', 'delete'])
```

Users are likewise created implicitly by assigning them roles:
Expand All @@ -93,19 +96,19 @@ acl.addUserRoles('joed', 'guest')
Hierarchies of roles can be created by assigning parents to roles:

```javascript
acl.addRoleParents('baz', ['foo','bar'])
acl.addRoleParents('baz', ['foo', 'bar'])
```

Note that the order in which you call all the functions is irrelevant (you can add parents first and assign permissions to roles later)

```javascript
acl.allow('foo', ['blogs','forums','news'], ['view', 'delete'])
acl.allow('foo', ['blogs', 'forums', 'news'], ['view', 'delete'])
```

Use the wildcard to give all permissions:

```javascript
acl.allow('admin', ['blogs','forums'], '*')
acl.allow('admin', ['blogs', 'forums'], '*')
```

Sometimes is necessary to set permissions on many different roles and resources. This would
Expand All @@ -114,17 +117,17 @@ lead to unnecessary nested callbacks for handling errors. Instead use the follow
```javascript
acl.allow([
{
roles:['guest','member'],
roles:['guest', 'member'],
allows:[
{resources:'blogs', permissions:'get'},
{resources:['forums','news'], permissions:['get','put','delete']}
{resources:['forums', 'news'], permissions:['get', 'put', 'delete']}
]
},
{
roles:['gold','silver'],
roles:['gold', 'silver'],
allows:[
{resources:'cash', permissions:['sell','exchange']},
{resources:['account','deposit'], permissions:['put','delete']}
{resources:'cash', permissions:['sell', 'exchange']},
{resources:['account', 'deposit'], permissions:['put', 'delete']}
]
}
])
Expand All @@ -137,14 +140,14 @@ acl.isAllowed('joed', 'blogs', 'view', function(err, res){
if(res){
console.log("User joed is allowed to view blogs")
}
}
})
```


Of course arrays are also accepted in this function:

```javascript
acl.isAllowed('jsmith', 'blogs', ['edit','view','delete'])
acl.isAllowed('jsmith', 'blogs', ['edit', 'view', 'delete'])
```

Note that all permissions must be full filed in order to get *true*.
Expand All @@ -153,16 +156,16 @@ Note that all permissions must be full filed in order to get *true*.
Sometimes is necessary to know what permissions a given user has over certain resources:

```javascript
acl.allowedPermissions('james', ['blogs','forums'], function(err, permissions){
acl.allowedPermissions('james', ['blogs', 'forums'], function(err, permissions){
console.log(permissions)
})
```

It will return an array of resource:[permissions] like this:

```javascript
[{'blogs' : ['get','delete']},
{'forums':['get','put']}]
[{'blogs' : ['get', 'delete']},
{'forums':['get', 'put']}]
```


Expand Down Expand Up @@ -246,7 +249,7 @@ __Arguments__
---------------------------------------
<a name="roleUsers" />
### roleUsers( rolename, function(err, roles) )
### roleUsers( rolename, function(err, users) )
Return all users who has a given role.
Expand Down Expand Up @@ -289,6 +292,23 @@ __Arguments__
---------------------------------------
<a name="removeRoleParents" />
### removeRoleParents( role, parents, function(err) )
Removes a parent or parent list from role.
If `parents` is not specified, removes all parents.
__Arguments__
```javascript
role {String} Child role.
parents {String|Array} Parent role(s) to be removed [optional].
callback {Function} Callback called when finished [optional].
```
---------------------------------------
<a name="removeRole" />
### removeRole( role, function(err) )
Expand Down Expand Up @@ -393,7 +413,7 @@ __Arguments__
userId {String|Number} User id.
resource {String} resource to ask permissions for.
permissions {String|Array} asked permissions.
callback {Function} Callback called wish the result.
callback {Function} Callback called with the result.
```
---------------------------------------
Expand All @@ -408,7 +428,7 @@ __Arguments__
roles {String|Array} Role(s) to check the permissions for.
resource {String} resource to ask permissions for.
permissions {String|Array} asked permissions.
callback {Function} Callback called wish the result.
callback {Function} Callback called with the result.
```
---------------------------------------
Expand All @@ -433,7 +453,7 @@ __Arguments__
```javascript
role {String|Array} Roles
permissions {String|Array} Permissions
callback {Function} Callback called wish the result.
callback {Function} Callback called with the result.
```
---------------------------------------
Expand Down
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
module.exports = require("./lib/acl.js");
module.exports.redisBackend = require("./lib/redis-backend.js");
module.exports.memoryBackend = require("./lib/memory-backend.js");
module.exports.mongodbBackend = require("./lib/mongodb-backend.js");
module.exports = require('./lib/acl.js');
module.exports.__defineGetter__('redisBackend', function(){
return require('./lib/redis-backend.js');
});
module.exports.__defineGetter__('memoryBackend', function(){
return require('./lib/memory-backend.js');
});
module.exports.__defineGetter__('mongodbBackend', function(){
return require('./lib/mongodb-backend.js');
});
Loading

0 comments on commit 5ab4a80

Please sign in to comment.