Created
April 25, 2015 12:13
-
-
Save gr2m/e9d39d92f72a6e580dcd to your computer and use it in GitHub Desktop.
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
function securityWrapper(checkAllowed, original, args) { | |
var userCtx = args.options.userCtx || { | |
//Admin party! | |
name: null, | |
roles: ["_admin"] | |
}; | |
if (userCtx.roles.indexOf("_admin") !== -1) { | |
return original(); | |
} | |
if (!checkAllowed) { | |
return Promise.resolve().then(throw401); | |
} | |
var promise = filledInSecurity(args) | |
.then(function (security) { | |
if (!checkAllowed(userCtx, security)) { | |
throw401(); | |
} | |
}) | |
.then(function() { | |
var originalPromise = original(); | |
if (originalPromise.on) { | |
queuedOnCalls.forEach(function(args) { | |
originalPromise.on.apply(originalPromise, args); | |
}) | |
queuedOnCalls = undefined | |
promise.on = originalPromise.on.bind(originalPromise); | |
} | |
return originalPromise; | |
}); | |
var queuedOnCalls = []; | |
promise.on = function() { | |
queuedOnCalls.push(arguments); | |
return promise; | |
} | |
return promise; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment