Search code examples
javascriptphpdatabaseparse-platform

Parse(.com) php SDK "master key" instead of password for user query


I can't seem to find any documentation on this. All I simply look to do is update a users info within my table without having the need of the users password. I'd like to simply look up the email of the user and update information for that user. Not change the password but other columns in the table associated with that user.

Example query:

$query = new ParseQuery("_User");
$query->equalTo("username", "user001");
foreach ($result as $doc) {
$doc->set('name', "new info");
$doc->save();
}

Through Parse's php SDK, how do I change a users information just by knowing the email/username of the user? without logging in?

I heard you can do this by using your "master key" instead of a password or through a cloud job but I don't know where to start with that.

I see in the API ref: save(boolean $useMasterKey = true) : null

Anyone have any ideas?

P.S: I have found this response but I'm still confused how to execute this (https://www.parse.com/questions/users-editing-other-users-in-javascript-sdk).


Solution

  • Modifying a user should probably be done on the server side using a cloud function. You can make a cloud function in javascript on your parse server by editing the cloud/main.js file. More documentation here.

    Basically, you can create a function like this:

    Parse.Cloud.define("modifyuser", function(request, response){
      var query = new Parse.Query(Parse.User);
      query.equalTo('objectId', request.params.objectId);
      query.find({
        useMasterKey: true,
        success: function(results){
          if(results.length>0){
            var user = results[0];
            user.set("SOMEPARAMETER",true);
            user.save().then(
                function(result){
                },
                function(error){
                    console.log("Error: " + error.code + " " + error.message);
                });
            }
          }
        },
        error: function(error){
                response.error('query error: '+ error.code + " : " + error.message);
        }
      });
    });
    

    And call this function from your client.

    $ParseCloud::run("modifyuser", ["objectId" => "THEUSEROBJECTID"]);