Pragmatism in the real world

Integrating BjyAuthorize with ZendNavigation

If you are using BjyAuthorize for ACL configuration and want to use ZendNavigation‘s ZendAcl integration features, then you need to set the Acl and Role information into ZendNavigation.

The easiest way to do this is to add the following to ApplicationModule::onBoostrap():

        $sm = $e->getApplication()->getServiceManager();

        // Add ACL information to the Navigation view helper
        $authorize = $sm->get('BjyAuthorizeServiceAuthorize');
        $acl = $authorize->getAcl();
        $role = $authorize->getIdentity();
        ZendViewHelperNavigation::setDefaultAcl($acl);
        ZendViewHelperNavigation::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(
            'BjyAuthorizeProviderResourceConfig' => array(
                'bug' => array(),
            ),
        ),

        'rule_providers' => array(
            'BjyAuthorizeProviderRuleConfig' => array(
                'allow' => array(
                    array(array('reporter'), 'bug', array('list', 'add')),
                ),
            ),
        ),

My ZendNavigation 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 thoughts on “Integrating BjyAuthorize with ZendNavigation

  1. 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('BjyAuthorizeServiceAuthorize')->getAcl();
    $role = $serviceLocator->get('BjyAuthorizeServiceAuthorize')->getIdentity();
    $nav->setAcl($acl);
    $nav->setRole($role); // Todo replace
    $nav->setUseAcl();
    return $nav->setUlClass('nav')->setTranslatorTextDomain(__NAMESPACE__);
    }

Comments are closed.