Parse.com, Adding users to created roles

╄→尐↘猪︶ㄣ 提交于 2019-12-22 18:03:21

问题


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

回答1:


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



回答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;


来源:https://stackoverflow.com/questions/18970416/parse-com-adding-users-to-created-roles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!