yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

RBAC is missing getRolesByRole() #11245

Closed michaelarnauts closed 8 years ago

michaelarnauts commented 8 years ago

Rbac makes it possible to specify a role that has a parent role. There is however no function getRolesByRole($roleName) to get it's children.

This function is almost identical to the function getPermissionsByRole($roleName).

in yiisoft/yii2/rbac/DbManager.php:

    /**
     * @inheritdoc
     */
    public function getRolesByRole($roleName)
    {
        $childrenList = $this->getChildrenList();
        $result = [];
        $this->getChildrenRecursive($roleName, $childrenList, $result);
        if (empty($result)) {
            return [];
        }
        $query = (new Query)->from($this->itemTable)->where([
            'type' => Item::TYPE_ROLE,
            'name' => array_keys($result),
        ]);
        $roles = [];
        foreach ($query->all($this->db) as $row) {
            $roles[$row['name']] = $this->populateItem($row);
        }
        return $roles;
    }

in yiisoft/yii2/rbac/ManagerInterface.php:

    /**
     * Returns all child roles that the specified role represents.
     * @param string $roleName the role name
     * @return Role[] all child roles that the role represents. The array is indexed by the role names.
     */
    public function getRolesByRole($roleName);
samdark commented 8 years ago

I think name should something like getChildRoles().

samdark commented 8 years ago

What do you need it for?

michaelarnauts commented 8 years ago

I have a admin-interface to manage the Users, Roles and Permissions where I would like to be able to get the assigned child Roles of a specified Role. This already exists for Permissions (getPermissionsByRole), but it doesn't for Roles.

samdark commented 8 years ago

OK. Makes sense.

samdark commented 8 years ago

Do you have time for a pull request?

michaelarnauts commented 8 years ago

I've added a PR, but you should check to be sure. I haven't tested the code of PhpManager.