Kevin
Kevin

Reputation: 187

How do I associate an existing User with a newly created Role using the parse.com data browser?

I have created a Role in parse.com data browser. I also have some User objects created. I'm unable to associate the users I have already created with the role.

I can only see a way to associate users to roles by clicking on the "View Relations" button for the Role object and then creating a new user. But, I don't want a new user. I want to associate an existing user with the role.

How do I associate an existing User with a newly created Role using the parse.com data browser?

Upvotes: 2

Views: 1167

Answers (2)

Sandra
Sandra

Reputation: 388

It's not currently possible to add objects to a relation in the data browser UI (which includes adding Users to Roles)

See https://www.parse.com/questions/how-can-i-add-objects-to-a-relation

Depending on your settings (e.g. if you've set permissions using CLP and/or ACLs), you can also use the REST API key with cURL or use your app (either in the app itself or cloud code) - see also https://parse.com/docs/rest/guide#roles-updating-roles

replace relevant tags below (e.g. admin and USER_ID) with appropriate names, IDs and keys

// Find admin role
var Role = Parse.Object.extend("_Role"); 
var query = new Parse.Query(Role); 
query.equalTo("name", "admin"); 

query.find({ success: function(results) { 
    var roleObject = results[0];

    // Add users here

    // Specify users to add
    var usersToAddToRole = ["USER_ID"];
    for (var i = 0; i < usersToAddToRole.length; i++) {
        roleObject.getUsers().add(usersToAddToRole[i]);
    }

    // Or add current user only 
    var user = Parse.User.current();
    var relation = roleObject.relation("users");
    relation.add(user);

    roleObject.save();
} });

The link to the Parse question above also suggests you can use your REST API key instead of the Master key to issue a cURL request (do not use the Master key in your client app - only on the server or in manual cURL requests!)

curl -X PUT \
-H "X-Parse-Application-Id: APP_ID" \
-H "X-Parse-REST-API-Key: REST_API_KEY" \
-H "Content-Type: application/json" \
-d '{"users":{"__op":"AddRelation","objects":[{"__type":"Pointer","className":"_User","objectId":"USER_ID"}]}}' \
https://api.parse.com/1/roles/ROLE_ID

The main documentation on managing Roles using the REST API is here https://parse.com/docs/rest/guide#roles
For more details on security, check http://blog.parse.com/learn/engineering/parse-security-i-are-you-the-key-master/

Upvotes: 0

Thijs
Thijs

Reputation: 2351

It is my understanding this is not possible through the website. The quickest way is to use the cUrl example and modify it for your own use. When you're logged in to Parse.com and you go to the documentation for the REST API call to update a role you can copy an example with your own API keys. All that is left to do is change the user ID and the role ID. It will leave you with something like this:

curl -X PUT \
  -H "X-Parse-Application-Id: <APP-ID>" \
  -H "X-Parse-Master-Key: <MASTER-KEY>" \
  -H "Content-Type: application/json" \
  -d '{
        "users": {
          "__op": "AddRelation",
          "objects": [
            {
              "__type": "Pointer",
              "className": "_User",
              "objectId": "<USER-ID>"
            }
          ]
        }
      }' \
  https://api.parse.com/1/roles/<ROLE-ID>

Upvotes: 3

Related Questions