[英]ZF2 / Navigation ACL / accept doesn't work if an asserter is provided
I have a member
role with a resource platform.ticketing
and a privilege access
. 我具有资源
platform.ticketing
和特权access
的member
角色。
Acl is defined such as below : Acl定义如下:
$this->allow('member', 'platform.ticketing', 'access', new HasTicketing());
HasTicketing
is a simple asserter returning true
if member can access or false
if not. HasTicketing
是一个简单的HasTicketing
器,如果成员可以访问则返回true
否则返回false
。
If I remove asserter, everything works fine but the menu appears ... 如果我删除断言,一切正常,但菜单出现...
On my navigation config : 在我的导航配置中:
// ...
[
'label' => 'Ticketing',
'route' => 'platform/ticketing',
'resource' => 'platform.ticketing',
'privilege' => 'access',
],
// ...
On navigation partial : 在导航部分上:
// ...
foreach($this->container->getPages() as $page) {
if(!$page->isVisible() || !$this->navigation()->accept($page)) continue;
// ...
Please, check the Xdebug trace at the returning of accept()
ZF AclListener
class 请在返回
accept()
ZF AclListener
类时检查Xdebug跟踪
I wasn't able to replicate the problem using Zend Framework v2.5.1 and the Skeleton Application: 使用Zend Framework v2.5.1和Skeleton应用程序,我无法复制该问题:
module/Application/config/module.config.php module / Application / config / module.config.php
return array(
// ...
'navigation' => array(
'default' => array(
array(
'label' => 'Home',
'route' => 'home',
),
array(
'label' => 'Page #1',
'route' => 'home',
'resource' => 'mvc:admin',
'privilege' => 'access',
'pages' => array(
array(
'label' => 'Child #1',
'route' => 'home',
),
),
),
array(
'label' => 'Page #2',
'route' => 'home',
),
),
),
'service_manager' => array(
// ...
'factories' => array(
'my-navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
// ...
),
),
// ...
);
module/Application/Module.php module / Application / Module.php
namespace Application;
use Application\Acl\Assertion;
use Zend\View\HelperPluginManager;
use Zend\Permissions\Acl as ZendAcl;
class Module
{
// ...
public function getViewHelperConfig()
{
return array(
'factories' => array(
// This will overwrite the native navigation helper
'navigation' => function(HelperPluginManager $pm) {
$assertion = new Assertion\TestAssertion();
// Setup ACL:
$acl = new ZendAcl\Acl();
$acl->addRole(new ZendAcl\Role\GenericRole('member'));
$acl->addRole(new ZendAcl\Role\GenericRole('admin'));
$acl->addResource(new ZendAcl\Resource\GenericResource('mvc:admin'));
$acl->addResource(new ZendAcl\Resource\GenericResource('mvc:community.account'));
$acl->allow('member', 'mvc:community.account');
$acl->allow('admin', 'mvc:admin', 'access', $assertion);
// $acl->allow('admin', 'mvc:admin', 'access');
// Get an instance of the proxy helper
$navigation = $pm->get('Zend\View\Helper\Navigation');
// Store ACL and role in the proxy helper:
$navigation->setAcl($acl)->setRole('admin');
// Return the new navigation helper instance
return $navigation;
}
)
);
// ...
}
module/Application/src/Application/Acl/Assertion/TestAssertion.php 模块/应用程序/ src /应用程序/Acl/Assertion/TestAssertion.php
namespace Application\Acl\Assertion;
use Zend\Permissions\Acl as ZendAcl;
class TestAssertion implements ZendAcl\Assertion\AssertionInterface
{
public function assert(ZendAcl\Acl $acl,
ZendAcl\Role\RoleInterface $role = null,
ZendAcl\Resource\ResourceInterface $resource = null,
$privilege = null)
{
return true;
}
}
module/Application/view/application/index/index.phtml 模块/应用程序/视图/应用程序/索引/index.phtml
<?php $this->navigation('my-navigation')->menu()->setPartial('application/partials/menu') ?>
<?php echo $this->navigation('my-navigation')->menu(); ?>
module/Application/view/application/partials/menu.phtml 模块/应用程序/视图/应用程序/部分/menu.phtml
<?php
foreach($this->container->getPages() as $page) {
if (!$page->isVisible() || !$this->navigation()->accept($page)) {
continue;
}
echo $this->navigation()->menu()->htmlify($page) . '<br />';
}
With Application\\Acl\\Assertion\\TestAssertion::assert()
returning true , "Page #1" menu link is displayed. 在
Application\\Acl\\Assertion\\TestAssertion::assert()
返回true的情况下 ,将显示“页面#1”菜单链接。
With Application\\Acl\\Assertion\\TestAssertion::assert()
returning false , "Page #1" menu link is not displayed. 在
Application\\Acl\\Assertion\\TestAssertion::assert()
返回false的情况下 ,不显示“页面#1”菜单链接。
If you remove the $assertion
in Application\\Module::getViewHelperConfig()
, $acl->allow('admin', 'mvc:admin', 'access');
如果在
Application\\Module::getViewHelperConfig()
删除$assertion
,则$acl->allow('admin', 'mvc:admin', 'access');
, the link will render again. ,链接将再次呈现。 This is because the role (admin) will have access to the resource (mvc:admin) and privilege (access).
这是因为角色(admin)有权访问资源(mvc:admin)和特权(access)。
The above leads me to believe that you are either using a dated version of Zend Framework or Application\\Acl\\Asserter\\HasTracking::assert()
isn't returning the boolean you are expecting. 上面的内容使我相信您使用的是Zend Framework的过时版本,或者
Application\\Acl\\Asserter\\HasTracking::assert()
未返回您期望的布尔值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.