Parse Server Access PFUser in BeforeSave Trigger

468 Views Asked by At

I need to check a property of my PFUser's in beforeSave triggers for each of my classes to determine if that user should be allowed to edit the piece of data they are attempting to edit.

For example, if a non-admin PFUser is attempting to edit or add to a class they shouldn't be allowed to, I want to prevent that in the beforeSave trigger. I access the keys being edited using dirtyKeys.

Parse-Server doesn't support .currentUser() like the old Parse server used to. How can I access the PFUser who is making the request? Is there a way to do it besides through session tokens?

Parse.Cloud.beforeSave("Class", function(request, response) {

//Get the keys that're being edited and iterate through them
var dirtyKeys = request.object.dirtyKeys();
for (var i = 0; i < dirtyKeys.length; ++i) {
  var dirtyKey = dirtyKeys[i];

  //Allow or don't allow editing of each key
  if (userObject.get("<KEY>")) {
    console.log('Class before save trigger IS key');
    //ADD CLASS SPECIFIC FUNCTIONALITY HERE

  } else {
    console.log('Class before save trigger NOT key');
    //ADD CLASS SPECIFIC FUNCTIONALITY HERE

  }
}

});

1

There are 1 best solutions below

0
On

Turns out the answer is much more obvious than I anticipated and was in the docs but I overlooked it despite my searching.

Since Parse.User.current() isn't working in Parse Server, the replacement is simply request.user. I was able to easily access all the data I needed from this and am good to go.

var user = request.user; // request.user replaces Parse.User.current()