Skip to content
Merged
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: 3 additions & 2 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ app.enableAuth = function() {
var remotes = this.remotes();
var app = this;

remotes.before('**', function(ctx, next, method) {
remotes.authorization = function(ctx, next) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we save the original authorization implementation and call after loopback auth passed? I think that's the canonical way of overriding methods like this.

var baseAuthorization = remotes.authorization;
remotes.authorization = function(ctx, next) {
  if (authorized) {
    baseAuthorization(ctx, next); // or perhaps .call(this, ctx, next)
  } else {
    next(new Error('Not authorized.'));
  }
};

var method = ctx.method;
var req = ctx.req;
var Model = method.ctor;
var modelInstance = ctx.instance;
Expand Down Expand Up @@ -354,7 +355,7 @@ app.enableAuth = function() {
} else {
next();
}
});
};

this.isAuthEnabled = true;
};
Expand Down
39 changes: 39 additions & 0 deletions test/acl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,42 @@ describe('security ACLs', function() {
});
});
});

describe('access check', function() {
var app;
before(function() {
app = loopback();
app.use(loopback.rest());
app.enableAuth();
app.dataSource('test', {connector: 'memory'});
});

it('should occur before other remote hooks', function(done) {
var MyTestModel = app.model('MyTestModel', {base: 'PersistedModel', dataSource: 'test'});
var checkAccessCalled = false;
var beforeHookCalled = false;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider using an array of strings instead of boolean properties. Each hook pushes a string name, and then the assertion can verify the order of execution.

var called = [];

MyTestModel.checkAccess = function() {
  called.push('checkAccess');
  // ...
}

// ...

expect(called).to.eql(['checkAccess', 'beforeHook']);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool idea...


// fake / spy on the checkAccess method
MyTestModel.checkAccess = function() {
var cb = arguments[arguments.length - 1];
checkAccessCalled = true;
var allowed = true;
cb(null, allowed);
};

MyTestModel.beforeRemote('find', function(ctx, next) {
// ensure this is called after checkAccess
if (!checkAccessCalled) return done(new Error('incorrect order'));
beforeHookCalled = true;
next();
});

request(app)
.get('/MyTestModels')
.end(function(err, result) {
assert(beforeHookCalled, 'the before hook should be called');
assert(checkAccessCalled, 'checkAccess should have been called');
done();
});
});
});