Skip to content

Commit

Permalink
Added compat with koa-rbac 0.x in middleware functions signature.
Browse files Browse the repository at this point in the history
  • Loading branch information
yanickrochon committed Jan 5, 2016
1 parent c4634fa commit 559cb62
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koa-rbac",
"version": "1.0.2",
"version": "1.0.3",
"description": "Role-Based Acess Control for Koa",
"author": "Yanick Rochon <yanick.rochon@gmail.com>",
"keywords": [
Expand Down
15 changes: 15 additions & 0 deletions rbac.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ Return a middleware to allow only for the given permissions.
@param redirectUrl {string} (optional) if not allowed, try to redirect
*/
function allowMiddleware(permissions, params, redirectUrl) {
if (arguments.length < 3 && typeof params === 'string') {
redirectUrl = params;
params = undefined;
}

return function * (next) {
const rbac = this.rbac;
var allowedPriority;
Expand All @@ -124,6 +129,11 @@ Return a middleware to deny any with the given permissions.
@param redirectUrl {string} (optional) if not allowed, try to redirect
*/
function denyMiddleware(permissions, params, redirectUrl) {
if (arguments.length < 3 && typeof params === 'string') {
redirectUrl = params;
params = undefined;
}

return function * (next) {
const rbac = this.rbac;
var deniedPriority;
Expand Down Expand Up @@ -154,6 +164,11 @@ Return a middleware to allow and not deny any with the given permissions.
@param redirectUrl {string} (optional) if not allowed, try to redirect
*/
function checkMiddleware(permissions, params, redirectUrl) {
if (arguments.length < 3 && typeof params === 'string') {
redirectUrl = params;
params = undefined;
}

return function * (next) {
const rbac = this.rbac;
var allowedPriority;
Expand Down
16 changes: 8 additions & 8 deletions test/rbac.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,20 @@ describe('Test RBAC', function () {
validate(MIDDLEWARE_OPTIONS, 'marge', middleware.check({ 'allow': ['manage'], 'deny': ['read'] }), 403, 'text/html'),

// redirect
validate(MIDDLEWARE_OPTIONS, 'bart', middleware.allow('read', null, '/foo'), 200, 'text/html'),
validate(MIDDLEWARE_OPTIONS, 'marge', middleware.allow(['update'], null, '/foo'), 200, 'text/html'),
validate(MIDDLEWARE_OPTIONS, 'bart', middleware.allow('manage', null, '/foo'), 302, 'text/html'),
validate(MIDDLEWARE_OPTIONS, 'bart', middleware.allow('read', '/foo'), 200, 'text/html'),
validate(MIDDLEWARE_OPTIONS, 'marge', middleware.allow(['update'], {}, '/foo'), 200, 'text/html'),
validate(MIDDLEWARE_OPTIONS, 'bart', middleware.allow('manage','/foo'), 302, 'text/html'),

validate(MIDDLEWARE_OPTIONS, 'bart', middleware.deny(['read'], null, '/foo'), 302, 'text/html'),
validate(MIDDLEWARE_OPTIONS, 'bart', middleware.deny(['read'], null, '/foo'), 302, 'application/json'),
validate(MIDDLEWARE_OPTIONS, 'bart', middleware.deny(['read'], {}, '/foo'), 302, 'text/html'),
validate(MIDDLEWARE_OPTIONS, 'bart', middleware.deny(['read'], '/foo'), 302, 'application/json'),
validate(MIDDLEWARE_OPTIONS, 'burns', middleware.deny(['read', 'update'], null, '/foo'), 200, 'text/html'),
validate(MIDDLEWARE_OPTIONS, 'bart', middleware.deny(['read'], null, '/foo'), 302, 'text/html'),
validate(MIDDLEWARE_OPTIONS, 'bart', middleware.deny(['read'], '/foo'), 302, 'text/html'),
validate(MIDDLEWARE_OPTIONS, 'bart', middleware.deny(['read'], null, '/foo'), 302, 'application/json'),
validate(MIDDLEWARE_OPTIONS, 'burns', middleware.deny(['read', 'manage'], null, '/foo'), 302, 'text/html'),
validate(MIDDLEWARE_OPTIONS, 'burns', middleware.deny(['read', 'manage'], null, '/foo'), 302, 'application/json'),

validate(MIDDLEWARE_OPTIONS, 'marge', middleware.check({ 'allow': ['manage'], 'deny': ['read'] }, null, '/foo'), 302, 'text/html'),
validate(MIDDLEWARE_OPTIONS, 'marge', middleware.check({ 'allow': ['manage'], 'deny': ['read'] }, null, '/foo'), 302, 'application/json '),
validate(MIDDLEWARE_OPTIONS, 'marge', middleware.check({ 'allow': ['manage'], 'deny': ['read'] }, '/foo'), 302, 'text/html'),
validate(MIDDLEWARE_OPTIONS, 'marge', middleware.check({ 'allow': ['manage'], 'deny': ['read'] }, {}, '/foo'), 302, 'application/json '),
]);
});

Expand Down

0 comments on commit 559cb62

Please sign in to comment.