I have setup Zend_Acl to work like this:
$acl->addRole(new Zend_Acl_Role('admin'));
$acl->addRole(new Zend_Acl_Role('user'));
$acl->add(new Zend_Acl_Resource('frontoffice'));
$acl->add(new Zend_Acl_Resource('backoffice'));
$acl->deny('user');
$acl->allow('user', null, 'frontoffice');
$acl->allow('admin');
So the role 'admin' has acces to everything, 'user' has only access to frontoffice. Frontoffice is the name of a module and backoffice is the name of a module. The Acl is checked in a custom plugin:
<?php
class Custom_Controller_Plugin_Auth extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$loginController = 'auth';
$loginAction = 'index';
$auth = Zend_Auth::getInstance();
// If user is not logged in and is not requesting login page
// - redirect to login page.
if (!$auth->hasIdentity()
&& $request->getControllerName() != $loginController
&& $request->getActionName() != $loginAction) {
$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
$redirector->gotoSimpleAndExit($loginAction, $loginController);
}
// User is logged in or on login page.
if ($auth->hasIdentity()) {
// Is logged in
// Let's check the credential;
$registry = Zend_Registry::getInstance();
$acl = $registry->get('acl');
$identity = $auth->getIdentity();
// role is a column in the user table (database)
$isAllowed = $acl->isAllowed($identity->role, null,
$request->getModuleName());
if (!$isAllowed) {
$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
$redirector->gotoUrlAndExit('/');
}
}
}
}
?>
Now, the name of my resource is the name of the current module. If I plug the acl into Zend_Navigation, and set the resource of a menu item to frontoffice, the menu item disappears both for user and admin, but they both should be able to view it. This is the navigation code in the bootstrap:
protected function _initNavigation()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$navigation = new Zend_Navigation($this->getOption('navigation'));
$auth = Zend_Auth::getInstance();
$role = $auth->getIdentity()->role;
$view->navigation($navigation)->setAcl(Zend_Registry::get('acl'))
->setRole($role);
}
Does anyone have a suggestion on how to solve this? Thanks in advance!