Repository navigation
Use the new remoting.authorization hook for check access #1370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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']);
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
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
authorizationimplementation and call after loopback auth passed? I think that's the canonical way of overriding methods like this.