2

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);
    }
});

2 Answers 2

5

thanks for the help. I go it done by using a cloud code like this

  1. Create the roles while creating the account.

    Parse.Cloud.afterSave("account", function(request) {
    
        var accountName = request.object.get("name");
        //create admin role
        var adminRoleACL = new Parse.ACL();
        adminRoleACL.setPublicReadAccess(false);
        adminRoleACL.setPublicWriteAccess(false);
        var adminRole = new Parse.Role(accountName + "_Administrator", adminRoleACL);
        adminRole.save();
    
        //create user role
        var userRoleACL = new Parse.ACL();
        userRoleACL.setPublicReadAccess(false);
        userRoleACL.setPublicWriteAccess(false);
        var userRole = new Parse.Role(accountName + "_User", userRoleACL);
        userRole.save();
    });
    
  2. Then add the users to the created role

    Parse.Cloud.define("addUsersToRole", function(request, response) {
    
        Parse.Cloud.useMasterKey();
        var currentUser = request.user;
        var accountName = request.params.accountname;
        var isAdmin = request.params.admin;
    
        var query = new Parse.Query(Parse.Role);
        query.contains("name", accountName);
        query.find({
            success : function(roles) {
                console.log("roles: " + roles.length);
                for (var i = 0; i < roles.length; i++) {
    
                    if ( isAdmin = false && roles[i].get("name").search("_Administrator") >= 0)
                        continue;
    
                    roles[i].getUsers().add(currentUser);
                    roles[i].save();
                }
                response.success();
            },
            error : function(error) {
                response.error("error adding to admin role " + error);
            }
        });
    });
    
Sign up to request clarification or add additional context in comments.

Comments

2

Actually you can access the user that is performing the request, check the Cloud Code Documentation for more details.

In particular, what you want is to look at request.user:

var currentUser = request.user;

2 Comments

Thanks for the reply. The documentation for request.user says this user - The Parse.User that is making the request. This will not be set if there was no logged-in user. I am trying to do add the users when we do signup, so the user is not logged in and hence is null. So i think this can only be done by calling another cloud function which does this after logging in
Who is saving the "account" object? The user saving that will be exposed as the request.user in Cloud Code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.