Skip to content

Commit 41a052c

Browse files
dblythymtrezza
andauthored
Fix: context for afterFind (#7078)
* Fix: context for afterFind * Update CHANGELOG.md Co-authored-by: Manuel <[email protected]>
1 parent 97c3046 commit 41a052c

File tree

5 files changed

+60
-10
lines changed

5 files changed

+60
-10
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ __BREAKING CHANGES:__
77
- NEW: Added file upload restriction. File upload is now only allowed for authenticated users by default for improved security. To allow file upload also for Anonymous Users or Public, set the `fileUpload` parameter in the [Parse Server Options](https://parseplatform.org/parse-server/api/master/ParseServerOptions.html). [#7071](https://github.com/parse-community/parse-server/pull/7071). Thanks to [dblythy](https://github.com/dblythy).
88
___
99
- IMPROVE: Optimize queries on classes with pointer permissions. [#7061](https://github.com/parse-community/parse-server/pull/7061). Thanks to [Pedro Diaz](https://github.com/pdiaz)
10+
- FIX: request.context for afterFind triggers. [#7078](https://github.com/parse-community/parse-server/pull/7078). Thanks to [dblythy](https://github.com/dblythy)
1011

1112
### 4.5.0
1213
[Full Changelog](https://github.com/parse-community/parse-server/compare/4.4.0...4.5.0)

spec/CloudCode.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -3157,4 +3157,14 @@ describe('afterLogin hook', () => {
31573157

31583158
await Parse.Cloud.run('contextTest', {}, { context: { a: 'a' } });
31593159
});
3160+
3161+
it('afterFind should have access to context', async () => {
3162+
Parse.Cloud.afterFind('TestObject', req => {
3163+
expect(req.context.a).toEqual('a');
3164+
});
3165+
const obj = new TestObject();
3166+
await obj.save();
3167+
const query = new Parse.Query(TestObject);
3168+
await query.find({ context: { a: 'a' } });
3169+
});
31603170
});

src/RestQuery.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ function RestQuery(
2525
restWhere = {},
2626
restOptions = {},
2727
clientSDK,
28-
runAfterFind = true
28+
runAfterFind = true,
29+
context
2930
) {
3031
this.config = config;
3132
this.auth = auth;
@@ -36,6 +37,7 @@ function RestQuery(
3637
this.runAfterFind = runAfterFind;
3738
this.response = null;
3839
this.findOptions = {};
40+
this.context = context || {};
3941

4042
if (!this.auth.isMaster) {
4143
if (this.className == '_Session') {
@@ -222,7 +224,16 @@ RestQuery.prototype.each = function (callback) {
222224
return !finished;
223225
},
224226
async () => {
225-
const query = new RestQuery(config, auth, className, restWhere, restOptions, clientSDK);
227+
const query = new RestQuery(
228+
config,
229+
auth,
230+
className,
231+
restWhere,
232+
restOptions,
233+
clientSDK,
234+
this.runAfterFind,
235+
this.context
236+
);
226237
const { results } = await query.execute();
227238
results.forEach(callback);
228239
finished = results.length < restOptions.limit;
@@ -772,7 +783,8 @@ RestQuery.prototype.runAfterFindTrigger = function () {
772783
this.className,
773784
this.response.results,
774785
this.config,
775-
parseQuery
786+
parseQuery,
787+
this.context
776788
)
777789
.then(results => {
778790
// Ensure we properly set the className back

src/rest.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,16 @@ function find(config, auth, className, restWhere, restOptions, clientSDK, contex
3939
.then(result => {
4040
restWhere = result.restWhere || restWhere;
4141
restOptions = result.restOptions || restOptions;
42-
const query = new RestQuery(config, auth, className, restWhere, restOptions, clientSDK);
42+
const query = new RestQuery(
43+
config,
44+
auth,
45+
className,
46+
restWhere,
47+
restOptions,
48+
clientSDK,
49+
true,
50+
context
51+
);
4352
return query.execute();
4453
});
4554
}
@@ -62,7 +71,16 @@ const get = (config, auth, className, objectId, restOptions, clientSDK, context)
6271
.then(result => {
6372
restWhere = result.restWhere || restWhere;
6473
restOptions = result.restOptions || restOptions;
65-
const query = new RestQuery(config, auth, className, restWhere, restOptions, clientSDK);
74+
const query = new RestQuery(
75+
config,
76+
auth,
77+
className,
78+
restWhere,
79+
restOptions,
80+
clientSDK,
81+
true,
82+
context
83+
);
6684
return query.execute();
6785
});
6886
};
@@ -187,7 +205,8 @@ function update(config, auth, className, restWhere, restObject, clientSDK, conte
187205
restWhere,
188206
undefined,
189207
undefined,
190-
false
208+
false,
209+
context
191210
).execute({
192211
op: 'update',
193212
});

src/triggers.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,12 @@ export function getRequestObject(
237237
if (originalParseObject) {
238238
request.original = originalParseObject;
239239
}
240-
241240
if (
242241
triggerType === Types.beforeSave ||
243242
triggerType === Types.afterSave ||
244243
triggerType === Types.beforeDelete ||
245-
triggerType === Types.afterDelete
244+
triggerType === Types.afterDelete ||
245+
triggerType === Types.afterFind
246246
) {
247247
// Set a copy of the context on the request object.
248248
request.context = Object.assign({}, context);
@@ -388,13 +388,21 @@ function logTriggerErrorBeforeHook(triggerType, className, input, auth, error) {
388388
);
389389
}
390390

391-
export function maybeRunAfterFindTrigger(triggerType, auth, className, objects, config, query) {
391+
export function maybeRunAfterFindTrigger(
392+
triggerType,
393+
auth,
394+
className,
395+
objects,
396+
config,
397+
query,
398+
context
399+
) {
392400
return new Promise((resolve, reject) => {
393401
const trigger = getTrigger(className, triggerType, config.applicationId);
394402
if (!trigger) {
395403
return resolve();
396404
}
397-
const request = getRequestObject(triggerType, auth, null, null, config);
405+
const request = getRequestObject(triggerType, auth, null, null, config, context);
398406
if (query) {
399407
request.query = query;
400408
}

0 commit comments

Comments
 (0)