Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Merge branch 'develop' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyambati Thomas committed May 3, 2016
2 parents 8eb5173 + 8b9a566 commit 03b4915
Show file tree
Hide file tree
Showing 27 changed files with 1,275 additions and 1,460 deletions.
213 changes: 0 additions & 213 deletions .eslintrc

This file was deleted.

1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"sinon",
"spyOn"
],
"esnext": "true",
"quotmark": true,
"trailing": true,
"undef": true,
Expand Down
6 changes: 3 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"POST",
"GET",
"PUT"
]
}],
"action": "allow"
],
"action": "allow"
}]
}]
File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions examples/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var acl = require('../../');
//

acl.config({
filename: 'nacl.json',
baseUrl: 'v1'
});

Expand Down Expand Up @@ -62,8 +63,6 @@ module.exports = function(app, express) {
next();
});

console.log('something');

/**
* lets create our jwt middleware
*/
Expand Down
73 changes: 48 additions & 25 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
var fs = require('fs');
var assert = require('assert');
var log = require('npmlog');
var yaml = require('js-yaml');

module.exports = {
log: log,
Expand All @@ -12,7 +13,7 @@
permissions: 'Policy not set, All traffic will be denied'.yellow
},
err: {
required: 'group, permissions, resources, and action'.red
required: 'group, permissions, resources,'.red
}
},

Expand All @@ -25,6 +26,7 @@

validate: function(config) {
var self = this;
var permissions;

/**
* Check if config is a array
Expand All @@ -46,59 +48,77 @@
*/

config.forEach(function(el) {
if (el.group && el.permissions && el.action) {
if (el.group && el.permissions) {

/**
* Assert that they are of the correct type
*/

assert.deepEqual(typeof el.group, 'string');
assert(Array.isArray(el.permissions), true);
assert.deepEqual(typeof el.action, 'string');

} else {

/* istanbul ignore next */
permissions = el.permissions;

return log.error('REQUIRED:', self.error.err.required);
permissions.forEach(function(policy) {
assert(policy.resource, true);
assert(policy.methods, true);
assert(policy.action, true);
});

return;
}

/* istanbul ignore next */

return log.error('REQUIRED:', self.error.err.required);


});

return config;
},

readJson: function(newPath, encoding) {
var path = 'config.json';
var config;
if (newPath) {
path = newPath;
}

readJson: function(path, encoding, yml) {
var config, buffer;
/**
* Read the config file with the rules.
*
*/

config = JSON.parse(fs.readFileSync(path, { encoding: encoding }));
try {

buffer = fs.readFileSync(path, { encoding: encoding });

if (yml) {
config = yaml.safeLoad(buffer);
} else {
config = JSON.parse(buffer);
}


} catch (e) {
/* istanbul ignore next */
throw Error(e);
}

return this.validate(config);
},

/**
* [policy Gets the policy from the array
* [group Gets the group from the array
* of rules based on the role of the user]
* @param {[Array]} rule
* @param {[String]} role
* @return {[Object]}
*/

policy: function(res, rules, role) {
var policy = rules.find(function(el) {
group: function(res, rules, role) {
var group = rules.find(function(el) {
return el.group === role;
});

if (policy) {
return policy;
if (group) {
return group;
} else {
return res
.status(403)
Expand All @@ -112,18 +132,21 @@


/**
* [methods Gets the methods from the selected policy]
* @param {[Object]} policy
* [methods Gets the methods from the selected group]
* @param {[Object]} group
* @param {[String]} resource
* @return {[Array/String]} Returns an array of methods
* or a string incase a glob is used;
*/

methods: function(policy, resource) {
var permissions = policy.permissions.find(function(el) {
policy: function(group, resource) {
var permissions = group.permissions.find(function(el) {
return el.resource === resource;
});
return (permissions) ? permissions.methods : permissions;
return (permissions) ? {
methods: permissions.methods,
action: permissions.action
} : permissions;
},

role: function(res, decoded, session) {
Expand Down

0 comments on commit 03b4915

Please sign in to comment.