在Zend框架PHP中使用Zend_ACL在Zend_Navigation中分配多个角色


Assign multiple roles in Zend_Navigation using Zend_ACL in Zend Framework PHP?

我不能让我的Zend_Navigation正常工作,

当使用AUth/Doctrine登录用户时,我从多对多表中取出分配给用户的角色(通常是其中的几个角色),

然后在bootstrap.php上行:(视图->导航美元navContainer) -> setAcl ($ this -> _acl) -> setRole ($ this -> _role);

我得到错误:$role必须是字符串、null或Zend_Acl_Role_Interface的一个实例;数组给'

但是,如果我使用foreach循环遍历角色-前面的角色将被下面的角色覆盖,并且我只获得最后一个角色的导航,

谁有什么合理的解决方案?

真的很感激,亚当

我遇到了同样的问题,但从一个稍微不同的角度来解决这个问题。我没有修改Zend_Navigation对象来接受两个或更多角色,而是扩展了Zend_Acl并修改了isAllowed()方法来检查所有这些角色。Zend_Navigation对象使用isAllowed()方法,所以重写它解决了这个问题。

My_Acl.php

<pre><code>
class My_Acl extends Zend_Acl
{
    public function isAllowed($role = null, $resource = null, $privilege = null)
    {
        // Get all the roles to check against
        $userRoles = Zend_Registry::get('aclUserRoles');
        $isAllowed = false;
        // Loop through them one by one and check if they're allowed
        foreach ($userRoles as $role)
        {
            // Using the actual ACL isAllowed method here
            if (parent::isAllowed($role->code, $resource))
            {
                $isAllowed = true;
            }
        }
        return $isAllowed;
    }
}
</code></pre>

然后,而不是创建Zend_Acl的实例,使用My_Acl,传递给你的导航对象,它应该工作

你真的不应该重写isAllowed(),是的,有一个解决方案。创建一个实现Zend_Acl_Role_Interface的类,如果内存不够,它需要定义一个方法getRole(),实际上,这可以是您用来验证用户的模型,并允许该类处理确定角色。一个用户应该只有一个角色。如果资源的访问权限应该授予多个角色的用户,但只在某些条件下,那么您应该使用断言,这就是它们存在的原因。