Validates permission before calling beforeSave trigger (#5546)

* Test to reproduce the problem

* Validating update before calling beforeSave trigger

* Fixing lint

* Commenting code

* Improving the code
This commit is contained in:
Antonio Davi Macedo Coelho de Castro
2019-05-11 10:37:27 -07:00
committed by Arthur Cinader
parent 2cc21bf1f2
commit 90c81c1750
3 changed files with 268 additions and 2 deletions

View File

@@ -220,6 +220,38 @@ RestWrite.prototype.runBeforeSaveTrigger = function() {
}
return Promise.resolve()
.then(() => {
// Before calling the trigger, validate the permissions for the save operation
let databasePromise = null;
if (this.query) {
// Validate for updating
databasePromise = this.config.database.update(
this.className,
this.query,
this.data,
this.runOptions,
false,
true
);
} else {
// Validate for creating
databasePromise = this.config.database.create(
this.className,
this.data,
this.runOptions,
true
);
}
// In the case that there is no permission for the operation, it throws an error
return databasePromise.then(result => {
if (!result || result.length <= 0) {
throw new Parse.Error(
Parse.Error.OBJECT_NOT_FOUND,
'Object not found.'
);
}
});
})
.then(() => {
return triggers.maybeRunTrigger(
triggers.Types.beforeSave,