Integrating BjyAuthorize with Zend\Navigation

If you are using BjyAuthorize for ACL configuration and want to use Zend\Navigation's Zend\Acl integration features, then you need to set the Acl and Role information into Zend\Navigation.

The easiest way to do this is to add the following to Application\Module::onBoostrap():

        $sm = $e->getApplication()->getServiceManager();
 
        // Add ACL information to the Navigation view helper
        $authorize = $sm->get('BjyAuthorize\Service\Authorize');
        $acl = $authorize->getAcl();
        $role = $authorize->getIdentity();
        \Zend\View\Helper\Navigation::setDefaultAcl($acl);
        \Zend\View\Helper\Navigation::setDefaultRole($role);

This assumes that you've set up BjyAuthorize with some resources and rules. For example, in my config/autoload/bjyauthorize.global.php, I have a 'bug' resource and have a rule that allows the reporter role access to the list and add privileges:

        'resource_providers' => array(
            'BjyAuthorize\Provider\Resource\Config' => array(
                'bug' => array(),
            ),
        ),
 
        'rule_providers' => array(
            'BjyAuthorize\Provider\Rule\Config' => array(
                'allow' => array(
                    array(array('reporter'), 'bug', array('list', 'add')),
                ),
            ),
        ),

My Zend\Navigation configuration for the bug menu item is in my Bug module's module.config.php and it looks like:

    'navigation' => array(
        'site' => array(
            'bug' => array(
                'label' => 'Bugs',
                'route' => 'bug',
                'resource' => 'bug',
                'privilege' => 'list',
                'pages' => array(
                    'create' => array(
                        'label' => 'Create new project',
                        'route' => 'bug/create',
                        'resource' => 'bug',
                        'privilege' => 'add',
                    ),                        
                ),
            ),
        ),        
    ),

That's all there is to it.

2 Responses to “Integrating BjyAuthorize with Zend\Navigation”

  1. 1 Maw

    Hi,I have a problem with integration BjyAuthorize and Zend navigation and don't know how to resolve them. I try this manual and everything works fine. But I expected when I define guards in bjyauthorize.config and after that, there will be reflection between my navigation and guard configuration (denied controllers or routes does not display navigation items). My problem is that navigation items are still displayed, but sections are correctly protected. Is there any way, how to reflect guards into navigation?
    My view helper config in Module.php

    'mainMenu' => function($sm){
    $nav = $sm->get('navigation')->menu();
    $serviceLocator = $sm->getServiceLocator();
    $acl = $serviceLocator->get('BjyAuthorize\Service\Authorize')->getAcl();
    $role = $serviceLocator->get('BjyAuthorize\Service\Authorize')->getIdentity();
    $nav->setAcl($acl);
    $nav->setRole($role); // Todo replace
    $nav->setUseAcl();
    return $nav->setUlClass('nav')->setTranslatorTextDomain(__NAMESPACE__);
    }

  2. 2 Dmitry Prudnikov

    https://github.com/zendframework/zf2/blob/master/library/Zend/Authentication/AuthenticationService.php is working a little bit strange in my opinion - getstorage creates new session container - so basically if you use byjauthorize - any acl will call session_start, that seems strange

    if we have a guest user (like a SE robot) - why should we create a session to check DefaultRole (guest) is allowed to access IndexController

    weird

The views expressed in these comments are not the views of the publisher. However, we believe in the rights of others to express their legitimate views and concerns. Any legitimate complaint emailed to rob@akrabat.com will be seriously considered and the post reviewed as desirable and necessary.