Question

I am trying to combine Zend_Navigation and Zend_Acl for URI pages, in a standalone fashion (i.e. only these ZF libraries). Unfortunately, even if I mean for a role not to see some pages, I see them in the menu (although if I call isAllowed, all works as expected).

Following a code snippet that shows my problem:

$pages = array (
  array( 'id' => '1', 'label' => 'page 1', 'uri' => '1.html', 'visible' => 1 ),
  array( 'id' => '2', 'label' => 'page 2', 'uri' => '2.html', 'visible' => 1 ),
  array( 'id' => '3', 'label' => 'page 3', 'uri' => '3.html', 'visible' => 1 )
);

$nav = new Zend_Navigation($pages);

$acl = new Zend_Acl();
$acl->addRole(new Zend_Acl_Role(1));
$acl->addRole(new Zend_Acl_Role(2));
$acl->addResource(new Zend_Acl_Resource(1));
$acl->addResource(new Zend_Acl_Resource(2));
$acl->addResource(new Zend_Acl_Resource(3));
$acl->allow(1, 1);
$acl->allow(1, 2);
$acl->allow(1, 3);
$acl->allow(2, 1);

// Role 1 sees 1,2,3 while Role 2 only 1

$view = new Zend_View();

$menu = new Zend_View_Helper_Navigation_Menu();
$menu->setView($view)
     ->setContainer($nav)
     ->setTranslator($translate)
     ->setAcl($acl)->setRole('2'); //tried both this or ->setAcl($acl) ->setRole('2');

echo "2 can't see 2 and 3, right? " . ((!$acl->isAllowed('2', '2'))?"right":"not right") . "\n";

echo $menu->menu()->renderMenu(
  null,
  array(
      'minDepth' => 0,
      'maxDepth' => 1,
      'onlyActiveBranch' => false,
      'renderParents' => true
  )
);

How can I make the above work? Thanks!!

Was it helpful?

Solution

Associate a resource with a page like this

$pages = array(
    array('id' => '1', ... , 'resource' => '1'),
    array('id' => '2', ... , 'resource' => '2'),
    array('id' => '3', ... , 'resource' => '3')
);

the rest is fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top