在 Zend Framework PHP 中使用 Zend_ACL 在 Zend_Navigation 中分配多个角色?
我无法让我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,但从稍微不同的角度解决了这个问题。我没有修改
Zend_Navigation
对象来接受两个或多个角色,而是扩展了Zend_Acl
并修改了isAllowed()
方法来检查所有这些角色。Zend_Navigation
对象使用isAllowed()
方法,因此覆盖它可以解决问题。My_Acl.php
然后,不要创建
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 extendedZend_Acl
and modified theisAllowed()
method to check against all those roles. TheZend_Navigation
objects use theisAllowed()
method, so overriding this solved the issue.My_Acl.php
Then, instead of creating an instance of
Zend_Acl
, useMy_Acl
, pass that to your navigation object and it should work.你真的不应该重写 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.