From 592f3f9278f17b324a051d99155cf69416969c3a Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Mon, 3 Feb 2014 16:00:01 -0800 Subject: [PATCH] Make sure the configured ACL submodel is used --- lib/models/model.js | 24 +++++++++++++++++------- test/model.test.js | 10 ++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/lib/models/model.js b/lib/models/model.js index 47fad80c..78df36f0 100644 --- a/lib/models/model.js +++ b/lib/models/model.js @@ -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': diff --git a/test/model.test.js b/test/model.test.js index 8cb112ab..b2222888 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -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 -