Skip to content

Commit

Permalink
init project
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Sänger committed Jan 21, 2015
1 parent 9def39b commit 0b7158e
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
6 changes: 6 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"node": true,
"predef": [ "module" ],
"esnext": true
}

49 changes: 49 additions & 0 deletions index.js
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, "\\$&");
}
19 changes: 19 additions & 0 deletions package.json
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"
}
}
4 changes: 4 additions & 0 deletions spec/.jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"node": "true",
"predef": [ "describe", "it", "expect" ]
}
50 changes: 50 additions & 0 deletions spec/index.spec.js
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
//
});

});

0 comments on commit 0b7158e

Please sign in to comment.