我无法使用Zend_Acl正常工作

时间:2012-08-02 06:53:35

标签: php zend-framework navigation zend-acl

我不知道是什么问题,但我正在尝试设置的导航菜单没有正确显示..

包含标签的所有导航页面<资源>没有显示所有类型的用户类型(管理员,访客和成员),任何想法为什么我有这个问题?

这是我的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>

,我在引导程序中的_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);

,我在引导程序中的_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上尝试了所有解决方案,但没有正常工作,

1 个答案:

答案 0 :(得分:0)

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

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