mattste
mattste

Reputation: 380

Parse 400 Bad Request setACL

I have an "Administrator" Role that I'd like to assign to all of the User objects currently in my Parse. However, no matter what I try I get a 400 error. When I try to manually add in the ACL via the dashboard, it doesn't save. For example, I'll put in "{ "role:Administrator":{"read":true, "write":true}}" and it will revert to (undefined) on refresh.

Edit: so I've tried creating a Cloud Code function. Still receiving a 400 error alongside "Result: Error: undefined unauthorized" when attempting to run the function.

main.js (Cloud Code):

Parse.initialize("*****", "*****");
Parse.Cloud.define("modifyUser", function(request, response) {
    if (!request.user) {
        response.error("Must be signed in to call this Cloud Function.")
    return;
    }

    // The rest of the function operates on the assumption that request.user is *authorized*
    Parse.Cloud.useMasterKey();

    var query = new Parse.Query(Parse.User);
    query.find({
        success: function(results) {
        response.success("Successfully retrieved " + results.length + " scores.");
        // Do something with the returned Parse.Object values
        for (var i = 0; i < results.length; i++) { 
            var object = results[i];
            alert(object.id + ' - ' + object.get('objectId'));
        }
        },
        error: function(error) {
            response.error("Error: " + error.code + " " + error.message);
        }
    });

});

My client-side .js file: $(function() { console.log("running dashboard"); Parse.initialize("**", "**");

    Parse.Cloud.run('modifyUser', {username: "[email protected]"}, {
        success: function(result) {
        // result is 'Hello world!'
        console.log(result);
         },
        error: function(error) {
            console.log("error: " + error.code + " " + error.message);
        }
    });
});

Before update: Ideally, I'd like to loop through all of the users in my Javascript file and update their role using the following code:

// set up ACL for User object
var userACL = new Parse.ACL();
userACL.setRoleWriteAccess("Administrator", true);
userACL.setRoleReadAccess("Administrator", true);

// grab all Users
var user_query = new Parse.Query(Parse.User);
user_query.find( {
    success: function(users) {
        // querying all users works successfully
        console.log(users);
        // assign Administrator ACL to each user
        for (var i=0; i<users.length; i++) {
            users[i].setACL(userACL);
            users[i].save(null, {
                success: function(user) {
                    console.log("save successfully");
                },
                error: function(error) {
                    console.log("error saving: " + error.code + " " + error.message);
                }
            });
        }
    },
    error: function(error) {
        console.log("error: " + error.code + " " + error.message);
    }
});

However, this returns the following: POST https://api.parse.com/1/classes/_User/userIDhere 400 (Bad Request) for each user I attempt to save alongside "user objects cannot allow writes from other users", although my user is listed under the Administrator Role.

Any help would be greatly appreciated.

Upvotes: 2

Views: 1380

Answers (1)

mattste
mattste

Reputation: 380

Alright, I've solved my problem. You shouldn't use Parse.initialize in your Cloud Code function, and you certainly can't use it alongside useMasterKey. Here's a link where I stumbled across this: https://www.parse.com/questions/usemasterkey-and-modifying-user-results-in-unauthorized

This should really be in the Cloud Code documentation.

Upvotes: 3

Related Questions