Parse.com,将用户添加到创建的角色

时间:2013-09-23 23:13:56

标签: parse-platform role user-roles

我有一个云代码,可在用户注册时创建两个帐户角色。以下是方法

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

现在我想要实现的是添加刚刚注册到这两个角色的用户。但不幸的是,我在云代码中看到我无法获得当前用户。 所以我所做的是在从客户端创建角色后添加角色中的用户。下面是相同的代码。代码执行正常,我没有看到任何错误,但我没有看到用户被添加到数据浏览器中的角色。知道为什么会这样吗?我错过了什么。我非常感谢你的帮助。

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 个答案:

答案 0 :(得分:4)

感谢您的帮助。我通过使用像这样的云代码完成它

  1. 创建帐户时创建角色。

    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. 然后将用户添加到创建的角色

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

答案 1 :(得分:2)

实际上您可以访问正在执行请求的用户,请查看Cloud Code Documentation以获取更多详细信息。

特别是,你想看的是request.user

var currentUser = request.user;