parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

useMasterKey in cloudCode not working as expected

Open REggar opened this issue 10 years ago • 25 comments

We had some Cloud Code like this in Parse:


Parse.Cloud.define('deleteUser', function (request, response) {`
    var User = Parse.Object.extend('_User');
    var query = new Parse.Query(User);

    query.get(request.params.user).then(function (result) {
        result.destroy();
    }).then(function (results) {
        response.success('User deleted');
    }, function (error) {
        response.error(error);
    })
});

It worked fine in Cloud Code however, it seems to be broken in parse-server, on debugging it looks like Parse.Cloud.useMasterKey(); is not being applied to the options of all ParseObject calls. This is required because the ACL prevents anyone creating additional items in the table.

REggar avatar Jan 29 '16 16:01 REggar

#13 In the top of the cloud code file, try:

Parse.serverURL = 'http://localhost:1337/parse';

wagnercsfilho avatar Jan 29 '16 16:01 wagnercsfilho

No such luck, it's worth adding that I'm getting errors on .save() of ParseObjects as well.

REggar avatar Jan 29 '16 16:01 REggar

Will take a look at this...

gfosco avatar Jan 29 '16 18:01 gfosco

+1. I can't .save() objects in Cloud Code via parse-sever if it requires useMasterKey(). In a beforeSave() function if I return success() early (before I create/save a protected object) it works fine, but if the code to create/save a sub-object runs I get an error:

ParseError { code: 141, message: 'User beforeSave error = 2 the service is currently unavailable' }

EDIT: I found the answer to my problem: #132

silvermana avatar Feb 03 '16 13:02 silvermana

Much like how Parse.User.current() is invalid in a node script, because of the global nature, so is Parse.Cloud.useMasterKey(), because it is global and it would cause other requests to use the master key even if it was not intended.

The correct approach is to always pass a useMasterKey: true option to saves and queries which need to use the master key. We are going to document this as soon as possible, but I wanted to get this out to you and be able to close some issues.

In a save call, the options block is the second param:

obj.save(null, { useMasterKey: true });

In a query, it's the first.

q.get({ useMasterKey: true }).then(.......)
q.find({ useMasterKey: true}).then(......)

Please update your code to follow this and open a new issue if you have problems. Thank you!

gfosco avatar Feb 06 '16 00:02 gfosco

@gfosco you should move this info to the guide imho.

some people like myself with not a lot of node js experience will find it helpful

sivi29y avatar May 11 '16 17:05 sivi29y

I have the same problem, I tried to use @gfosco answer but I think I did it wrong. Could you help me? I read https://github.com/ParsePlatform/Parse-Server/wiki/Compatibility-with-Hosted-Parse#Cloud-Code too but it doesn't seem to work.

Parse.Cloud.define("removeFriendRequest", function(request, response) {
  Parse.Cloud.useMasterKey();

  console.log("removeFriendRequest: remove userid: " + request.params.userIdToRemove + " from user: " + request.params.userId);
  var query = new Parse.Query(Parse.User);
  query.equalTo("objectId", request.params.userId);

  // Get the first user which matches the above constraints.
  query.first({
    success: function(anotherUser) {
        anotherUser.remove("friends", request.params.userIdToRemove);
        anotherUser.save(null, {
        success: function(user) {
          response.success("removeFriendRequest updated");
        },
        error: function(user, error) {
          response.error(error);
        }
        });
    }
  });
});

Parse.Cloud.define("addGroupChatCount", function(request, response) {
  Parse.Cloud.useMasterKey();

  console.log("addGroupChatCount: added userid: " + request.params.userId + " for users: " + request.params.userIds + " for weevent: " + request.params.weeventId);
  var GroupMessage = Parse.Object.extend("GroupMessage");
  var query = new Parse.Query(GroupMessage);
  query.containedIn("fromId", request.params.userIds);
  query.equalTo("weeventId", request.params.weeventId);

  // Get the first user which matches the above constraints.
  query.find({ 
    success: function(results) {
      for (var i = 0; i < results.length; i++) {
        var result = results[i];
        result.increment("unreadAmount");
      };
      Parse.Object.saveAll(results, {
        success: function(list){
          response.success("add group chat succeeded");
        },
        error: function(error){
          response.error(error);
        }
      });
    }
  });
});

PS: This is the original code, I can give you what I tried if you want.

EDIT: Problem solved.

OxyFlax avatar May 17 '16 14:05 OxyFlax

@OxyFlax It would be great if you shared what you did to make your code above work.

TheRyanHickman avatar Jun 06 '16 01:06 TheRyanHickman

@gfosco What should be the syntax for setting useMasterKey: true in:

var query = new Parse.Query(Parse.Installation); query.containedIn("user", userList);

because for push notifications, we don't call find or fetch

khanamir avatar Jun 14 '16 09:06 khanamir

@gfosco I would also need to know the new syntax about useMasterKey: true if I would like to use it on query.equalTo('user', request.object.get("createdBy"));

So for push...

Thanks much for feedback and best regards, Chris

code-K avatar Jun 17 '16 18:06 code-K

@OxyFlax Can you please post your solution/ fix done?

kumarpranav avatar Sep 27 '16 13:09 kumarpranav

I have the same problem as @khanamir – seemingly no way to just set an option?

I need to rewrite this to pass in { useMasterKey: true } somewhere, somehow;

Parse.Cloud.useMasterKey();
var installationQuery = new Parse.Query(Parse.Installation);
installationQuery.equalTo('user', user);

Parse.Push.send({
    where: installationQuery,
    data: { ... }
},
{
    // Would passing in `useMasterKey: true` here make the query
    // passed into the `where` above use the master key?

    // useMasterKey: true, ?

    success: function() {},
    error: function() {}
});

/cc @OxyFlax

Haraldson avatar Nov 11 '16 10:11 Haraldson

Alongside success and error, as you commented

flovilmart avatar Nov 11 '16 12:11 flovilmart

Same Error I am Getting Here ::: ==>

Parse.Cloud.define("assignPasswordToUser", function(request, response){ var query = new Parse.Query(Parse.User); query.equalTo("email", request.params.email); query.find({ success: function(results){ if (results) { Parse.Cloud.useMasterKey(); var password = request.params.password; console.log(results); results[0].setPassword(password); results[0].save(); response.success(1); }; }, error: function(){ response.error("Error"); } }); });

HemanParbhakar avatar Jan 11 '17 08:01 HemanParbhakar

Stop using Parse.Cloud.useMasterKey() once and for all, use .save(null, {useMasterKey: true})

flovilmart avatar Jan 11 '17 12:01 flovilmart

Parse.Cloud.define("assignPasswordToUser", function(request, response){ var query = new Parse.Query(Parse.User); query.equalTo("email", request.params.email); query.find({ success: function(results){ if (results) {

            var password = request.params.password;
            console.log(results);
            results[0].setPassword(password);
			results[0].save(null, {useMasterKey: true});
            response.success(1);
        };
    },
    error: function(){
        response.error("Error");
    }
});

});

Using This Gives Me An Error

HemanParbhakar avatar Jan 12 '17 04:01 HemanParbhakar

error: Failed running cloud function assignPasswordToUser for user undefined with: Input: {"username":"9988736886","password":"********"} Error: {"code":141,"message":"Object not found."} functionName=assignPasswordToUser, code=141, message=Object not found., username=9988736886, password=hello, user=undefined error: Error generating response. ParseError { code: 141, message: 'Object not found.' } code=141, message=Object not found. [object Object]

I Am Getting This Error When Using this Function

Parse.Cloud.define('assignPasswordToUser', function(request, response) { new Parse.Query(Parse.User).get(request.params.username, { useMasterKey: true, success: function(user) { username.setPassword(request.params.password); username.save(null, { useMasterKey: true, success: response.success, error: function(obj, error) { response.error(error.message); } }); }, error: function(obj, error) { response.error(error.message); } }); });

HemanParbhakar avatar Jan 12 '17 04:01 HemanParbhakar

Parse.Query().get takes an objectId as a parameter not a username

flovilmart avatar Jan 12 '17 13:01 flovilmart

I would just like to add that we were using useMasterKey() method the entire time for a year than out of now where everything started breaking without any code changes, after hours of headbanging I discovered the useMasterKey: true option

What changed for this to break?

If useMasterKey() is not the correct way anymore, we need to have some kind of deprecation logging.

ericraio avatar Feb 12 '17 21:02 ericraio

The depreciation notice has been added on Dec 7th: https://github.com/ParsePlatform/parse-server/blob/master/src/cloud-code/Parse.Cloud.js#L64 and https://github.com/ParsePlatform/parse-server/commit/025e7a321886becfe48778c152b01dd0cb3fd492

This should have been deprecated at an earlier date, sorry for the inconvenience.

flovilmart avatar Feb 12 '17 21:02 flovilmart