Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

My guard configuration in bjyauthorize.global.php

'guards' => array(
    'BjyAuthorize\Guard\Controller' => array(
        array('controller' => 'Article\Controller\Article', '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.

like image 363
user1893983 Avatar asked Dec 11 '12 08:12

user1893983


2 Answers

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(
    'BjyAuthorize\Guard\Controller' => array(
        array('controller' => 'cebEvent.registrationController','action' => 'add', 'roles' => array('guest','registration_manage')),
    ),
 ),
like image 33
griesi Avatar answered Nov 16 '22 11:11

griesi


To help out anyone that is attempting this using BjyAuthorize\Guard\Route...

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(
    'BjyAuthorize\Guard\Route' => 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('BjyAuthorize\Service\Authorize');

$acl  = $auth->getAcl();
$role = $auth->getIdentity();
\Zend\View\Helper\Navigation::setDefaultAcl($acl);
\Zend\View\Helper\Navigation::setDefaultRole($role);

I hope this helps somebody out there.

like image 82
moranjk Avatar answered Nov 16 '22 11:11

moranjk