Skip to content

Commit

Permalink
add acl.isInRole function to test whether a user is in a certain role…
Browse files Browse the repository at this point in the history
…. tests added for isInRole & userRoles functions. version bumped
  • Loading branch information
Stephen Schutt committed Sep 8, 2014
1 parent eafc996 commit 7e08bf3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Readme.md
Expand Up @@ -243,6 +243,21 @@ __Arguments__
---------------------------------------
<a name="isInRole" />
### isInRole( userId, rolename, function(err, is_in_role) )
Return boolean whether user is in the role
__Arguments__
```javascript
userId {String|Number} User id.
rolename {String|Number} role name.
callback {Function} Callback called when finished.
```
---------------------------------------
<a name="addRoleParents" />
### addRoleParents( role, parents, function(err) )
Expand Down
16 changes: 16 additions & 0 deletions lib/acl.js
Expand Up @@ -112,6 +112,22 @@ Acl.prototype.userRoles = function(userId, cb){
return this.backend.getAsync('users', userId).nodeify(cb);
};

/**
isInRole( userId, rolename, function(err, is_in_role) )
Return boolean whether user is in the
@param {String|Number} User id.
@param {String|Number} rolename.
@param {Function} Callback called when finished.
@return {Promise} Promise resolved with boolean of whether user is in role
*/
Acl.prototype.isInRole = function(userId, rolename, cb){
return this.backend.getAsync('users', userId).nodeify(function(err, roles) {
return cb(err, roles.indexOf(rolename) != -1);
});
};

/**
addRoleParents( role, parents, function(err) )
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "acl",
"version": "0.4.3",
"version": "0.4.4",
"description": "An Access Control List module, based on Redis with Express middleware support",
"keywords": [
"middleware",
Expand Down
25 changes: 25 additions & 0 deletions test/tests.js
Expand Up @@ -71,6 +71,31 @@ exports.Allows = function () {
})
})

describe('read User Roles', function() {
it('run userRoles function', function(done) {
var acl = new Acl(this.backend)
acl.addUserRoles('harry', 'admin', function (err) {
if (err) return done(err);

acl.userRoles('harry', function(err, roles) {
if (err) return done(err);

assert.deepEqual(roles, ['admin']);
acl.isInRole('harry', 'admin', function(err, is_in_role) {
if (err) return done(err);

assert.ok(is_in_role);
acl.isInRole('harry', 'no role', function(err, is_in_role) {
if (err) return done(err);

assert.notOk(is_in_role)
done()
})
})
})
})
})
})

describe('allow', function () {
it('admin view/add/edit/delete users', function (done) {
Expand Down

0 comments on commit 7e08bf3

Please sign in to comment.