Zend插件的preDispatch无法看到角色

时间:2013-01-12 12:17:07

标签: zend-framework plugins acl

我尝试在插件的preDispatch方法中使用ACL,但它看不到角色和资源。

在bootstrap中:

protected function _initAutoLoad() {
...

$acl=new Application_Model_LibraryAcl();
$auth=Zend_Auth::getInstance();
$fc= Zend_Controller_Front::getInstance();
$fc->registerPlugin(new Application_Plugin_AccessCheck($acl, $auth));
...
}

在插件的preDispatch中:

$this->_acl = $acl;
$this->_auth=Zend_Auth::getInstance();
$identity = $this->_auth->getStorage()->read();
$role=$identity->role;
$resource = $request->getControllerName();
$action= $request->getActionName();
if($this->_acl->isAllowed($role, $resource, $action)){
...
}

我得到错误'角色'管理员'未找到'(此错误适用于所有角色)。不管我试图做什么都行不通。我通过将ACL代码从Model移动到这个插件来解决这个问题,但我认为在这里保留ACL并不是一个好主意。其他模型和控制器可以从Application_Model_LibraryAcl查看ACL及其角色和资源,但插件的preDispatch不希望这样。

我使用的是ZF 1.12。

EDIT。我的Application_Model_LibraryAcl()是:

<?php
class Application_Model_LibraryAcl extends Zend_Acl {

    public function __construct(){




        //----------------------ADD ROLES HERE---------------------------------

        $acl = new Zend_Acl();
        $acl->addRole(new Zend_Acl_Role('guest'));
        $acl->addRole(new Zend_Acl_Role('user'), 'guest');
        $acl->addRole(new Zend_Acl_Role('admin'), 'user');


        //----------------------ADD RESOURCES HERE---------------------------------

        $acl->add(new Zend_Acl_Resource('authentication'));
        $acl->add(new Zend_Acl_Resource('index'));


        //----------------------ADD PERMITIONS HERE---------------------------------

        $acl->deny();

        $acl->allow('admin', NULL);
        $acl->allow('guest', 'authentication', array('login', 'registration', 'confirm', 'index' ));
        $acl->allow('guest', 'index', array('index' ));
        $acl->allow('user', 'index', array('index', 'confirm'));
        $acl->allow('user', 'authentication', array('logout' ));
        $acl->deny('user', 'authentication', array('login' ));


    }

}

我将当前用户的角色保留在Zend存储中。当我将所有代码从模型移动到插件而没有任何更改时,此代码工作正常。如果我使用model:

,我在这个字符串上有错误
if($this->_acl->isAllowed($role, $resource, $action))
当我使用ACL作为插件的一部分时,

并没有任何错误。

0 个答案:

没有答案