Fix: context for afterFind (#7078)

* Fix: context for afterFind

* Update CHANGELOG.md

Co-authored-by: Manuel <trezza.m@gmail.com>
This commit is contained in:
dblythy
2020-12-19 00:54:48 +11:00
committed by GitHub
parent 97c3046f3f
commit 41a052c2c3
5 changed files with 60 additions and 10 deletions

View File

@@ -7,6 +7,7 @@ __BREAKING CHANGES:__
- 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). - 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).
___ ___
- 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) - 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)
- FIX: request.context for afterFind triggers. [#7078](https://github.com/parse-community/parse-server/pull/7078). Thanks to [dblythy](https://github.com/dblythy)
### 4.5.0 ### 4.5.0
[Full Changelog](https://github.com/parse-community/parse-server/compare/4.4.0...4.5.0) [Full Changelog](https://github.com/parse-community/parse-server/compare/4.4.0...4.5.0)

View File

@@ -3157,4 +3157,14 @@ describe('afterLogin hook', () => {
await Parse.Cloud.run('contextTest', {}, { context: { a: 'a' } }); 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' } });
});
}); });

View File

@@ -25,7 +25,8 @@ function RestQuery(
restWhere = {}, restWhere = {},
restOptions = {}, restOptions = {},
clientSDK, clientSDK,
runAfterFind = true runAfterFind = true,
context
) { ) {
this.config = config; this.config = config;
this.auth = auth; this.auth = auth;
@@ -36,6 +37,7 @@ function RestQuery(
this.runAfterFind = runAfterFind; this.runAfterFind = runAfterFind;
this.response = null; this.response = null;
this.findOptions = {}; this.findOptions = {};
this.context = context || {};
if (!this.auth.isMaster) { if (!this.auth.isMaster) {
if (this.className == '_Session') { if (this.className == '_Session') {
@@ -222,7 +224,16 @@ RestQuery.prototype.each = function (callback) {
return !finished; return !finished;
}, },
async () => { 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(); const { results } = await query.execute();
results.forEach(callback); results.forEach(callback);
finished = results.length < restOptions.limit; finished = results.length < restOptions.limit;
@@ -772,7 +783,8 @@ RestQuery.prototype.runAfterFindTrigger = function () {
this.className, this.className,
this.response.results, this.response.results,
this.config, this.config,
parseQuery parseQuery,
this.context
) )
.then(results => { .then(results => {
// Ensure we properly set the className back // Ensure we properly set the className back

View File

@@ -39,7 +39,16 @@ function find(config, auth, className, restWhere, restOptions, clientSDK, contex
.then(result => { .then(result => {
restWhere = result.restWhere || restWhere; restWhere = result.restWhere || restWhere;
restOptions = result.restOptions || restOptions; 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(); return query.execute();
}); });
} }
@@ -62,7 +71,16 @@ const get = (config, auth, className, objectId, restOptions, clientSDK, context)
.then(result => { .then(result => {
restWhere = result.restWhere || restWhere; restWhere = result.restWhere || restWhere;
restOptions = result.restOptions || restOptions; 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(); return query.execute();
}); });
}; };
@@ -187,7 +205,8 @@ function update(config, auth, className, restWhere, restObject, clientSDK, conte
restWhere, restWhere,
undefined, undefined,
undefined, undefined,
false false,
context
).execute({ ).execute({
op: 'update', op: 'update',
}); });

View File

@@ -237,12 +237,12 @@ export function getRequestObject(
if (originalParseObject) { if (originalParseObject) {
request.original = originalParseObject; request.original = originalParseObject;
} }
if ( if (
triggerType === Types.beforeSave || triggerType === Types.beforeSave ||
triggerType === Types.afterSave || triggerType === Types.afterSave ||
triggerType === Types.beforeDelete || triggerType === Types.beforeDelete ||
triggerType === Types.afterDelete triggerType === Types.afterDelete ||
triggerType === Types.afterFind
) { ) {
// Set a copy of the context on the request object. // Set a copy of the context on the request object.
request.context = Object.assign({}, context); 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) => { return new Promise((resolve, reject) => {
const trigger = getTrigger(className, triggerType, config.applicationId); const trigger = getTrigger(className, triggerType, config.applicationId);
if (!trigger) { if (!trigger) {
return resolve(); return resolve();
} }
const request = getRequestObject(triggerType, auth, null, null, config); const request = getRequestObject(triggerType, auth, null, null, config, context);
if (query) { if (query) {
request.query = query; request.query = query;
} }