Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/models/acl.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ ACL.prototype.debug = function() {
* @param {Function} callback
*/
ACL.checkAccess = function (context, callback) {
if (!this.find) {
return callback(new Error('ACL model is not attached to a datasource.' +
' Make sure your application calls app.autoAttach().'));
}

if(!(context instanceof AccessContext)) {
context = new AccessContext(context);
}
Expand Down
22 changes: 22 additions & 0 deletions test/acl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,28 @@ describe('security ACLs', function () {
});
});

it('should report helpful error when ACL is not attached', function(done) {
// HACK(bajtos) We need an ACL model that was not attached yet
// Since other tests are already attaching ACL to a datasource,
// we have to reload the module.
delete require('module')._cache[require.resolve('../lib/models/acl.js')];
acl = require('../lib/models/acl');
loopback.ACL = ACL = acl.ACL;
loopback.Scope = Scope = acl.Scope;

// Verify the assumption that ACL.find is not defined yet
expect(ACL.find).to.equal(undefined);

ACL.checkAccess({
model: 'User',
property: ACL.ALL,
accessType: ACL.ALL
}, function(err) {
if (!err) done(new Error('ACL.checkAccess was supposed to throw'));
expect(err.message).to.contain('app.autoAttach');
done();
});
});
});


Expand Down