Skip to content

Commit c2abbae

Browse files
authored
fix(Users): Makes sure verifying emails triggers hooks and liveQuery (#3851)
* Use RestWrite when verifying emails so hooks are called (as master) * Fixes tests for postgres * nit * Makes rest.update support a full where instead of objectId * Use rest.update to guaranteed proper beforeSave and liveQuery calls
1 parent 73aafa2 commit c2abbae

File tree

7 files changed

+29
-31
lines changed

7 files changed

+29
-31
lines changed

spec/ParseInstallation.spec.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,11 @@ describe('Installations', () => {
339339
.then(() => database.adapter.find('_Installation', installationSchema, {}, {}))
340340
.then(results => {
341341
expect(results.length).toEqual(1);
342-
var id = results[0].objectId;
342+
var objectId = results[0].objectId;
343343
var update = {
344344
'channels': ['baz']
345345
};
346-
return rest.update(config, auth.nobody(config), '_Installation', id, update);
346+
return rest.update(config, auth.nobody(config), '_Installation', { objectId }, update);
347347
})
348348
.then(() => database.adapter.find('_Installation', installationSchema, {}, {}))
349349
.then(results => {
@@ -370,7 +370,7 @@ describe('Installations', () => {
370370
.then(results => {
371371
expect(results.length).toEqual(1);
372372
input = { 'installationId': installId2 };
373-
return rest.update(config, auth.nobody(config), '_Installation', results[0].objectId, input);
373+
return rest.update(config, auth.nobody(config), '_Installation', { objectId: results[0].objectId }, input);
374374
}).then(() => {
375375
fail('Updating the installation should have failed.');
376376
done();
@@ -393,7 +393,7 @@ describe('Installations', () => {
393393
.then(results => {
394394
expect(results.length).toEqual(1);
395395
input = { 'deviceToken': b };
396-
return rest.update(config, auth.nobody(config), '_Installation', results[0].objectId, input);
396+
return rest.update(config, auth.nobody(config), '_Installation', { objectId: results[0].objectId }, input);
397397
}).then(() => {
398398
fail('Updating the installation should have failed.');
399399
}).catch((error) => {
@@ -421,7 +421,7 @@ describe('Installations', () => {
421421
'deviceToken': u,
422422
'deviceType': 'ios'
423423
};
424-
return rest.update(config, auth.nobody(config), '_Installation', results[0].objectId, input);
424+
return rest.update(config, auth.nobody(config), '_Installation', { objectId: results[0].objectId }, input);
425425
})
426426
.then(() => database.adapter.find('_Installation', installationSchema, {}, {}))
427427
.then(results => {
@@ -448,7 +448,7 @@ describe('Installations', () => {
448448
input = {
449449
'deviceType': 'ios'
450450
};
451-
return rest.update(config, auth.nobody(config), '_Installation', results[0].objectId, input);
451+
return rest.update(config, auth.nobody(config), '_Installation', { objectId: results[0].objectId }, input);
452452
}).then(() => {
453453
fail('Should not have been able to update Installation.');
454454
done();
@@ -472,7 +472,7 @@ describe('Installations', () => {
472472
input = {
473473
'custom': 'allowed'
474474
};
475-
return rest.update(config, auth.nobody(config), '_Installation', results[0].objectId, input);
475+
return rest.update(config, auth.nobody(config), '_Installation', { objectId: results[0].objectId }, input);
476476
})
477477
.then(() => database.adapter.find('_Installation', installationSchema, {}, {}))
478478
.then(results => {
@@ -514,7 +514,7 @@ describe('Installations', () => {
514514
'objectId': secondObject.objectId,
515515
'deviceToken': t
516516
};
517-
return rest.update(config, auth.nobody(config), '_Installation', secondObject.objectId, input);
517+
return rest.update(config, auth.nobody(config), '_Installation', { objectId: secondObject.objectId }, input);
518518
})
519519
.then(() => database.adapter.find('_Installation', installationSchema, {objectId: firstObject.objectId}, {}))
520520
.then(results => {
@@ -562,7 +562,7 @@ describe('Installations', () => {
562562
'installationId': installId2,
563563
'deviceToken': t
564564
};
565-
return rest.update(config, auth.nobody(config), '_Installation', secondObject.objectId, input);
565+
return rest.update(config, auth.nobody(config), '_Installation', { objectId: secondObject.objectId }, input);
566566
})
567567
.then(() => delay(100))
568568
.then(() => database.adapter.find('_Installation', installationSchema, {objectId: firstObject.objectId}, {}))
@@ -619,7 +619,7 @@ describe('Installations', () => {
619619
'deviceToken': t,
620620
'channels': []
621621
};
622-
return rest.update(config, auth.nobody(config), '_Installation', results[0].objectId, input);
622+
return rest.update(config, auth.nobody(config), '_Installation', { objectId: results[0].objectId }, input);
623623
})
624624
.then(() => database.adapter.find('_Installation', installationSchema, {}, {}))
625625
.then(results => {
@@ -657,7 +657,7 @@ describe('Installations', () => {
657657
'installationId': installId,
658658
'deviceType': 'ios'
659659
};
660-
return rest.update(config, auth.nobody(config), '_Installation', results[0].objectId, input);
660+
return rest.update(config, auth.nobody(config), '_Installation', { objectId: results[0].objectId }, input);
661661
})
662662
.then(() => database.adapter.find('_Installation', installationSchema, {}, {}))
663663
.then(results => {
@@ -699,7 +699,7 @@ describe('Installations', () => {
699699
'amount': 1
700700
}
701701
};
702-
return rest.update(config, auth.nobody(config), '_Installation', results[0].objectId, input);
702+
return rest.update(config, auth.nobody(config), '_Installation', { objectId: results[0].objectId }, input);
703703
})
704704
.then(() => database.adapter.find('_Installation', installationSchema, {}, {}))
705705
.then(results => {
@@ -744,7 +744,7 @@ describe('Installations', () => {
744744
'deviceToken': t,
745745
'deviceType': 'ios'
746746
};
747-
return rest.update(config, auth.nobody(config), '_Installation', installObj.objectId, input);
747+
return rest.update(config, auth.nobody(config), '_Installation', { objectId: installObj.objectId }, input);
748748
})
749749
.then(() => database.adapter.find('_Installation', installationSchema, { objectId: tokenObj.objectId }, {}))
750750
.then(results => {
@@ -791,7 +791,7 @@ describe('Installations', () => {
791791
'amount': 1
792792
}
793793
};
794-
return rest.update(config, auth.nobody(config), '_Installation', installObj.objectId, input);
794+
return rest.update(config, auth.nobody(config), '_Installation', { objectId: installObj.objectId }, input);
795795
})
796796
.then(() => database.adapter.find('_Installation', installationSchema, { objectId: tokenObj.objectId }, {}))
797797
.then(results => {

spec/rest.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('rest create', () => {
6464
expect(mob.subdoc.wu).toBe('tan');
6565
expect(typeof mob.objectId).toEqual('string');
6666
const obj = { 'subdoc.wu': 'clan' };
67-
return rest.update(config, auth.nobody(config), 'MyClass', mob.objectId, obj)
67+
return rest.update(config, auth.nobody(config), 'MyClass', { objectId: mob.objectId }, obj)
6868
})
6969
.then(() => database.adapter.find('MyClass', { fields: {} }, {}, {}))
7070
.then(results => {
@@ -195,7 +195,7 @@ describe('rest create', () => {
195195
objectId = r.response.objectId;
196196
return auth.getAuthForSessionToken({config, sessionToken: r.response.sessionToken })
197197
}).then((sessionAuth) => {
198-
return rest.update(config, sessionAuth, '_User', objectId, updatedData);
198+
return rest.update(config, sessionAuth, '_User', { objectId }, updatedData);
199199
}).then(() => {
200200
return Parse.User.logOut().then(() => {
201201
return Parse.User.logIn('hello', 'world');
@@ -435,7 +435,7 @@ describe('rest update', () => {
435435
createdAt: {__type: "Date", iso: newCreatedAt}, // should be ignored
436436
};
437437

438-
return rest.update(config, nobody, className, objectId, restObject).then(() => {
438+
return rest.update(config, nobody, className, { objectId }, restObject).then(() => {
439439
const restWhere = {
440440
objectId: objectId,
441441
};

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ const toPostgresValue = value => {
5858
}
5959

6060
const transformValue = value => {
61-
if (value.__type === 'Pointer') {
61+
if (typeof value === 'object' &&
62+
value.__type === 'Pointer') {
6263
return value.objectId;
6364
}
6465
return value;

src/Controllers/UserController.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,13 @@ export class UserController extends AdaptableController {
5959

6060
updateFields._email_verify_token_expires_at = {__op: 'Delete'};
6161
}
62-
62+
const masterAuth = Auth.master(this.config);
6363
var checkIfAlreadyVerified = new RestQuery(this.config, Auth.master(this.config), '_User', {username: username, emailVerified: true});
6464
return checkIfAlreadyVerified.execute().then(result => {
6565
if (result.results.length) {
6666
return Promise.resolve(result.results.length[0]);
6767
}
68-
return this.config.database.update('_User', query, updateFields).then((document) => {
69-
if (!document) {
70-
throw undefined
71-
}
72-
return Promise.resolve(document);
73-
})
68+
return rest.update(this.config, masterAuth, '_User', query, updateFields);
7469
});
7570
}
7671

@@ -229,7 +224,7 @@ export class UserController extends AdaptableController {
229224

230225
// Mark this private
231226
function updateUserPassword(userId, password, config) {
232-
return rest.update(config, Auth.master(config), '_User', userId, {
227+
return rest.update(config, Auth.master(config), '_User', { objectId: userId }, {
233228
password: password
234229
});
235230
}

src/RestWrite.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,8 @@ RestWrite.prototype.transformUser = function() {
360360
throw new Parse.Error(Parse.Error.OPERATION_FORBIDDEN, error);
361361
}
362362

363-
if (this.query) {
363+
// Do not cleanup session if objectId is not set
364+
if (this.query && this.objectId()) {
364365
// If we're updating a _User object, we need to clear out the cache for that user. Find all their
365366
// session tokens, and remove them from the cache.
366367
promise = new RestQuery(this.config, Auth.master(this.config), '_Session', {

src/Routers/ClassesRouter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ export class ClassesRouter extends PromiseRouter {
103103
}
104104

105105
handleUpdate(req) {
106-
return rest.update(req.config, req.auth, req.params.className, req.params.objectId, req.body, req.info.clientSDK);
106+
const where = { objectId: req.params.objectId }
107+
return rest.update(req.config, req.auth, req.params.className, where, req.body, req.info.clientSDK);
107108
}
108109

109110
handleDelete(req) {

src/rest.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,14 @@ function create(config, auth, className, restObject, clientSDK) {
113113
// Returns a promise that contains the fields of the update that the
114114
// REST API is supposed to return.
115115
// Usually, this is just updatedAt.
116-
function update(config, auth, className, objectId, restObject, clientSDK) {
116+
function update(config, auth, className, restWhere, restObject, clientSDK) {
117117
enforceRoleSecurity('update', className, auth);
118118

119119
return Promise.resolve().then(() => {
120120
const hasTriggers = checkTriggers(className, config, ['beforeSave', 'afterSave']);
121121
const hasLiveQuery = checkLiveQuery(className, config);
122122
if (hasTriggers || hasLiveQuery) {
123-
return find(config, Auth.master(config), className, {objectId: objectId});
123+
return find(config, Auth.master(config), className, restWhere);
124124
}
125125
return Promise.resolve({});
126126
}).then((response) => {
@@ -129,7 +129,7 @@ function update(config, auth, className, objectId, restObject, clientSDK) {
129129
originalRestObject = response.results[0];
130130
}
131131

132-
var write = new RestWrite(config, auth, className, {objectId: objectId}, restObject, originalRestObject, clientSDK);
132+
var write = new RestWrite(config, auth, className, restWhere, restObject, originalRestObject, clientSDK);
133133
return write.execute();
134134
});
135135
}

0 commit comments

Comments
 (0)