Comparing version 0.0.1 to 0.1.0
25
index.js
@@ -38,2 +38,6 @@ 'use strict'; | ||
/** | ||
* Constructor | ||
* @param Object config_ Configuration object (see test/config.js for an axample) | ||
*/ | ||
function Can(config_) { | ||
@@ -94,8 +98,21 @@ | ||
Can.prototype.check = function() { | ||
return this._exec.apply(this, arguments); | ||
/** | ||
* Check if user is authorized to perform an action | ||
* @param {object} user an user instance (any value, type, schema,...) | ||
* @param {string} actionName action the user is trying to perform | ||
* @param {string} targetName object type on which the action is being performed (e.g.: user, blog, photo) | ||
* @param {object|undefined} targetObject the actual object the action is being performed on (e.g.: object from db) | ||
* @return {boolean} true or false | ||
*/ | ||
Can.prototype.check = function(user, actionName, targetName, targetObject) { | ||
return this._exec.call(this, user, actionName, targetName, targetObject); | ||
}; | ||
Can.prototype.assert = function(__, actionName, targetName) { | ||
assert(this._exec.apply(this, arguments) === true, 'User is not authorized to perform action ' + actionName + ' on object ' + targetName); | ||
/** | ||
* Check if user is authorized to perform an action; throws if not | ||
* @throws {AssertionError} If user is not authorized to perform this action | ||
*/ | ||
Can.prototype.assert = function(user, actionName, targetName, targetObject) { | ||
assert(this._exec.call(this, user, actionName, targetName, targetObject) === true, 'User is not authorized to perform action ' + actionName + ' on object ' + targetName); | ||
}; |
{ | ||
"name": "can.js", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "micro authorization library", | ||
@@ -26,3 +26,6 @@ "main": "index.js", | ||
"node": "^0.10" | ||
}, | ||
"devDependencies": { | ||
"pre-commit": "0.0.9" | ||
} | ||
} |
# Can.js | ||
Micro authorization-library for NodeJS. | ||
[![Build Status](https://travis-ci.org/plasticpanda/can.js.svg?branch=master)](https://travis-ci.org/plasticpanda/can.js) | ||
[![NPM](https://nodei.co/npm/can.js.png)](https://nodei.co/npm/can.js/) | ||
For usage check ```test``` folder. | ||
Micro authorization library for NodeJS. | ||
For usage check the ```test``` folder. | ||
## LICENSE | ||
@@ -24,2 +28,1 @@ | ||
@@ -18,3 +18,6 @@ 'use strict'; | ||
'*': { | ||
'*': [hasRole('admin')] | ||
'*': [hasRole('admin')] // admin users are always allowed to perfor any action, | ||
// if the right-side evaluates to "true" the action is authorized | ||
// else, the next rules are evaluated | ||
// if no rule evaluates to true the default action is to deny the action | ||
}, | ||
@@ -24,2 +27,3 @@ | ||
// every function in the array *must* return true for this action to be authorized | ||
'create': [isAuthenticated, function () { | ||
@@ -26,0 +30,0 @@ return true; |
@@ -50,6 +50,14 @@ 'use strict'; | ||
tape('nonexistent rules', function (t) { | ||
t.notOk(can.check(fixitures.user_john, 'foo', 'baz'), 'evaluates to false'); | ||
t.ok(can.check(fixitures.user_admin, 'foo', 'bar'), '...unless they match a wildcard'); | ||
t.end(); | ||
}); | ||
tape('throw on forbidden', function (t) { | ||
t.doesNotThrow(function () { can.assert(fixitures.user_anonymous, 'visit', 'site'); }, 'can visit website'); | ||
t.throws(function () { can.assert(fixitures.user_anonymous, 'comment', 'site'); }, 'cannot comment'); | ||
t.throws(function () { can.assert(fixitures.user_anonymous, 'foo', 'bar'); }, 'on nonexistent rule'); | ||
t.end(); | ||
}); | ||
}); |
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
8669
177
28
1