Different submenus for different user roles in ZF2 navigation (Zend\Navigation)

546 views Asked by At

In a project I use ACL menu for different kind of roles but faced with a problem when try to make same menu for several roles but for some roles I want to hide some points. Documentation says to use 'resource' parameter in array to do it but it works for me only for whole parent menu:

array(
    'label'      => 'Community',
    'module'     => 'community',
    'controller' => 'index',
    'action'     => 'index',
    'resource'   => 'mvc:community'
    'pages'      => array(
        array(
            'label'      => 'My Account',
            'module'     => 'community',
            'controller' => 'account',
            'action'     => 'index',
        ),
        array(
            'label' => 'Forums',
            'uri'   => 'http://forums.example.com/',
            'class' => 'external' // class
        )
    )
)

But not if I want to hide some elements from the current menu:

    array(
    'label'      => 'Community',
    'module'     => 'community',
    'controller' => 'index',
    'action'     => 'index',
    'pages'      => array(
        array(
            'label'      => 'My Account',
            'module'     => 'community',
            'controller' => 'account',
            'action'     => 'index',
            'resource'   => 'mvc:community.account'
        ),
        array(
            'label' => 'Forums',
            'uri'   => 'http://forums.example.com/',
            'class' => 'external'
        )
    )
)

Menu ACL build code:

$acl = new \Zend\Permissions\Acl\Acl();
$acl->addRole(new \Zend\Permissions\Acl\Role\GenericRole('root'));
$acl->addRole(new \Zend\Permissions\Acl\Role\GenericRole('guest'));

$acl->addResource(new \Zend\Permissions\Acl\Resource\GenericResource('mvc:community.account'));

$acl->allow('root', null);
$acl->allow('guest', null);
$acl->deny('guest', 'mvc:community.account');

So when I use 'resource' key for whole menu it works when use it in child - doesn't. At the moment I created several almost similar arrays for each of role and allow/deny access for the one but want to find right way to do it.

1

There are 1 answers

0
user2958410 On

Actually I think it is a bug.

The Problem is, that at the time rendering the menu, the 'resource' entries for submenus are gone. You can solve the issue using your own partial, and there, re-add the 'resource'-entries for subpages. - Of coure, quite dirty solution.