Search code examples
androidparse-platformaclparse-cloud-code

Android- Parse Database edit other users' information


I'm working on a school project that is a transportation application on android. I'm trying to edit user information from another user as logged in. When I try to edit another user's saved variable, I get

java.lang.IllegalArgumentException: Cannot save a ParseUser that is not authenticated.

On my search, I saw that some people recommend changing ACL to public write on the users to edit them, I tried that and unfortunately, It didn't change anything, I still have this error. Another advice was to use cloud code or master key but I couldn't find any documentation that shows how to implement them. I'd be glad if anyone helps me. Thank you very much.


Solution

  • you can use masterKey in cloud code like this:

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

    Here is a complete example using cloud code and master key:

    Parse.Cloud.define("saveOtherUser", async (request) => {
    
      const otherUserID = request.params.otherUserID;//other user's ID;
    
      const user = request.user; //This is you. We are NOT gonna update this.
      //you can check your security with using this user. For example:
    
      if(!user.get("admin")){
    
        //we are checking if th requesting user has admin privaleges.
        //Otherwise everyone who call this cloud code can change other users information.
    
        throw "this operation requires admin privilages"
    
        //and our cloud code terminates here. Below codes never run
        //so other users information stays safe.
      }
    
      //We create other user
      const otherUser = new Parse.User({id:otherUserID});
    
      //Change variables
      otherUser.set("variable","New Variable, New Value");
    
      //Now we are going to save user
      await otherUser.save(null,{useMasterKey:true});
    
      //this is the response our android app will recieve
      return true;
    
    
    });
    

    This is our Java code for android app:

    HashMap<String, Object> params = new HashMap<>();
    params.put("otherUserID", otherUser.getObjectId());
    
    ParseCloud.callFunctionInBackground("saveOtherUser", params, new FunctionCallback<Boolean>() {
        @Override
        public void done(Boolean object, ParseException e) {
            if(e==null&&object){
                //save operation successful
            }
            else{
                //save operation failed
            }
        }
    });
    

    Aklına takılan olursa sor :)