2
votes

I have a working ACL module and Navigation setup in a Zend Framework 2 project. The navigation object is generated and rendered properly, but it contains an element admin that I only want to be rendered when the logged in user has the role admin. The acl is properly blocking access (i.e. redirects to 403 page) when a non-admin tries to access any pages there, however the navigation still renders the nav-item. The navigation setup is very simple (as it should be based on the read tutorials)

module.config.php

'service_manager' => array(
    'factories' => array(
        'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
    ),
),

view.phtml

<div id="navigation">
    <?php echo $this->navigation('navigation')
                    ->menu()
                    ->setMinDepth(0)
                    ->setMaxDepth(0)
                    ->setAcl($this->acl)
                    ->setRole('guest'); ?>
</div>

After a lot of debugging, I pinpointed where something is going wrong but I have no clue why or how to fix it. I tried var_dumping $this->acl in the view and it is set, as expected. However:

var_dump($this->navigation('navigation')->getAcl());

after the line with setAcl(), returns NULL. So somehow it seems that setAcl($this->acl) is not actually injecting the acl.

2
it's better to inject the ALC when you create the Navigation object. Create your own NavigationFactory and inject the ALC there. You can base it on the DefaultNavigationFactory just fineAndrew
Thanks for the suggestion Andrew, unfortunately it didn't work out. I injected acl and role in custom factory. They now get set properly, I verified this. I went into the Zend files and in Zend/View/Helper/Navigation/AbstractHelper.php the function accept is checking the acl for page and role. Acl and role are set properly but still accept is returning true for all resources for every role. accept calls the function isAllowed() which calls $this->getEventManager()->trigger(__FUNCTION__, $this, $params) but I have no idea what this doesPankrates
I had the same problem and solved it by adding the property "resource" to each element of my navigation. Such property must match the name of a resource defined in your ACL.Dayron Gallardo
Have you tried using the setDefault methods on navigation, @Pankrates? akrabat.com/zend-framework-2/…webDEVILopers

2 Answers

1
votes

I had the same problem and solved it by adding setAcl and setRole before menu:

echo $this->navigation('navigation')->setAcl($this->acl)->setRole($this->role)->menu()->setPartial('partial/navbar.phtml');
0
votes

Somewhere in module bootstrap

\Zend\View\Helper\Navigation::setDefaultAcl($acl);
\Zend\View\Helper\Navigation::setDefaultRole($role);