From cc8cb5c2a7c45445ca1a1d9343e278dca2571f8f Mon Sep 17 00:00:00 2001 From: dblythy Date: Thu, 17 Dec 2020 02:41:33 +1100 Subject: [PATCH 1/2] Fix: context for afterFind --- spec/CloudCode.spec.js | 10 ++++++++++ src/RestQuery.js | 18 +++++++++++++++--- src/rest.js | 25 ++++++++++++++++++++++--- src/triggers.js | 16 ++++++++++++---- 4 files changed, 59 insertions(+), 10 deletions(-) diff --git a/spec/CloudCode.spec.js b/spec/CloudCode.spec.js index 7bc39a43bb..9b95bbf8b0 100644 --- a/spec/CloudCode.spec.js +++ b/spec/CloudCode.spec.js @@ -3157,4 +3157,14 @@ describe('afterLogin hook', () => { await Parse.Cloud.run('contextTest', {}, { context: { a: 'a' } }); }); + + it('afterFind should have access to context', async () => { + Parse.Cloud.afterFind('TestObject', req => { + expect(req.context.a).toEqual('a'); + }); + const obj = new TestObject(); + await obj.save(); + const query = new Parse.Query(TestObject); + await query.find({ context: { a: 'a' } }); + }); }); diff --git a/src/RestQuery.js b/src/RestQuery.js index 78fd022bc1..ef3846daec 100644 --- a/src/RestQuery.js +++ b/src/RestQuery.js @@ -25,7 +25,8 @@ function RestQuery( restWhere = {}, restOptions = {}, clientSDK, - runAfterFind = true + runAfterFind = true, + context ) { this.config = config; this.auth = auth; @@ -36,6 +37,7 @@ function RestQuery( this.runAfterFind = runAfterFind; this.response = null; this.findOptions = {}; + this.context = context || {}; if (!this.auth.isMaster) { if (this.className == '_Session') { @@ -222,7 +224,16 @@ RestQuery.prototype.each = function (callback) { return !finished; }, async () => { - const query = new RestQuery(config, auth, className, restWhere, restOptions, clientSDK); + const query = new RestQuery( + config, + auth, + className, + restWhere, + restOptions, + clientSDK, + this.runAfterFind, + this.context + ); const { results } = await query.execute(); results.forEach(callback); finished = results.length < restOptions.limit; @@ -772,7 +783,8 @@ RestQuery.prototype.runAfterFindTrigger = function () { this.className, this.response.results, this.config, - parseQuery + parseQuery, + this.context ) .then(results => { // Ensure we properly set the className back diff --git a/src/rest.js b/src/rest.js index f443d59480..fca3497a5d 100644 --- a/src/rest.js +++ b/src/rest.js @@ -39,7 +39,16 @@ function find(config, auth, className, restWhere, restOptions, clientSDK, contex .then(result => { restWhere = result.restWhere || restWhere; restOptions = result.restOptions || restOptions; - const query = new RestQuery(config, auth, className, restWhere, restOptions, clientSDK); + const query = new RestQuery( + config, + auth, + className, + restWhere, + restOptions, + clientSDK, + true, + context + ); return query.execute(); }); } @@ -62,7 +71,16 @@ const get = (config, auth, className, objectId, restOptions, clientSDK, context) .then(result => { restWhere = result.restWhere || restWhere; restOptions = result.restOptions || restOptions; - const query = new RestQuery(config, auth, className, restWhere, restOptions, clientSDK); + const query = new RestQuery( + config, + auth, + className, + restWhere, + restOptions, + clientSDK, + true, + context + ); return query.execute(); }); }; @@ -187,7 +205,8 @@ function update(config, auth, className, restWhere, restObject, clientSDK, conte restWhere, undefined, undefined, - false + false, + context ).execute({ op: 'update', }); diff --git a/src/triggers.js b/src/triggers.js index eeb86c8369..47331675b0 100644 --- a/src/triggers.js +++ b/src/triggers.js @@ -237,12 +237,12 @@ export function getRequestObject( if (originalParseObject) { request.original = originalParseObject; } - if ( triggerType === Types.beforeSave || triggerType === Types.afterSave || triggerType === Types.beforeDelete || - triggerType === Types.afterDelete + triggerType === Types.afterDelete || + triggerType === Types.afterFind ) { // Set a copy of the context on the request object. request.context = Object.assign({}, context); @@ -388,13 +388,21 @@ function logTriggerErrorBeforeHook(triggerType, className, input, auth, error) { ); } -export function maybeRunAfterFindTrigger(triggerType, auth, className, objects, config, query) { +export function maybeRunAfterFindTrigger( + triggerType, + auth, + className, + objects, + config, + query, + context +) { return new Promise((resolve, reject) => { const trigger = getTrigger(className, triggerType, config.applicationId); if (!trigger) { return resolve(); } - const request = getRequestObject(triggerType, auth, null, null, config); + const request = getRequestObject(triggerType, auth, null, null, config, context); if (query) { request.query = query; } From cf2de91c51f0e599c42b5147fba6e3784702b5c7 Mon Sep 17 00:00:00 2001 From: dblythy Date: Thu, 17 Dec 2020 17:13:43 +1100 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa847e4a99..a485c160f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### master [Full Changelog](https://github.com/parse-community/parse-server/compare/4.5.0...master) +- FIX: request.context for afterFind triggers. [#7078](https://github.com/parse-community/parse-server/pull/7078). Thanks to [dblythy](https://github.com/dblythy) - 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) ### 4.5.0