Skip to content
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

Pass context in beforeDelete, afterDelete, beforeFind and Parse.Cloud.run. #6666

Merged
merged 16 commits into from
Jul 10, 2020
54 changes: 54 additions & 0 deletions spec/CloudCode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2968,4 +2968,58 @@ describe('afterLogin hook', () => {
obj.set("obj2", obj2);
await obj.save(null, { context: { a: 'a' } });
});

it('should have access to context as saveAll argument', async () => {
Parse.Cloud.beforeSave('TestObject', (req) => {
expect(req.context.a).toEqual('a');
});
Parse.Cloud.afterSave('TestObject', (req) => {
expect(req.context.a).toEqual('a');
});
const obj1 = new TestObject();
const obj2 = new TestObject();
await Parse.Object.saveAll([obj1, obj2], { context: { a: 'a' }});
});

it('should have access to context as destroyAll argument', async () => {
Parse.Cloud.beforeDelete('TestObject', (req) => {
expect(req.context.a).toEqual('a');
});
Parse.Cloud.afterDelete('TestObject', (req) => {
expect(req.context.a).toEqual('a');
});
const obj1 = new TestObject();
const obj2 = new TestObject();
await Parse.Object.saveAll([obj1, obj2]);
await Parse.Object.destroyAll([obj1, obj2], { context: { a: 'a' } });
});

it('should have access to context as destroy a object', async () => {
Parse.Cloud.beforeDelete('TestObject', (req) => {
expect(req.context.a).toEqual('a');
});
Parse.Cloud.afterDelete('TestObject', (req) => {
expect(req.context.a).toEqual('a');
});
const obj = new TestObject();
await obj.save();
await obj.destroy({ context: { a: 'a' } });
});

it('should have access to context in beforeFind hook', async () => {
Parse.Cloud.beforeFind('TestObject', (req) => {
expect(req.context.a).toEqual('a');
});
const query = new Parse.Query('TestObject');
return query.find({ context: { a: 'a' } });
});

it('should have access to context when cloud function is called.', async () => {
Parse.Cloud.define('contextTest', async (req) => {
expect(req.context.a).toEqual('a');
return {};
});

await Parse.Cloud.run('contextTest', {}, { context: { a: 'a' } });
});
});
7 changes: 4 additions & 3 deletions src/GraphQL/helpers/objectsMutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const createObject = async (className, fields, config, auth, info) => {
fields = {};
}

return (await rest.create(config, auth, className, fields, info.clientSDK))
return (await rest.create(config, auth, className, fields, info.clientSDK, info.context))
.response;
};

Expand All @@ -27,12 +27,13 @@ const updateObject = async (
className,
{ objectId },
fields,
info.clientSDK
info.clientSDK,
info.context
)).response;
};

const deleteObject = async (className, objectId, config, auth, info) => {
await rest.del(config, auth, className, objectId, info.clientSDK);
await rest.del(config, auth, className, objectId, info.context);
mtrezza marked this conversation as resolved.
Show resolved Hide resolved
return true;
};

Expand Down
9 changes: 6 additions & 3 deletions src/GraphQL/helpers/objectsQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ const getObject = async (
className,
objectId,
options,
info.clientSDK
info.clientSDK,
info.context
);

if (!response.results || response.results.length == 0) {
Expand Down Expand Up @@ -142,7 +143,8 @@ const findObjects = async (
className,
where,
preCountOptions,
info.clientSDK
info.clientSDK,
info.context
)
).count;
if ((skip || 0) + limit < preCount) {
Expand Down Expand Up @@ -222,7 +224,8 @@ const findObjects = async (
className,
where,
options,
info.clientSDK
info.clientSDK,
info.context
);
results = findResult.results;
count = findResult.count;
Expand Down
3 changes: 2 additions & 1 deletion src/GraphQL/loaders/usersQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ const getUserFromSessionToken = async (
'_Session',
{ sessionToken },
options,
info.clientVersion
info.clientVersion,
info.context,
);
if (
!response.results ||
Expand Down
1 change: 1 addition & 0 deletions src/ParseServerRESTController.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ function ParseServerRESTController(applicationId, router) {
info: {
applicationId: applicationId,
sessionToken: options.sessionToken,
context: options.context || {}, // Add context
},
query,
};
Expand Down
9 changes: 2 additions & 7 deletions src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function RestWrite(
data,
originalData,
clientSDK,
context,
action
) {
if (auth.isReadOnly) {
Expand All @@ -46,18 +47,12 @@ function RestWrite(
this.clientSDK = clientSDK;
this.storage = {};
this.runOptions = {};
this.context = {};
this.context = context || {};

if (action) {
this.runOptions.action = action;
}

// Parse context
if (data._context && data._context instanceof Object) {
this.context = data._context;
delete data._context;
}

if (!query) {
if (this.config.allowCustomObjectId) {
if (
Expand Down
3 changes: 2 additions & 1 deletion src/Routers/AggregateRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export class AggregateRouter extends ClassesRouter {
this.className(req),
body.where,
options,
req.info.clientSDK
req.info.clientSDK,
req.info.context,
)
.then(response => {
for (const result of response.results) {
Expand Down
3 changes: 2 additions & 1 deletion src/Routers/AudiencesRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export class AudiencesRouter extends ClassesRouter {
'_Audience',
body.where,
options,
req.info.clientSDK
req.info.clientSDK,
req.info.context,
)
.then(response => {
response.results.forEach(item => {
Expand Down
11 changes: 7 additions & 4 deletions src/Routers/ClassesRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export class ClassesRouter extends PromiseRouter {
this.className(req),
body.where,
options,
req.info.clientSDK
req.info.clientSDK,
req.info.context,
)
.then(response => {
return { response: response };
Expand Down Expand Up @@ -120,7 +121,8 @@ export class ClassesRouter extends PromiseRouter {
req.auth,
this.className(req),
req.body,
req.info.clientSDK
req.info.clientSDK,
req.info.context
);
}

Expand All @@ -132,7 +134,8 @@ export class ClassesRouter extends PromiseRouter {
this.className(req),
where,
req.body,
req.info.clientSDK
req.info.clientSDK,
req.info.context
);
}

Expand All @@ -143,7 +146,7 @@ export class ClassesRouter extends PromiseRouter {
req.auth,
this.className(req),
req.params.objectId,
req.info.clientSDK
mtrezza marked this conversation as resolved.
Show resolved Hide resolved
req.info.context
)
.then(() => {
return { response: {} };
Expand Down
9 changes: 6 additions & 3 deletions src/Routers/CloudCodeRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ export class CloudCodeRouter extends PromiseRouter {
req.auth,
'_JobSchedule',
formatJobSchedule(job_schedule),
req.client
req.client,
req.info.context
);
}

Expand All @@ -102,7 +103,9 @@ export class CloudCodeRouter extends PromiseRouter {
req.auth,
'_JobSchedule',
{ objectId },
formatJobSchedule(job_schedule)
formatJobSchedule(job_schedule),
undefined,
req.info.context
)
.then(response => {
return {
Expand All @@ -114,7 +117,7 @@ export class CloudCodeRouter extends PromiseRouter {
static deleteJob(req) {
const { objectId } = req.params;
return rest
.del(req.config, req.auth, '_JobSchedule', objectId)
.del(req.config, req.auth, '_JobSchedule', objectId, req.info.context)
.then(response => {
return {
response,
Expand Down
1 change: 1 addition & 0 deletions src/Routers/FunctionsRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export class FunctionsRouter extends PromiseRouter {
headers: req.config.headers,
ip: req.config.ip,
functionName,
context: req.info.context,
};

if (theValidator && typeof theValidator === 'function') {
Expand Down
3 changes: 2 additions & 1 deletion src/Routers/IAPValidationRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ function getFileForProductIdentifier(productIdentifier, req) {
'_Product',
{ productIdentifier: productIdentifier },
undefined,
req.info.clientSDK
req.info.clientSDK,
req.info.context
)
.then(function(result) {
const products = result.results;
Expand Down
3 changes: 2 additions & 1 deletion src/Routers/InstallationsRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export class InstallationsRouter extends ClassesRouter {
'_Installation',
body.where,
options,
req.info.clientSDK
req.info.clientSDK,
req.info.context
)
.then(response => {
return { response: response };
Expand Down
3 changes: 2 additions & 1 deletion src/Routers/SessionsRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export class SessionsRouter extends ClassesRouter {
'_Session',
{ sessionToken: req.info.sessionToken },
undefined,
req.info.clientSDK
req.info.clientSDK,
req.info.context
)
.then(response => {
if (!response.results || response.results.length == 0) {
Expand Down
9 changes: 6 additions & 3 deletions src/Routers/UsersRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ export class UsersRouter extends ClassesRouter {
'_Session',
{ sessionToken },
{ include: 'user' },
req.info.clientSDK
req.info.clientSDK,
req.info.context
)
.then(response => {
if (
Expand Down Expand Up @@ -302,7 +303,8 @@ export class UsersRouter extends ClassesRouter {
'_Session',
{ sessionToken: req.info.sessionToken },
undefined,
req.info.clientSDK
req.info.clientSDK,
req.info.context
)
.then(records => {
if (records.results && records.results.length) {
Expand All @@ -311,7 +313,8 @@ export class UsersRouter extends ClassesRouter {
req.config,
Auth.master(req.config),
'_Session',
records.results[0].objectId
records.results[0].objectId,
req.info.context
)
.then(() => {
this._runAfterLogoutTrigger(req, records.results[0]);
Expand Down
3 changes: 0 additions & 3 deletions src/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ function handleBatch(router, req) {
info: req.info,
};

// Add context to request body
if (req.body._context) { request.body._context = req.body._context; }

return router
.tryRouteRequest(restRequest.method, routablePath, request)
.then(
Expand Down
5 changes: 5 additions & 0 deletions src/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function handleParseHeaders(req, res, next) {
dotNetKey: req.get('X-Parse-Windows-Key'),
restAPIKey: req.get('X-Parse-REST-API-Key'),
clientVersion: req.get('X-Parse-Client-Version'),
context: {},
};

var basicAuth = httpAuth(req);
Expand Down Expand Up @@ -103,6 +104,10 @@ export function handleParseHeaders(req, res, next) {
info.masterKey = req.body._MasterKey;
delete req.body._MasterKey;
}
if (req.body._context && req.body._context instanceof Object) {
info.context = req.body._context;
delete req.body._context;
}
if (req.body._ContentType) {
req.headers['content-type'] = req.body._ContentType;
delete req.body._ContentType;
Expand Down
Loading