Zend_Acl 查找所有继承的角色

发布于 2024-10-24 06:11:15 字数 389 浏览 1 评论 0原文

我得到了角色链:

acl.roles.guest  = null
acl.roles.member = "guest"
acl.roles.admin  = "member"
acl.roles.owner  = "admin"

在 .....Controller/Action/Helper/Acl.php 中我存储了 _acl 对象

这是获取我的角色和父母列表的方法吗?

$this->_acl->getParents ( 'admin' )

应该

admin, member, guest

作为数组或字符串返回(例如逗号分隔)

I got chain of roles:

acl.roles.guest  = null
acl.roles.member = "guest"
acl.roles.admin  = "member"
acl.roles.owner  = "admin"

in .....Controller/Action/Helper/Acl.php I have stored _acl object

Is it a way to get list of my role and parents ?

$this->_acl->getParents ( 'admin' )

should return

admin, member, guest

as array, or string (eg comma delimited)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎盛开的樱花 2024-10-31 06:11:15

您可以使用

  • getRoles() - 返回已注册角色的数组。

检查每个角色

  • ,然后使用inheritsRole() - 返回来 true 当且仅当 $role 继承自 $inherit

请参阅 API 文档 Zend_Acl,位于 http://framework.zend.com/apidoc/core/Zend_Acl/Zend_Acl.html

示例,

$parents = array();
foreach ($acl->getRoles() as $inherit) {
    if ($acl->inheritsRole('owner', $inherit)) {
        $parents[] = $inherit;
    }
}

该示例将返回类似

Array
(
    [0] => guest
    [1] => member
    [2] => admin
)

替代方案的

内容实际上 Zend_Acl_Role_Registry 中的 getParents() 方法,但似乎没有办法通过 Zend_Acl 的公共接口访问该方法。请参阅源代码

扩展 Zend_Acl 以获得方法 getParentsForRole() 是很简单的 不过:

class My_Acl extends Zend_Acl
{
    public function getParentsForRole($role)
    {
        return $this->_getRoleRegistry()->getParents($role);
    }
}

这只会返回直接父级,例如,对于“owner”,它返回“admin”,因此您可能需要添加另一种方法来递归地获取所有父级到最后一个:

class My_Acl extends Zend_Acl
{
    public function getAllParentsForRole($role, $parents = array())
    {
        foreach ($this->getParentsForRole($role) as $parentName => $parentRole) {
            if (FALSE === isset($parents[$parentName])) {
                $parents[$parentName] = $parentRole;
                $parents = $this->getAllParentsForRole($parentRole, $parents);
            }
        }
        return $parents;
    }

    public function getParentsForRole($role)
    {
        return $this->_getRoleRegistry()->getParents($role);
    }
}

示例

$acl = new My_Acl;
$acl->addRole('guest');
$acl->addRole('other');
$acl->addRole('member', 'guest');
$acl->addRole('admin', 'member');
$acl->addRole('owner', array('admin', 'other'));

print_r($acl->getAllParentsForRole('owner'));

将导致

Array
(
    [admin] => Zend_Acl_Role Object
        (
            [_roleId:protected] => admin
        )

    [member] => Zend_Acl_Role Object
        (
            [_roleId:protected] => member
        )

    [guest] => Zend_Acl_Role Object
        (
            [_roleId:protected] => guest
        )

    [other] => Zend_Acl_Role Object
        (
            [_roleId:protected] => other
        )    
)

这与对 getParents 的单个调用将返回的结果一致。如果您不需要,您也可以使用第一个示例中的 foreach 代码,例如

class My_Acl extends Zend_Acl
{
    public function getAllParentsForRole($role)
    {
        $parents = array();
        foreach ($this->getRoles() as $inherit) {
            if($this->inheritsRole($role, $inherit)) {
                $parents[] = $inherit;
            }
        }
        return $parents;
    }
}

You can use

  • getRoles() - Returns an array of registered roles.

and then check each of these with

  • inheritsRole() - Returns true if and only if $role inherits from $inherit

See the API docs Zend_Acl at http://framework.zend.com/apidoc/core/Zend_Acl/Zend_Acl.html

Example

$parents = array();
foreach ($acl->getRoles() as $inherit) {
    if ($acl->inheritsRole('owner', $inherit)) {
        $parents[] = $inherit;
    }
}

which would then return something like

Array
(
    [0] => guest
    [1] => member
    [2] => admin
)

Alternative

There actually is a getParents() method in Zend_Acl_Role_Registry, but there doesn't seem to be a way to access that through the public interface of Zend_Acl. See the sourcecode

It is trivial to extend Zend_Acl to have a method getParentsForRole() though:

class My_Acl extends Zend_Acl
{
    public function getParentsForRole($role)
    {
        return $this->_getRoleRegistry()->getParents($role);
    }
}

This does only return the immediate parents though, e.g. for 'owner' it returns 'admin', so you might want to add another method to fetch all the parents recursively down to the last one:

class My_Acl extends Zend_Acl
{
    public function getAllParentsForRole($role, $parents = array())
    {
        foreach ($this->getParentsForRole($role) as $parentName => $parentRole) {
            if (FALSE === isset($parents[$parentName])) {
                $parents[$parentName] = $parentRole;
                $parents = $this->getAllParentsForRole($parentRole, $parents);
            }
        }
        return $parents;
    }

    public function getParentsForRole($role)
    {
        return $this->_getRoleRegistry()->getParents($role);
    }
}

Example

$acl = new My_Acl;
$acl->addRole('guest');
$acl->addRole('other');
$acl->addRole('member', 'guest');
$acl->addRole('admin', 'member');
$acl->addRole('owner', array('admin', 'other'));

print_r($acl->getAllParentsForRole('owner'));

would result in

Array
(
    [admin] => Zend_Acl_Role Object
        (
            [_roleId:protected] => admin
        )

    [member] => Zend_Acl_Role Object
        (
            [_roleId:protected] => member
        )

    [guest] => Zend_Acl_Role Object
        (
            [_roleId:protected] => guest
        )

    [other] => Zend_Acl_Role Object
        (
            [_roleId:protected] => other
        )    
)

which is in accordance with what a single call to getParents would return. If you dont need that, you can also just use the foreach code from the first example, e.g.

class My_Acl extends Zend_Acl
{
    public function getAllParentsForRole($role)
    {
        $parents = array();
        foreach ($this->getRoles() as $inherit) {
            if($this->inheritsRole($role, $inherit)) {
                $parents[] = $inherit;
            }
        }
        return $parents;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文