在 Zend Framework PHP 中使用 Zend_ACL 在 Zend_Navigation 中分配多个角色?

发布于 2024-12-03 02:13:03 字数 394 浏览 2 评论 0原文

我无法让我的 Zend_Navigation 正常工作,

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

然后在bootstrap.php上线: $view->navigation($navContainer)->setAcl($this->_acl)->setRole($this->_role);

我收到错误: '$role 必须是字符串、null 或 Zend_Acl_Role_Interface 的实例;但是,

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

有人对此有任何逻辑解决方案吗?

真的很感激, 亚当

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

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

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

发布评论

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

评论(2

缱倦旧时光 2024-12-10 02:13:04

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

I had the same problem but approached the solution from a slightly different angle. Instead of modifying the Zend_Navigation object to accept two or more roles, I extended Zend_Acl and modified the isAllowed() method to check against all those roles. The Zend_Navigation objects use the isAllowed() method, so overriding this solved the issue.

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>

Then, instead of creating an instance of Zend_Acl, use My_Acl, pass that to your navigation object and it should work.

征﹌骨岁月お 2024-12-10 02:13:04

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

You should really never, ever override isAllowed(), and yes there is a solution. Create a class that implements Zend_Acl_Role_Interface and if memory serves it requires defining a single method getRole(), this could, in fact, be your model that you use to authenticate a user against and allow that class to handle determining the role. A user should only have a single role. If access to the resource should be granted to users of multiple roles but only under certain conditions, then you should use an assertion, thats why they are there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文