1

我有一个扩展 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::setAcl() 的参数 1 必须是 Zend_Acl 的实例,给定的 Federico_Plugin_Acl 实例,在 /home/fiodorovich/public_html/gisele/application/Bootstrap.php 第 118 行调用并在 /home 中定义/fiodorovich/library/ZendFramework/library/Zend/View/Helper/Navigation/HelperAbstract.php 在第 333 行调用堆栈

这是意料之中的,因为 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

所以......我一直在努力解决这个问题,......但似乎没有让它正常工作......关于我在这里缺少什么的任何想法?

4

1 回答 1

1

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

$view->navigation($navigation)->setAcl(<here>);
于 2011-06-09T14:29:21.107 回答