我正在为我的应用程序创建 ACL。但我似乎无法找到错误在哪里。由于它是一个简单的 ACL,我认为在这里找到问题并不难。我收到以下错误:致命错误:Zend_Acl_Role_Registry_Exception: Role 'secretaria' not found in /usr/local/zend/share/ZendFramework/library/Zend/View/Helper/Navigation/HelperAbstract.php on line 522 这是我的代码(下)。如果有人可以帮助我找到我会非常感激的错误。
<?php
class Plugins_acl extends Zend_Controller_Plugin_Abstract{
//NOME DOS RESOURCES
//private $modules = array();
//private $perfils = array();
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$acl = new Zend_Acl();
//create the user roles
//1-ADMINISTRADOR
//2-MEDICO
//3- ASSISTENTE
//4- SECREATRIA
$acl->addRole(new Zend_Acl_Role('administrador'));
$acl->addRole(new Zend_Acl_Role('medico'));
$acl->addRole(new Zend_Acl_Role('assistente'));
$acl->addRole(new Zend_Acl_Role('secretaria'));
$acl->addRole(new Zend_Acl_Role('guest'));
//create the resources
$acl->addResource(new Zend_Acl_Resource('index'));
$acl->addResource(new Zend_Acl_Resource('error'));
$acl->addResource(new Zend_Acl_Resource('dashboard'));
$acl->addResource(new Zend_Acl_Resource('login'));
$acl->addResource(new Zend_Acl_Resource('default'));
$acl->allow(null,'index');
$acl->allow('secretaria','dashboard');
$acl->allow('secretaria','index');
$acl->allow('secretaria','default');
$acl->allow('guest','login');
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity())
{
$identity = $auth->getIdentity();
$role = strtolower($identity['role']);
}
else
{
$role = 'guest';
}
$controller = $request->controller;
$action = $request->action;
$module= $request->module;
if(!$acl->isAllowed($role, $module))
{
if('guest' == $role)
{
$request->setModuleName('auth');
$request->setControllerName('login');
$request->setActionName('index');
}
else
{
$request->setModuleName('error');
$request->setControllerName('error');
$request->setActionName('notauthorized'); }
}
}
}
这是我的导航代码
public function _initNavigation ()
{
$this->acl = Zend_Registry::get('acl');
$this->bootstrap('layout');
$this->role = Zend_Auth::getInstance()->getStorage()->read();
$layout = $this->getResource('layout');
$view = $layout->getView();
$navConfig = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav');
$navigation= new Zend_Navigation($navConfig);
$view->navigation($navigation)->setAcl($this->acl)->setRole($this->role['Role']);
}
此时的问题是找不到 acl 的注册表;因此在加载导航时它不知道角色等...
<?php
类 Plugins_acl 扩展 Zend_Controller_Plugin_Abstract{
//NOME DOS RESOURCES
//private $modules = array();
//private $perfils = array();
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$acl =new Zend_Acl();
//create the user roles
//1-ADMINISTRADOR
//2-MEDICO
//3- ASSISTENTE
//4- SECREATRIA
$acl->addRole(new Zend_Acl_Role('administrador'));
$acl->addRole(new Zend_Acl_Role('medico'));
$acl->addRole(new Zend_Acl_Role('assistente'));
$acl->addRole(new Zend_Acl_Role('secretaria'));
$acl->addRole(new Zend_Acl_Role('guest'));
//create the resources
$acl->addResource(new Zend_Acl_Resource('index'));
$acl->addResource(new Zend_Acl_Resource('error'));
$acl->addResource(new Zend_Acl_Resource('dashboard'));
$acl->addResource(new Zend_Acl_Resource('login'));
$acl->addResource(new Zend_Acl_Resource('default'));
$acl->allow(null,'index','index');
$acl->allow('secretaria','dashboard');
$acl->allow('secretaria','index');
$acl->allow('secretaria','default');
$acl->allow('guest','login');
Zend_Registry::set('Zend_Acl', $acl);
$auth = Zend_Auth::getInstance();
$roleUser = Zend_Auth::getInstance()->getStorage()->read();
Zend_Registry::set('Zend_Acl_Role', $roleUser['Role']);
if($auth->hasIdentity())
{
$identity = $auth->getIdentity($roleUser['Role']);
$role = strtolower($identity['Role']);
}
else
{
$role = 'guest';
}
$controller = $request->controller;
$action = $request->action;
$module= $request->module;
if(!$acl->isAllowed($role, $module))
{
if('guest' == $role)
{
$request->setModuleName('auth');
$request->setControllerName('login');
$request->setActionName('index');
}
else
{
$request->setModuleName('error');
$request->setControllerName('error');
$request->setActionName('notauthorized'); }
}
/*
$this->modules = $aclNs->resources['modules'];
$this->perfils = $aclNs->resources['perfils'];
$this->modules[]= 'default';
$this->modules[]= 'logout';
$this->modules[]= 'login';
$this->modules[]= 'error';
$this->modules[]= 'index';
$this->modules[]= 'gerenciar';
*/
}
}