Zend_Navigation: Getting Only the Pages Accessible by the Current User/Role
I have a Zend_Navigation running with data provided by a navigation.xml file. Let's assume the first level (0) consists of two pages, Frontend and Backend. Frontend is accessible by the guest role, Backend only by the admin role.
If I do a
<?php echo $this->navigation()->menu()->setMaxDepth(0); ?>
it correctly displays the "Frontend" link when not logged in, and both the "Frontend" and "Backend" links when logged in as an admin.
However, displaying the "Frontend" link doesn't make much sense for guests, because they don't have any other pages to navigate to on that level anyway. So I would rather not display that navigation at all for guests.
I know I could do
<?php
if ('guest' !== $this->view->role) {
echo $this->navigation()->menu();
}
?>
but I'm hoping for a better way to do this.
What I'm looking for is something like
<?php
if (count($this->navigation()->getPagesForRole($this->view->role)) > 1) {
echo $this->navigation()->menu();
}
?>
I can't figure out how to achieve this with the API provided by Zend_Navigation... I see there is a getPages() method, but it returns all pages "unfiltered".
I assume there is an easy solution to this, but I've been trying to figure this out for the past two hours and found nothing, so I guess I'm looking in the wrong direction.
Thanks for your time.
Edit:
I forgot to mention that the navigation is already using ACL to control privileges, and it's working fine. My only problem is that I do not want to display the 开发者_高级运维Frontend/Backend navigation for the guests, because a navigation of only one link item is pretty useless. Sorry about the confusion.
Zend Navigation requires a Zend_Acl instance to achieve that. You have to adjust your Navigation config to include resource/privilege information, e.g.
$navArray = array(
…,
array(
'module' => 'admin',
'label' => 'Administration',
'resource' => 'admin',
'privilege' => 'index',
'pages' => array(
…
);
See http://framework.zend.com/manual/en/zend.navigation.containers.html
Then you need to set your ACL defining any access restrictions for those resources and privileges.
$acl = new Zend_Acl();
$acl->addRole(new Zend_Acl_Role('user'))
->addRole(new Zend_Acl_Role('admin'));
…
See http://framework.zend.com/manual/en/zend.acl.introduction.html
Your ACL has to be set to the Navigation Helper, along with the role of the current user
$this->getHelper('Navigation')
->setAcl($acl)
->setRole('user');
After that, any calls to the Helper API should be aware of the ACL. In the above example, the admin menu should not be rendered, if user is disallowed access to it in the ACL.
Additional resources:
- http://www.youtube.com/watch?v=n31mQGZxtbE
- http://www.brandonsavage.net/controlling-access-zend_navigation-and-zend_acl/
- http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.navigation.acl
You can use Zend_ACL for this
Zend_Navigation can work with it
You can integrate Zend_Acl
with Zend_Navigation
. Look here.
精彩评论