context-access
Advanced tools
Comparing version 1.0.1 to 1.1.0
{ | ||
"name": "context-access", | ||
"description": "Powerful access control with a dead simple API.", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"keywords": ["acl", "permissions", "access control", "permission", "access"], | ||
@@ -6,0 +6,0 @@ "repo": "bloodhound/context-access", |
29
index.js
@@ -25,3 +25,6 @@ /** | ||
exports.allow = function(context) { | ||
this.contexts.push(new Context(context)); | ||
if (!(context instanceof Context)) { | ||
context = new Context(context); | ||
} | ||
this.contexts.push(context); | ||
}; | ||
@@ -35,2 +38,3 @@ | ||
* @param {Context} context | ||
* @param {Context} target Optional. A target context to assert a match. | ||
* @return {Boolean} | ||
@@ -40,7 +44,15 @@ * @api public | ||
exports.assert = function(context) { | ||
for (var len = this.contexts.length, i=0; i<len; i++) { | ||
if (this.contexts[i].match(context, true)) return true; | ||
continue; | ||
exports.assert = function(context, target) { | ||
if (target) { | ||
if (!(target instanceof Context)) { | ||
target = new Context(target); | ||
} | ||
if (target.match(context)) return true; | ||
} | ||
else { | ||
for (var len = this.contexts.length, i=0; i<len; i++) { | ||
if (this.contexts[i].match(context)) return true; | ||
continue; | ||
} | ||
} | ||
return false; | ||
@@ -74,2 +86,8 @@ }; | ||
/** | ||
* Export `Context` | ||
*/ | ||
module.exports.Context = Context; | ||
/** | ||
* Match given `context` with this context. | ||
@@ -85,2 +103,3 @@ * | ||
Context.prototype.match = function(context, operator) { | ||
if (operator === undefined) operator = true; | ||
var imbricatedTargets = []; | ||
@@ -87,0 +106,0 @@ var matchImbricated = function(imbricated, operator) { |
@@ -7,3 +7,3 @@ { | ||
"homepage": "https://github.com/bloodhound/context-access", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"contributors": [ | ||
@@ -24,3 +24,3 @@ { | ||
"scripts": { | ||
"test": "NODE_ENV=test node_modules/mocha/bin/mocha --bail --reporter spec $(find test -name \"*.js\")" | ||
"test": "NODE_ENV=test node_modules/mocha/bin/mocha --bail --reporter spec test" | ||
}, | ||
@@ -27,0 +27,0 @@ "engines": { |
@@ -66,2 +66,4 @@ # context-access | ||
### AND and OR operations | ||
You can imbricate arrays to alternate AND and OR operations when asserting: | ||
@@ -91,16 +93,11 @@ | ||
var app = require('express')(); | ||
var access = require('coaccess'); | ||
var access = require('context-access'); | ||
// Allow users with manager or admin role to POST to /users | ||
access.allow({ | ||
role: 'guest', | ||
path: '/users', | ||
method: 'GET' | ||
method: [['GET', 'POST']] | ||
role: [['manager', 'admin']], | ||
}); | ||
access.allow({ | ||
role: 'admin', | ||
path: '/users', | ||
method: ['GET', 'PUT', 'POST', 'DELETE'] | ||
}); | ||
// Route middleware | ||
@@ -116,3 +113,5 @@ var authorize = function(req, res, next) { | ||
} | ||
res.send(403, 'You must be an admin to do this!'); | ||
else { | ||
res.send(403, 'You must be an admin to do this!'); | ||
} | ||
}; | ||
@@ -119,0 +118,0 @@ |
@@ -10,2 +10,9 @@ var should = require('should'); | ||
it('should export Context', function(done) { | ||
should.exist(access); | ||
access.should.have.property('Context'); | ||
access.allow.should.be.a('function'); | ||
done(); | ||
}); | ||
it('should export allow()', function(done) { | ||
@@ -59,3 +66,3 @@ should.exist(access); | ||
it('should match contexts', function(done) { | ||
it('should match allowed contexts', function(done) { | ||
access.allow({ | ||
@@ -92,2 +99,11 @@ application: 'one', | ||
}); | ||
it('should match provided target context', function(done) { | ||
var target = new access.Context({ role: 'guest' }); | ||
result = access.assert({ role: 'guest' }, target); | ||
result.should.equal(true); | ||
result = access.assert({ role: 'admin' }, target); | ||
result.should.equal(false); | ||
done(); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9735
226
150