如何获取acl角色对应的所有用户?

How to get all the users corresponding to the acl role?

我需要获取该角色的所有用户。我已经知道这方面的代码,但我不知道如何从角色中获取用户。

我只知道我需要在角色上使用getUsers()功能,但我遇到了问题。

我的代码:

self.getCompanyUsers = function getCompanyUsers() {
    $rootScope.displayLoading = true;
    var userQuery = new Parse.Query(Parse.Role);
    userQuery.contains('name', $state.params.id);

    userQuery.find().then(function(roles) {
        $scope.users = roles;
        $rootScope.displayLoading = false;      
    }).then(function() { 
        console.log(roles.getUsers());

    })
};

您提供的代码似乎存在一些问题,需要先修复。

第一;你的承诺流程似乎有点偏离。您正试图在不 return 任何东西的情况下继续它。我不确定为什么您需要在承诺链中执行此操作,因为您不必等待任何事情完成。所以你应该删除第二个 then,除非你从你发布的代码中遗漏了一些东西。

其次;您正在尝试对 Parse.Role 的数组调用 getUsers()。有几种方法可以解决这个问题,具体取决于此功能的最终用途:

  • 您可以将 userQuery.find() 更改为 userQuery.first(),这只会 return 一个对象并且 roles.getUsers() 有效。
  • 或者您可以遍历从现有查询中获得的结果,并对每个结果调用 getUsers()。我建议这不是您想要做的,因为它可能会导致对 User 对象进行大量查询,如果这是您想要的,可能会有更好的选择。

第三; getUsers() 将简单地 return Parse.Relation 而不是角色中的用户。要获取用户,您必须先获取查询对象。因此:role.getUsers().query();

这是一个普通的 Parse.Query,您可以这样使用它。例如,role.getUsers().query().find()

如果我的想法是正确的,你会想要类似这样的东西:

    self.getCompanyUsers = function getCompanyUsers() {
        $rootScope.displayLoading = true;
        var userQuery = new Parse.Query(Parse.Role);
        userQuery.contains('name', $state.params.id);
        userQuery.first().then(function(role) {
            $scope.users = role;


            if(!role)
            {
                //check a role has been found
                return Parse.Promise.error("No role found")
            }

            //role.getUsers() will be the Parse.Relation
            //role.getUsers().query() will be a normal Parse.Query
            return role.getUsers().query().find();

        }).then(function(users)
            {
                //users will be an array of the users in the role, depending on ACL/CLP.
                console.log(users);
                $rootScope.displayLoading = false;
            }
        )
    };