开发者

is there a way to set up the acl roles that are allowed to access different parts of the site in my navigation.xml?

I have this in my bootstrap:

protected function _initAutoload()
{
    $this->_auth = Zend_Auth::getInstance();
    $this->_acl = new Federico_Plugin_Acl($this->_auth);
 ....
}
....
protected function _initNavigation()
{
    $this->bootstrap('view');

    $view = $this->getResource('view');
    $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml','nav');
    $navigation = new Zend_Navigation($config);
    $view->navigation($navigation)->setAcl($this->_acl)
                                  ->setRole($this->_auth->getStorage()->read()->role);//I just added this
}

however the insert I just did generated this:

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 106 and defined in /home/fiodorovich/library/ZendFramework/library/Zend/View/Helper/Navigation/HelperAbstract.php on line 333

And this is what my navigation.xml looks like so far:

<configdata>
<nav>
    <home>
        <label>HOME</label>
        <controller>index</controller>
        <action>index</action>
    </home>
    <about>
        <label>Nosotros</label>
        <module>default</module>
        <controller>about</controller>
        <action>index</action>
    </about>
<admin>
        <label>Admin</label>
        <uri>admin/index</uri>
        <resource>admin</resource>
        <pages>
            <alta>
                <active>0</active>
                <label>Alta Usuario</label>
                <controller>users</controller>
                <action>create</action>
            </alta>    
        </pages>
    </admin>
</nav>

Right now, even the guests users can see that items in the nav, althogh they can't access since that's already set up in the Acl class... how do I pass the acl roles in here?

EDIT:

//my 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_Control开发者_StackOverflow社区ler_Request_Abstract $request)
{
    if ($this->_auth->hasIdentity()) {
        // user is logged in and we can get role
        $role = $this->_auth->getStorage()->read()->role;  
    } else {
        // guest
        $role = self::DEFAULT_ROLE;
    }
    $action = $request->getActionName();
    $controller = $request->getControllerName();
    if($this->_acl->has($controller)) {
        if(!$this->_acl->isAllowed($role, $controller, $action)) {
            $request->setActionName('error');
            $request->setControllerName('error');
        }
    }
}
}


Get the Zend_View instance (in your bootstrap, in an action helper, wherever it's easier for you) and then:

$view->navigation()
    ->setAcl(Zend_Acl $acl)
    ->setRole(Zend_Acl_Role $role);

Basically, the navigation view helper must explicitly be given knowledge about the ACL and current role.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜