I have a cloud code which creates two account roles when a user signs up. Below is the method
Parse.Cloud.afterSave("account", function(request) {
var accountName = request.object.get("name");
//create admin role
var adminRoleACL = new Parse.ACL();
adminRoleACL.setPublicWriteAccess(true);
var adminRole = new Parse.Role(accountName + "_Administrator", adminRoleACL);
adminRole.save() ;
//create user role
var userRoleACL = new Parse.ACL();
userRoleACL.setPublicWriteAccess(true);
var userRole = new Parse.Role(accountName + "_User", userRoleACL);
userRole.save();
});
Now what i wanted to achieve was to add the user which just signed up to these two roles. But unfortunately i saw that in cloud code i can't get the current user. So what i did was to add the users in the role after the roles are created from the client side. Below is the code for the same. The code executes fine and i did not see any error, however i did not see the users being added to the roles in the data browser. Any idea why is this happening? Am i missing something. I would be really thankful for all your help.
user.signUp(null, {
success : function(user) {
var currentUser = Parse.User.current();
var accountName = account.get("name");
var query = new Parse.Query(Parse.Role);
query.contains("name", accountName);
query.find({
success : function(roles) {
if (!roles) {
alert("No roles for " + accountName + " were found");
} else {
for (var i = 0; i < roles.length; i++) {
//add the user for admin role
//TODO: this needs to be done only once for the account owner
if (roles[i].get("name").search(USER_TYPE.ADMIN) >= 0) {
roles[i].getUsers().add(currentUser);
}
//add the user for user role
if (roles[i].get("name").search(USER_TYPE.USER) >= 0) {
roles[i].getUsers().add(currentUser);
}
var saved = roles[i].save();
}
alert("User was added into roles");
}
},
error : function(error) {
alert("Could not add users to the account " + accountName + " error: " + error.message);
}
});
alert("User created successfully");
},
error : function(user, error) {
alert("Error: " + error.code + " " + error.message);
}
});