Merge pull request #166 from strongloop/feature/acl-subclass

Make sure the configured ACL submodel is used
This commit is contained in:
Raymond Feng 2014-02-03 20:31:06 -08:00
commit 323d5f80b0
2 changed files with 27 additions and 7 deletions

View File

@ -117,10 +117,20 @@ Model.setup = function () {
/*!
* Get the reference to ACL in a lazy fashion to avoid race condition in require
*/
var ACL = null;
function getACL() {
return ACL || (ACL = require('./acl').ACL);
}
var _aclModel = null;
Model._ACL = function getACL(ACL) {
if(ACL !== undefined) {
// The function is used as a setter
_aclModel = ACL;
}
if(_aclModel) {
return _aclModel;
}
var aclModel = require('./acl').ACL;
_aclModel = loopback.getModelByType(aclModel);
return _aclModel;
};
/**
* Check if the given access token can invoke the method
@ -137,9 +147,9 @@ function getACL() {
Model.checkAccess = function(token, modelId, method, callback) {
var ANONYMOUS = require('./access-token').ANONYMOUS;
token = token || ANONYMOUS;
var ACL = getACL();
var aclModel = Model._ACL();
var methodName = 'string' === typeof method? method: method && method.name;
ACL.checkAccessForToken(token, this.modelName, modelId, methodName, callback);
aclModel.checkAccessForToken(token, this.modelName, modelId, methodName, callback);
};
/*!
@ -158,7 +168,7 @@ Model._getAccessTypeForMethod = function(method) {
'method is a required argument and must be a RemoteMethod object'
);
var ACL = getACL();
var ACL = Model._ACL();
switch(method.name) {
case'create':

View File

@ -612,6 +612,16 @@ describe('Model', function() {
}
});
describe('Model._getACLModel()', function() {
it('should return the subclass of ACL', function() {
var Model = require('../').Model;
var acl = ACL.extend('acl');
Model._ACL(null); // Reset the ACL class for the base model
var model = Model._ACL();
assert.equal(model, acl);
});
});
// describe('Model.hasAndBelongsToMany()', function() {
// it("TODO: implement / document", function(done) {
// /* example -