Last active
June 13, 2016 07:06
-
-
Save 1nstinct/12399f8adc4e5cfd6e88 to your computer and use it in GitHub Desktop.
sails-hook-parametized-policies N policies
This file contains hidden or 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
/** | |
* Ability to check any count of policies in OR ordering | |
* @author Yuriy Syedin <[email protected]> | |
* @returns {Function} | |
*/ | |
module.exports = function () { | |
var makeFakeForbidden = function (policies, p, req, res, next) { | |
if (!policies[p + 1]) return res; // if there is no next policy, that should be verified - leave result as it is | |
var customRes = {}; // init empty response object | |
for (var i in res) { | |
if (i === 'forbidden') { | |
// override forbidden function for next policy | |
customRes['forbidden'] = function () { | |
policies[p + 1](req, makeFakeForbidden(policies, p + 1, req, res, next), next); // call next policy with custom result | |
} | |
} else if (i == 'headerSent') { // fixing issue "...connect deprecated res.headerSent: use standard res.headersSent at api/policyFactories/or.js..." | |
customRes['headersSent'] = res['headersSent']; | |
} else { | |
customRes[i] = res[i]; | |
} | |
} | |
return customRes; | |
}; | |
// policies as arguments, that were passed | |
var args = arguments.callee.arguments; | |
return function (req, res, next) { | |
// check first policy | |
args[0](req, makeFakeForbidden(args, 0, req, res, next), next); | |
} | |
}; |
Not at all :)
I've done changes, that realize abilty to send any count of policies as arguments.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks.man. a little bit espaguetti but it works