Merge pull request #453 from strongloop/feature/fix-issue-451
Pass in remotingContext for ACL
This commit is contained in:
commit
cec7deb4a5
|
@ -323,6 +323,7 @@ app.enableAuth = function() {
|
||||||
req.accessToken,
|
req.accessToken,
|
||||||
modelId,
|
modelId,
|
||||||
method,
|
method,
|
||||||
|
ctx,
|
||||||
function(err, allowed) {
|
function(err, allowed) {
|
||||||
// Emit any cached data events that fired while checking access.
|
// Emit any cached data events that fired while checking access.
|
||||||
req.resume();
|
req.resume();
|
||||||
|
|
|
@ -66,6 +66,7 @@ function AccessContext(context) {
|
||||||
if (token.appId) {
|
if (token.appId) {
|
||||||
this.addPrincipal(Principal.APPLICATION, token.appId);
|
this.addPrincipal(Principal.APPLICATION, token.appId);
|
||||||
}
|
}
|
||||||
|
this.remotingContext = context.remotingContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define constant for the wildcard
|
// Define constant for the wildcard
|
||||||
|
|
|
@ -234,16 +234,23 @@ Model._ACL = function getACL(ACL) {
|
||||||
* @param {AccessToken} token The access token
|
* @param {AccessToken} token The access token
|
||||||
* @param {*} modelId The model ID.
|
* @param {*} modelId The model ID.
|
||||||
* @param {SharedMethod} sharedMethod The method in question
|
* @param {SharedMethod} sharedMethod The method in question
|
||||||
|
* @param {Object} ctx The remote invocation context
|
||||||
* @callback {Function} callback The callback function
|
* @callback {Function} callback The callback function
|
||||||
* @param {String|Error} err The error object
|
* @param {String|Error} err The error object
|
||||||
* @param {Boolean} allowed True if the request is allowed; false otherwise.
|
* @param {Boolean} allowed True if the request is allowed; false otherwise.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Model.checkAccess = function(token, modelId, sharedMethod, callback) {
|
Model.checkAccess = function(token, modelId, sharedMethod, ctx, callback) {
|
||||||
var ANONYMOUS = require('./access-token').ANONYMOUS;
|
var ANONYMOUS = require('./access-token').ANONYMOUS;
|
||||||
token = token || ANONYMOUS;
|
token = token || ANONYMOUS;
|
||||||
var aclModel = Model._ACL();
|
var aclModel = Model._ACL();
|
||||||
|
|
||||||
|
ctx = ctx || {};
|
||||||
|
if(typeof ctx === 'function' && callback === undefined) {
|
||||||
|
callback = ctx;
|
||||||
|
ctx = {};
|
||||||
|
}
|
||||||
|
|
||||||
aclModel.checkAccessForContext({
|
aclModel.checkAccessForContext({
|
||||||
accessToken: token,
|
accessToken: token,
|
||||||
model: this,
|
model: this,
|
||||||
|
@ -251,7 +258,8 @@ Model.checkAccess = function(token, modelId, sharedMethod, callback) {
|
||||||
method: sharedMethod.name,
|
method: sharedMethod.name,
|
||||||
sharedMethod: sharedMethod,
|
sharedMethod: sharedMethod,
|
||||||
modelId: modelId,
|
modelId: modelId,
|
||||||
accessType: this._getAccessTypeForMethod(sharedMethod)
|
accessType: this._getAccessTypeForMethod(sharedMethod),
|
||||||
|
remotingContext: ctx
|
||||||
}, function(err, accessRequest) {
|
}, function(err, accessRequest) {
|
||||||
if(err) return callback(err);
|
if(err) return callback(err);
|
||||||
callback(null, accessRequest.isAllowed());
|
callback(null, accessRequest.isAllowed());
|
||||||
|
|
|
@ -160,6 +160,19 @@ describe('access control - integration', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('/accounts', function () {
|
describe('/accounts', function () {
|
||||||
|
var count = 0;
|
||||||
|
before(function() {
|
||||||
|
var roleModel = loopback.getModelByType(loopback.Role);
|
||||||
|
roleModel.registerResolver('$dummy', function (role, context, callback) {
|
||||||
|
process.nextTick(function () {
|
||||||
|
if(context.remotingContext) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
callback && callback(null, false); // Always true
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
lt.beforeEach.givenModel('account');
|
lt.beforeEach.givenModel('account');
|
||||||
|
|
||||||
lt.it.shouldBeDeniedWhenCalledAnonymously('GET', '/api/accounts');
|
lt.it.shouldBeDeniedWhenCalledAnonymously('GET', '/api/accounts');
|
||||||
|
@ -170,7 +183,6 @@ describe('access control - integration', function () {
|
||||||
lt.it.shouldBeDeniedWhenCalledUnauthenticated('GET', urlForAccount);
|
lt.it.shouldBeDeniedWhenCalledUnauthenticated('GET', urlForAccount);
|
||||||
lt.it.shouldBeDeniedWhenCalledByUser(CURRENT_USER, 'GET', urlForAccount);
|
lt.it.shouldBeDeniedWhenCalledByUser(CURRENT_USER, 'GET', urlForAccount);
|
||||||
|
|
||||||
|
|
||||||
lt.it.shouldBeDeniedWhenCalledAnonymously('POST', '/api/accounts');
|
lt.it.shouldBeDeniedWhenCalledAnonymously('POST', '/api/accounts');
|
||||||
lt.it.shouldBeDeniedWhenCalledUnauthenticated('POST', '/api/accounts');
|
lt.it.shouldBeDeniedWhenCalledUnauthenticated('POST', '/api/accounts');
|
||||||
lt.it.shouldBeDeniedWhenCalledByUser(CURRENT_USER, 'POST', '/api/accounts');
|
lt.it.shouldBeDeniedWhenCalledByUser(CURRENT_USER, 'POST', '/api/accounts');
|
||||||
|
|
|
@ -124,6 +124,13 @@
|
||||||
"principalType": "ROLE",
|
"principalType": "ROLE",
|
||||||
"principalId": "$owner",
|
"principalId": "$owner",
|
||||||
"property": "deleteById"
|
"property": "deleteById"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"accessType": "*",
|
||||||
|
"permission": "DENY",
|
||||||
|
"property": "find",
|
||||||
|
"principalType": "ROLE",
|
||||||
|
"principalId": "$dummy"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue