la_f0ka
la_f0ka

Reputation: 1773

Having problems combining Zend_Acl and Zend_Navigation

I have a Federico_Plugin_Acl that extends Zend_Controller_Plugin_Abstract which looks like this:

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');
        }
    }
}
}

And this method is in my bootstrap to make use of this class:

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);
}

With these configurations set like that, I get the following error:

Catchable fatal error: Argument 1 passed to Zend_View_Helper_Navigation_HelperAbstract::setAcl() must be an instance of Zend_Acl, instance of Federico_Plugin_Acl given, called in /home/fiodorovich/public_html/gisele/application/Bootstrap.php on line 118 and defined in /home/fiodorovich/library/ZendFramework/library/Zend/View/Helper/Navigation/HelperAbstract.php on line 333 Call Stack

Which is to be expected since Federico_Plugin_Acl is an instance of Zend_Controller_Plugin_Abstract...Still, if I extend Zend_Acl instead I get this error:

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

So...I've been trying for a while to get this thing solved,... but don't seem to get this to work properly...Any ideas on what I'm missing here?

Upvotes: 1

Views: 1008

Answers (1)

akond
akond

Reputation: 16035

Zend_Controller_Plugin_Abstract and Zend_Acl are completelly different things. What you want to do is get the ACL object out of your plugin (which is now in private section) and pass it over to

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

Upvotes: 1

Related Questions