Reputation: 310
I have seen that this can be done using cloud code. But I am not sure how to create a function in cloudcode and executing/Invoking it with parameters from Javascript. Is this possible?
Upvotes: 0
Views: 340
Reputation: 62686
It is possible. To make a user and admin, one must add that user to the admin role's users. Here's a cloud function to do that...
Parse.Cloud.define("setUserAdmin", function(request, response) {
var user;
var userQuery = new Parse.Query(Parse.User);
return userQuery.get(request.userId).then(function(result) {
user = result;
var roleQuery = new Parse.Query(Parse.Role);
roleQuery.equalTo("name", "admin");
// here's our defense against mischief: find the admin role
// only if the requesting user is an admin
roleQuery.equalTo("users", request.user);
return roleQuery.first();
}).then(function(role) {
if (!role) {
return Parse.Promise.error("only admins can add admins");
}
Parse.Cloud.useMasterKey();
var relation = role.relation("users");
relation.add(user);
return role.save();
}).then(function(result) {
response.success(result);
}, function(error) {
response.error(error);
});
});
Calling that from the JS sdk...
var params = { userId: someUserId };
return Parse.Cloud.run("setUserAdmin", params).then(function(result) {
// handle success
}, function(error) {
// handle error
});
Upvotes: 1