Georges Badra
Georges Badra

Reputation: 65

how to delete a parse user from an android application?

I'm making an android app that have 2 UserTypes an administrator and regular users the administrator have the ability to control the Parse database edit, add and delete but i have come to a problem concerning the delete user here is my code but it is not working

final String userId = user.getUserId();
         ParseQuery<ParseUser> query = ParseUser.getQuery();
         query.whereEqualTo("objectid", userId);

         query.getFirstInBackground(new GetCallback<ParseUser>() {



            public void done(ParseUser object, ParseException e) {
                 // TODO Auto-generated method stub
                 try {
                    object.delete();
                    Toast.makeText(getApplicationContext(),
                            "Successfully deleted Project"+userId, Toast.LENGTH_LONG).show();
                    //prog.dismiss();

                } catch (ParseException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }

        });

any help will be appreciated thx

Upvotes: 2

Views: 2704

Answers (3)

Gautam
Gautam

Reputation: 1343

 ParseUser user = ParseUser.getCurrentUser();
        user.deleteInBackground(new DeleteCallback() {
            @Override
            public void done(com.parse.ParseException e) {
                if (e == null) {
                   //user deleted
                } else {
                    ccd.showDialog(mContext, e.getMessage());
                    e.printStackTrace();
                }
            }
        });

Upvotes: 2

A. Vin
A. Vin

Reputation: 885

The master key can be accessed via the Parse Rest API. Just write a Parse Cloud Code function to delete a user using the master key then call that function from the admin account using the Parse Android API.

Upvotes: 0

danh
danh

Reputation: 62676

A user cannot delete another user from the client. It can be done in cloud code with Parse.Cloud.useMasterKey().

(As an aside, it looks like the posted code wasn't even fetching the user by id. It would stand to reason that whereEqualTo("objectId", someObjectId) would find an object, but parse provides a special form of query for this called getInBackground(). See the doc for Parse.Query.

EDIT - In cloud code, the function would go something like this:

Parse.Cloud.define("deleteUserWithId", function(request, response) {
    Parse.Cloud.useMasterKey();
    var userId = request.params.userId;
    var query = new Parse.Query(Parse.User);
    query.get(userId).then(function(user) {
        return user.destroy();
    }).then(function() {
        response.success(user);
    }, function(error) {
        response.error(error);
    });
});

From the client (though I'm a java dilettante):

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

Upvotes: 6

Related Questions