1

I can't get my Zend_Navigation to work properly,

When logging in user with AUth/Doctrine, I am pulling out the roles assigned to the user (usually it's a few of them) from a Many-to-many table,

Then in the bootstrap.php on line: $view->navigation($navContainer)->setAcl($this->_acl)->setRole($this->_role);

I get error: '$role must be a string, null, or an instance of Zend_Acl_Role_Interface; array given'

However if I loop through the roles with foreach - the previous roles are being overwritten by the following ones and I get the nav only for last role,

Does anyone have any logical solution for this ?

Really appreciate, Adam

4

2 回答 2

1

我有同样的问题,但从一个稍微不同的角度接近解决方案。我没有修改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,将它传递给您的导航对象,它应该可以工作。

于 2013-03-22T08:17:05.560 回答
0

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

于 2013-08-15T21:32:58.350 回答