ZF2 Integrating BjyAuthorize with Zend\Navigation

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

My Application/Module.php view helper configuration:

'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__); 
            }

My guard configuration in bjyauthorize.global.php

'guards' => array(
    'BjyAuthorizeGuardController' => array(
        array('controller' => 'ArticleControllerArticle', 'roles' => array('user')),
),

And My navigation.global.php

return array(
    'navigation' => array(
       'default' => array(
           'articles' => array(
                'label' => 'Articles',
                'route' => 'articles',
            ),
        ),
    ));

I try to change routes configuration to module/controller/action and still does not work.


You did not specify the resource within your navigation configuration. Also make sure if $role matches bjyauthorize-identity

'navigation' => array(
  'default' => array(
     array(
       'label' => 'Registration',
   'resource'   => 'controller/cebEvent.registrationController:add',
   'route' => 'registration/add',
     ),
  ),
),

The specified resource must match your guard configuration:

'guards' => array(
    'BjyAuthorizeGuardController' => array(
        array('controller' => 'cebEvent.registrationController','action' => 'add', 'roles' => array('guest','registration_manage')),
    ),
 ),

To help out anyone that is attempting this using BjyAuthorizeGuardRoute...

You will need to prefix your route name with route/ and use that for the resource value.

If your config has a guard like this...

'guards' => array(
    'BjyAuthorizeGuardRoute' => array(
         array('route' => 'zfcadmin/zfcuseradmin/list', 'roles' => array('admin')),
     )
);

You would define something like this for your navigation

'navigation' => array(
    'default' => array(
        'admin' => array(
            'label' => 'Admin',
            'route' => 'zfcadmin',
            'pages' => array(
                'users' => array(
                    'resource' => 'route/zfcadmin/zfcuseradmin/list', // route resource ;)
                    'label' => 'Users',
                    'route' => 'zfcadmin/zfcuseradmin/list',
                ),
            ),
        ),
    ),
),

You can set the default ACL and Role by placing this in your onBootstrap method of your module.

$sm   = $e->getApplication()->getServiceManager();
$auth = $sm->get('BjyAuthorizeServiceAuthorize');

$acl  = $auth->getAcl();
$role = $auth->getIdentity();
ZendViewHelperNavigation::setDefaultAcl($acl);
ZendViewHelperNavigation::setDefaultRole($role);

I hope this helps somebody out there.


Can you post an usage example for using GuardRoute instead of Controller? My example route for this case:

'guards' => array(
    'BjyAuthorizeGuardRoute' => array(
        array('route' => 'zfcadmin/sap-targetvalue/index', 'roles' => array('guest', 'user')),
        array('route' => 'zfcadmin/sap-targetvalue/create', 'roles' => array('user')),
    )
);
链接地址: http://www.djcxy.com/p/22232.html

上一篇: 使用授权在Play Framework中限制对方法的访问

下一篇: ZF2将BjyAuthorize与Zend \ Navigation集成