Address review comments
This commit is contained in:
parent
5ec7a9bb34
commit
abbd0de126
|
@ -11,6 +11,7 @@
|
||||||
"lib/model.js",
|
"lib/model.js",
|
||||||
"lib/persisted-model.js",
|
"lib/persisted-model.js",
|
||||||
{ "title": "Middleware", "depth": 2 },
|
{ "title": "Middleware", "depth": 2 },
|
||||||
|
"server/middleware/acl.js",
|
||||||
"server/middleware/context.js",
|
"server/middleware/context.js",
|
||||||
"server/middleware/favicon.js",
|
"server/middleware/favicon.js",
|
||||||
"server/middleware/rest.js",
|
"server/middleware/rest.js",
|
||||||
|
|
|
@ -19,6 +19,7 @@ module.exports = acl;
|
||||||
* Normalize the http verb to lower case
|
* Normalize the http verb to lower case
|
||||||
* @param {String} verb HTTP verb/method
|
* @param {String} verb HTTP verb/method
|
||||||
* @returns {String|*}
|
* @returns {String|*}
|
||||||
|
* @private
|
||||||
*/
|
*/
|
||||||
function normalizeVerb(verb) {
|
function normalizeVerb(verb) {
|
||||||
verb = verb.toLowerCase();
|
verb = verb.toLowerCase();
|
||||||
|
@ -32,6 +33,7 @@ function normalizeVerb(verb) {
|
||||||
* Normalize items to string[]
|
* Normalize items to string[]
|
||||||
* @param {String|String[]} items
|
* @param {String|String[]} items
|
||||||
* @returns {String[]}
|
* @returns {String[]}
|
||||||
|
* @private
|
||||||
*/
|
*/
|
||||||
function normalizeList(items) {
|
function normalizeList(items) {
|
||||||
if (!items) {
|
if (!items) {
|
||||||
|
@ -65,6 +67,7 @@ function toLowerCase(m) {
|
||||||
*
|
*
|
||||||
* @param {Object} scopes Scope mappings
|
* @param {Object} scopes Scope mappings
|
||||||
* @returns {Array}
|
* @returns {Array}
|
||||||
|
* @private
|
||||||
*/
|
*/
|
||||||
function normalizeScopeMappings(scopes) {
|
function normalizeScopeMappings(scopes) {
|
||||||
var routes = [];
|
var routes = [];
|
||||||
|
@ -98,6 +101,7 @@ function normalizeScopeMappings(scopes) {
|
||||||
* Normalize and sort ACL entries
|
* Normalize and sort ACL entries
|
||||||
* @param {Object[]} acls An array of ACLs
|
* @param {Object[]} acls An array of ACLs
|
||||||
* @returns {*|Array}
|
* @returns {*|Array}
|
||||||
|
* @private
|
||||||
*/
|
*/
|
||||||
function normalizeACLs(acls) {
|
function normalizeACLs(acls) {
|
||||||
acls = acls || [];
|
acls = acls || [];
|
||||||
|
@ -118,6 +122,7 @@ function normalizeACLs(acls) {
|
||||||
* @param {Object[]} acls An array of acl entries
|
* @param {Object[]} acls An array of acl entries
|
||||||
* @param {Object[]} scopes An array of scopes
|
* @param {Object[]} scopes An array of scopes
|
||||||
* @returns {Object[]} ACLs matching one of the scopes
|
* @returns {Object[]} ACLs matching one of the scopes
|
||||||
|
* @private
|
||||||
*/
|
*/
|
||||||
function matchACLs(acls, scopes) {
|
function matchACLs(acls, scopes) {
|
||||||
var matchedACLs = [];
|
var matchedACLs = [];
|
||||||
|
@ -146,6 +151,7 @@ function matchACLs(acls, scopes) {
|
||||||
* @param {Request} req loopback Request
|
* @param {Request} req loopback Request
|
||||||
* @param {Function} cb Callback function
|
* @param {Function} cb Callback function
|
||||||
* @returns {*}
|
* @returns {*}
|
||||||
|
* @private
|
||||||
*/
|
*/
|
||||||
function identifyScopes(req, scopes, cb) {
|
function identifyScopes(req, scopes, cb) {
|
||||||
var routes = normalizeScopeMappings(scopes);
|
var routes = normalizeScopeMappings(scopes);
|
||||||
|
@ -159,6 +165,7 @@ function identifyScopes(req, scopes, cb) {
|
||||||
/**
|
/**
|
||||||
* Try to find out the principals for the given request
|
* Try to find out the principals for the given request
|
||||||
* @param {Request} req HTTP request object
|
* @param {Request} req HTTP request object
|
||||||
|
* @private
|
||||||
*/
|
*/
|
||||||
function identifyPrincipals(req) {
|
function identifyPrincipals(req) {
|
||||||
var principals = [{
|
var principals = [{
|
||||||
|
@ -198,6 +205,7 @@ function identifyPrincipals(req) {
|
||||||
* @param {Request} req HTTP request object
|
* @param {Request} req HTTP request object
|
||||||
* @param {Object[]} routes An array of routes (methods, path)
|
* @param {Object[]} routes An array of routes (methods, path)
|
||||||
* @returns {Array} Scopes matching the request
|
* @returns {Array} Scopes matching the request
|
||||||
|
* @private
|
||||||
*/
|
*/
|
||||||
function findMatchedScopes(req, routes) {
|
function findMatchedScopes(req, routes) {
|
||||||
var matchedScopes = [];
|
var matchedScopes = [];
|
||||||
|
@ -223,9 +231,11 @@ function findMatchedScopes(req, routes) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate the ACL score based its specifics of principalType and permission
|
* Calculate the ACL score based on its specifics of principalType and
|
||||||
|
* permission
|
||||||
* @param {Object} acl ACL rule
|
* @param {Object} acl ACL rule
|
||||||
* @returns {number}
|
* @returns {number}
|
||||||
|
* @private
|
||||||
*/
|
*/
|
||||||
function getACLScore(acl) {
|
function getACLScore(acl) {
|
||||||
var score = 0;
|
var score = 0;
|
||||||
|
@ -281,11 +291,12 @@ function sortACLs(acl1, acl2) {
|
||||||
return a === b ? 0 : (a > b ? 1 : -1);
|
return a === b ? 0 : (a > b ? 1 : -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/**
|
||||||
* Compare two routes
|
* Compare two routes
|
||||||
* @param {Object} a The first route {verb: 'get', path: '/:id'}
|
* @param {Object} a The first route {verb: 'get', path: '/:id'}
|
||||||
* @param [Object} b The second route {verb: 'get', path: '/findOne'}
|
* @param [Object} b The second route {verb: 'get', path: '/findOne'}
|
||||||
* @returns {number} 1: r1 comes after 2, -1: r1 comes before r2, 0: equal
|
* @returns {number} 1: r1 comes after 2, -1: r1 comes before r2, 0: equal
|
||||||
|
* @private
|
||||||
*/
|
*/
|
||||||
function sortRoutes(a, b) {
|
function sortRoutes(a, b) {
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
var expect = require('chai').expect;
|
||||||
var routeAcl = require('../server/middleware/acl');
|
var routeAcl = require('../server/middleware/acl');
|
||||||
var loopback = require('../index');
|
var loopback = require('../index');
|
||||||
|
|
||||||
|
@ -138,10 +139,9 @@ describe('route based ACLs', function() {
|
||||||
};
|
};
|
||||||
var res = {};
|
var res = {};
|
||||||
handler(req, res, function(err) {
|
handler(req, res, function(err) {
|
||||||
if (err) return done();
|
expect(err).to.be.instanceof(Error);
|
||||||
else {
|
expect(err.statusCode).to.equal(403);
|
||||||
return done(new Error('The request should be denied'));
|
done();
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -177,10 +177,9 @@ describe('route based ACLs', function() {
|
||||||
};
|
};
|
||||||
var res = {};
|
var res = {};
|
||||||
handler(req, res, function(err) {
|
handler(req, res, function(err) {
|
||||||
if (err) return done();
|
expect(err).to.be.instanceof(Error);
|
||||||
else {
|
expect(err.statusCode).to.equal(403);
|
||||||
return done(new Error('The request should be denied'));
|
done();
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue