我无法使用Zend_Acl使导航正常工作



我不知道问题出在哪里,但我试图设置的导航菜单没有正确显示。。

所有包含标签<resource>并没有显示给所有类型的用户类型(管理员、访客和会员),知道我为什么会有这个问题吗?

这是我的navigation.xml文件的一部分:

<nav>
    <home>
        <label>Acceuil</label>
        <controller>index</controller>
        <action>index</action>
    </home>
    <contact>
        <label>Contact</label>
        <controller>index</controller>
        <action>contact</action>
        <resource>index</resource>
    </contact>
    <Categories>
        <label>Categories</label>
        <controller>index</controller>
        <action>categories</action>
    </Categories>
    <administration>
        <label>Administration</label>
        <controller>admin</controller>
        <action>index</action>
        <resource>admin</resource>
        <pages>
                <societes>
                    <label>Societes</label>
                    <controller>admin</controller>
                    <action>societes</action>
                    <resource>admin</resource>
                </societes>
        </pages>
    </administration>
</nav>

,引导程序中的my_initAcl:

$acl = new Zend_Acl();
    // add the roles
    $acl->addRole(new Zend_Acl_Role('guest'));
    $acl->addRole(new Zend_Acl_Role('user'), 'guest');
    $acl->addRole(new Zend_Acl_Role('admin'), 'user');
    // add the resources I have only two controller + Error
    $acl->add(new Zend_Acl_Resource('index'));
    $acl->add(new Zend_Acl_Resource('admin'));
    $acl->add(new Zend_Acl_Resource('error'));
    // set up the access rules
    $acl->allow(null, array('error'));
    // a guest can only read content and login
    $acl->allow('guest', 'index', array('contact', 'index', 'login', 'categories'));
    // users can also work with content
    $acl->allow('user', 'index', array('signaler', 'logout'));
    $acl->deny('user', 'index', array('inscription', 'login'));
    // administrators can do anything
    $acl->allow('admin', 'admin', array('index', 'categories', 'ajout-categorie', 'modifier-categorie', 'supprimer-categorie'));
    $acl->deny('admin', 'index', array('contact'));
    Zend_Registry::set('acl', $acl);

,引导程序中的my_initNavigation():

protected function _initNavigation(){
    $this->bootstrap('layout');
    $layout = $this->getResource('layout');
    $view = $layout->getView();
    $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav');
    $container = new Zend_Navigation($config);
    $acl = Zend_Registry::get('acl');
    $auth = Zend_Auth::getInstance();
    if($auth->hasIdentity()) {
        $identity = $auth->getIdentity();
        $role = strtolower($identity->role);
    }else{
        $role = 'guest';
    }
    echo $role;
    $view->navigation($container)->setAcl($acl)->setRole($role);
}

我的Acl类:Application_Plugin_Acl

public function preDispatch(Zend_Controller_Request_Abstract $request){
    $this->acl = Zend_Registry::get('acl');
    // fetch the current user
    $auth = Zend_Auth::getInstance();
    if($auth->hasIdentity()) {
        $identity = $auth->getIdentity();
        $role = strtolower($identity->role);
    }else{
        $role = 'guest';
    }
    $controller = $request->controller;
    $action = $request->action;
    if (!$this->acl->isAllowed($role, $controller, $action)) {
        if ($role === 'guest') {
            $request->setControllerName('index');
            $request->setActionName('login');
        } else {
            $request->setControllerName('error');
            $request->setActionName('noauth');
        }
    }
}

我的application.ini中也有这样的内容:指向Acl类

resources.frontController.plugins.acl = Application_Plugin_Acl

如果问题太长,很抱歉,但我在Stack Overflow上尝试了所有解决方案,但都没有正常工作,

我使用的允许和拒绝方法是错误的:

$this->allow('guest', 'index');
$this->deny('guest', array('signaler','logout'));
// cms users can also work with content
$this->allow('user', 'index');
$this->deny('user',array('inscription','login'));
// administrators can do anything
$this->allow('admin', 'admin');
$this->deny('admin','index',array('contact','about'));

相关内容

  • 没有找到相关文章

最新更新