Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
Changed hooks to use hook.service instead of this (#18)
Browse files Browse the repository at this point in the history
This fixes the problem that hooks lose context of this when used inside
other hooks.

Fixes #17: Hook "restrictToOwner" looses context (this) when wrapped
  • Loading branch information
salttis authored and daffl committed Jun 1, 2017
1 parent aba1075 commit 417fbac
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/has-role-or-restrict.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default function (options = {}) {
return hook;
}

return this.find({ query }, params).then(results => {
return hook.service.find({ query }, params).then(results => {
if (hook.method === 'get' && Array.isArray(results) && results.length === 1) {
hook.result = results[0];
return hook;
Expand Down Expand Up @@ -103,7 +103,7 @@ export default function (options = {}) {
// set on the resource we are requesting.
const params = Object.assign({}, hook.params, { provider: undefined });

this.get(hook.id, params).then(data => {
hook.service.get(hook.id, params).then(data => {
if (data.toJSON) {
data = data.toJSON();
} else if (data.toObject) {
Expand Down Expand Up @@ -131,7 +131,7 @@ export default function (options = {}) {
return hook;
}

return this.find({ query }, params).then(results => {
return hook.service.find({ query }, params).then(results => {
if (hook.method === 'get' && Array.isArray(results) && results.length === 1) {
hook.result = results[0];
return hook;
Expand Down
2 changes: 1 addition & 1 deletion src/restrict-to-owner.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default function (options = {}) {
// set on the resource we are requesting.
const params = Object.assign({}, hook.params, { provider: undefined });

return this.get(hook.id, params).then(data => {
return hook.service.get(hook.id, params).then(data => {
if (data.toJSON) {
data = data.toJSON();
} else if (data.toObject) {
Expand Down
2 changes: 1 addition & 1 deletion src/restrict-to-roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default function (options = {}) {
// set on the resource we are requesting.
const params = Object.assign({}, hook.params, { provider: undefined });

return this.get(hook.id, params).then(data => {
return hook.service.get(hook.id, params).then(data => {
if (data.toJSON) {
data = data.toJSON();
} else if (data.toObject) {
Expand Down
14 changes: 8 additions & 6 deletions test/has-role-or-restrict.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ describe('hasRoleOrRestrict', () => {
it('should merge the restriction in to the query and call find', () => {
let hook = {
app: {
service: mockService,
get: function () {}
},
service: MockService,
method: 'find',
type: 'before',
params: {
Expand All @@ -100,9 +100,9 @@ describe('hasRoleOrRestrict', () => {
let hook = {
id: '525235',
app: {
service: MockService,
get: function () {}
},
service: MockService,
method: 'find',
type: 'before',
params: {
Expand Down Expand Up @@ -132,7 +132,8 @@ describe('hasRoleOrRestrict', () => {
},
app: {
get: function () { return {}; }
}
},
service: MockService
};
});

Expand Down Expand Up @@ -171,9 +172,9 @@ describe('hasRoleOrRestrict', () => {
it('should merge the restriction in to the query and call find', () => {
let hook = {
app: {
service: mockService,
get: function () {}
},
service: MockService,
method: 'find',
type: 'before',
params: {
Expand All @@ -191,9 +192,9 @@ describe('hasRoleOrRestrict', () => {
id: '525235',
method: 'find',
app: {
service: MockService,
get: function () {}
},
service: MockService,
type: 'before',
params: {
provider: 'rest'
Expand Down Expand Up @@ -276,7 +277,8 @@ describe('hasRoleOrRestrict', () => {
},
app: {
get: function () { return {}; }
}
},
service: MockService
};
});

Expand Down
3 changes: 2 additions & 1 deletion test/restrict-to-owner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ describe('restrictToOwner', () => {
provider: 'rest',
user: { _id: '1' }
},
app
app,
service: MockService
};
});

Expand Down
3 changes: 2 additions & 1 deletion test/restrict-to-roles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ describe('restrictToRoles', () => {
},
app: {
get: function () { return {}; }
}
},
service: MockService
};
});

Expand Down

0 comments on commit 417fbac

Please sign in to comment.