结合Zend_Acl和Zend_Navigation有问题

时间:2011-06-09 14:23:31

标签: zend-framework zend-acl

我有一个扩展Zend_Controller_Plugin_Abstract的Federico_Plugin_Acl,如下所示:

class Federico_Plugin_Acl extends Zend_Controller_Plugin_Abstract {

private $_acl = null;
private $_auth = null;
const DEFAULT_ROLE = 'guest';

public function __construct($auth) {
    $this->_auth = $auth;

    $this->_acl = new Zend_Acl();
    $this->_acl->addRole(new Zend_Acl_Role(self::DEFAULT_ROLE));
    $this->_acl->addRole(new Zend_Acl_Role('user'), self::DEFAULT_ROLE);
    $this->_acl->addRole(new Zend_Acl_Role('admin'), 'user');

    $this->_acl->addResource(new Zend_Acl_Resource('index'));
    $this->_acl->addResource(new Zend_Acl_Resource('users'));
    $this->_acl->addResource(new Zend_Acl_Resource('about'));
    $this->_acl->addResource(new Zend_Acl_Resource('gisele'));
    $this->_acl->addResource(new Zend_Acl_Resource('admin'));

    $this->_acl->allow('guest', 'index');
    $this->_acl->allow('guest', 'about');
    $this->_acl->deny('guest', 'gisele');
    $this->_acl->deny('guest', 'users');

    $this->_acl->allow('user', 'users', array('index')); 
    $this->_acl->allow('admin', 'users');
    $this->_acl->allow('admin', 'gisele');
}

public function preDispatch(Zend_Controller_Request_Abstract $request) {
    if ($this->_auth->hasIdentity()) {
        $role = $this->_auth->getStorage()->read()->role;
    } else {
        $role = self::DEFAULT_ROLE;
    }

    $action = $request->getActionName();
    $controller = $request->getControllerName();
    if ($this->_acl->has($controller)) {
        if (!$this->_acl->isAllowed($role, $controller, $action)) {
            $request->setActionName('login');
            $request->setControllerName('index');
        }
    }
}
}

这个方法在我的引导程序中使用这个类:

protected function _initNavigation()
{
    $this->_auth = Zend_Auth::getInstance();
    $this->_acl = new Federico_Plugin_Acl($this->_auth);

    $this->bootstrap('view');

    $view = $this->getResource('view');
    $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml','nav');
    $navigation = new Zend_Navigation($config);

    $roleAuth = $this->_auth->getIdentity();
     if(null == $roleAuth)
        $role = 'guest';
    else
        $role = $roleAuth->role;

    $view->navigation($navigation)->setAcl($this->_acl)->setRole($role);
}

这些配置设置如下,我收到以下错误:

  

可捕获的致命错误:传递给Zend_View_Helper_Navigation_HelperAbstract的参数1 :: setAcl()必须是Zend_Acl的一个实例,给出Federico_Plugin_Acl的实例,在第118行的/home/fiodorovich/public_html/gisele/application/Bootstrap.php中调用并定义在第333行的/home/fiodorovich/library/ZendFramework/library/Zend/View/Helper/Navigation/HelperAbstract.php   调用堆栈

由于Federico_Plugin_Acl是Zend_Controller_Plugin_Abstract的一个实例,所以可以预料到......但是,如果我扩展Zend_Acl而得到此错误:

Fatal error: Zend_Acl_Role_Registry_Exception: Role 'guest' not found in /home/fiodorovich/library/ZendFramework/library/Zend/View/Helper/Navigation/HelperAbstract.php on line 522

所以...我已经尝试了一段时间才能解决这个问题,但是似乎没有让这个工作正常......对于我在这里缺少什么的任何想法?

1 个答案:

答案 0 :(得分:1)

Zend_Controller_Plugin_Abstract和Zend_Acl是完全不同的东西。你想要做的是从插件中获取ACL对象(现在是私有部分)并将其传递给

$view->navigation($navigation)->setAcl(<here>);