-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dennis Sänger
committed
Jan 21, 2015
1 parent
9def39b
commit 0b7158e
Showing
6 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"node": true, | ||
"predef": [ "module" ], | ||
"esnext": true | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict'; | ||
|
||
var _CONST = { | ||
'ROOT_RIGHT_SYMBOL': '*' | ||
}; | ||
|
||
var mod = {}; | ||
module.exports = mod; | ||
|
||
mod.isAllowed = function(has, requires) { | ||
has = has || []; | ||
requires = requires || []; | ||
requires = (isArray(requires)) ? requires : [requires]; | ||
|
||
return isEmpty(requires) || isRoot(has) || checkOr(has, requires); | ||
}; | ||
|
||
function isArray(maybe) { | ||
return maybe && typeof maybe === 'object' && maybe.hasOwnProperty('length'); | ||
} | ||
|
||
function isEmpty(requires) { | ||
var isEmptyList = (!requires || requires.length === 0); | ||
var hasEmptyEntry = (requires[0] === '' && requires.length === 1); | ||
return isEmptyList || hasEmptyEntry; | ||
} | ||
|
||
function isRoot(has) { | ||
has = has || []; | ||
return has.indexOf(_CONST.ROOT_RIGHT_SYMBOL) > -1; | ||
} | ||
|
||
function checkOr(has, requires) { | ||
var found = false; | ||
|
||
has.forEach(function(right) { | ||
requires.forEach(function(required) { | ||
right = escapeRegExp(right); | ||
var regExp = new RegExp('^' + right + '(\\.[a-zA-Z\\.]*)*$'); | ||
found = found || regExp.test(required); | ||
}); | ||
}); | ||
return found; | ||
} | ||
|
||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions | ||
function escapeRegExp(string){ | ||
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "access.js", | ||
"version": "0.0.1", | ||
"description": "Littl' lib that checks access to parts of your webapp", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "npm test" | ||
}, | ||
"keywords": [ | ||
"access", | ||
"rights", | ||
"user" | ||
], | ||
"author": "Dennis Saenger <[email protected]>", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"jasmine-node": "^1.14.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"node": "true", | ||
"predef": [ "describe", "it", "expect" ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
|
||
var uut = require('../'); | ||
|
||
describe('access.js', function() { | ||
|
||
it('should export a `isAllowed` method', function() { | ||
expect(uut.isAllowed).toBeDefined(); | ||
expect(typeof uut.isAllowed).toEqual('function'); | ||
}); | ||
|
||
describe('isAllowed()', function() { | ||
|
||
it('should return true if required right is empty string', function() { | ||
var bool = uut.isAllowed(['some'], ''); | ||
expect(bool).toBe(true); | ||
}); | ||
|
||
it('should return true if required right is undefined', function() { | ||
var bool = uut.isAllowed(['some']); | ||
expect(bool).toBe(true); | ||
}); | ||
|
||
it('should return true for ([*], "foo")', function() { | ||
var bool = uut.isAllowed(['*'], 'foo'); | ||
expect(bool).toBe(true); | ||
}); | ||
|
||
it('should acceppt array for requires', function() { | ||
var bool = uut.isAllowed(['x'], ['y', 'x']); | ||
expect(bool).toBe(true); | ||
}); | ||
|
||
it('should return true for ([x], "x.y")', function() { | ||
var bool = uut.isAllowed(['x'], 'x.y'); | ||
expect(bool).toBe(true); | ||
}); | ||
|
||
// | ||
// TODO | ||
// uut.isAllowed(['x', 'y'], ['a', ['x','y']]) | ||
// this means: user needs (right a) OR (right x AND y) | ||
// | ||
// uut.isAllowed(['x', 'y'], ['a', ['x', 'y']]) => true | ||
// uut.isAllowed(['x'], ['a', ['x', 'y']]) => false | ||
// uut.isAllowed(['a'], ['a', ['x', 'y']]) => true | ||
// | ||
}); | ||
|
||
}); |