Search code examples
javaandroidparse-platform

Can't edit other Parse User objects , save In Background doesn't write in Parse Dashboard


I have been bonking my head everywhere on this problem , I would really need some help please !! I am pretty new to Android.

My problem is the following , I have completed the User Class with some columns , for example "Former Friends" which are a list of Strings . I do a first query , then I find the Parseuser objects matching the query (which are not the logged in user) and then I try to fill those columns.

I also update the info for the logged in user

It properly works for the logged in user ,however I can't see the filled info for the other Parse object user

I tried modifying the write access for the first user (objects.get(0)) ,but it doesn't work

ParseQuery<ParseUser> query = ParseUser.getQuery();


 query.whereNotEqualTo("username", getCurrentUser().getUsername());

query.whereNotContainedIn("username",getCurrentUser().getList("Former_friends"));



 query.findInBackground(new FindCallback<ParseUser>() {
     @Override
  public void done(List<ParseUser> objects, ParseException e) {



  if (e == null) {

  if (objects.size() > 0) {


// Here I just add the first object to a list and I update the current user data ,that works fine


List<String> aList = ParseUser.getCurrentUser().getList("Former_friends");
                                                aList.add(objects.get(0).getString("username"));


                                                ParseUser.getCurrentUser().put("Former_friends", aList);


                                                ParseUser.getCurrentUser().saveInBackground();


 ParseUser userfound =objects.get(0);

// The two following Lines doesn't work. I don't see "Any String" in the ParseDashboard "Name" columns..                                                

userfound.put("Name","Any String");
userfound.saveInBackground();

There are no bugs , but no update for the non-logged-in user

Big thx,

Serge


Solution

  • For security reasons, in Parse Server, one user cannot change another user object directly from client side. If it were possible, a bad user could erase all other users data, for example.

    So this operation requires you to write a cloud code function. You should have a cloud function similar to this one here:

    Parse.cloud.define('formerFriends', async request => {
      const query = new Parse.Query(Parse.User);
      query.notEqualTo('username', request.user.getUsername());
      query.notContainedIn('username', request.user.get('Former_friends'));
      const objects = await query.find({ useMasterKey: true });
      if (object.length > 0) {
        const aList = request.user.get('Former_friends');                                                
        aList.add(objects[0].getUsername());
        request.user.set('Former_friends', aList);
        await request.user.save();
        const userfound = objects[0];
        userfound.set('Name', 'Any String');
        await userfound.save(null, { useMasterKey: true });
      }
    });
    

    And then call the cloud function from Android like this:

    HashMap<String, Object> params = new HashMap<String, Object>();
    ParseCloud.callFunctionInBackground("formerFriends", params, new FunctionCallback<Float>() {
      void done(Object result, ParseException e) {
        if (e == null) {
          // success
        }
      }
    });