Zend_Acl和Zend_Navigation不使用模块名称作为资源

时间:2012-07-29 08:45:27

标签: zend-framework module zend-navigation zend-acl

我已经设置Zend_Acl这样工作:

$acl->addRole(new Zend_Acl_Role('admin'));
$acl->addRole(new Zend_Acl_Role('user'));
$acl->add(new Zend_Acl_Resource('frontoffice'));
$acl->add(new Zend_Acl_Resource('backoffice'));
$acl->deny('user');
$acl->allow('user', null, 'frontoffice');
$acl->allow('admin');

因此'admin'角色可以访问所有内容,'user'只能访问前台。 Frontoffice是模块的名称,后台是模块的名称。在自定义插件中检查Acl:

<?php
class Custom_Controller_Plugin_Auth extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
    $loginController = 'auth';
    $loginAction     = 'index';

    $auth = Zend_Auth::getInstance();

    // If user is not logged in and is not requesting login page
    // - redirect to login page.
    if (!$auth->hasIdentity()
            && $request->getControllerName() != $loginController
            && $request->getActionName()     != $loginAction) {

        $redirector =     Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
        $redirector->gotoSimpleAndExit($loginAction, $loginController);
    }

    // User is logged in or on login page.

    if ($auth->hasIdentity()) {
        // Is logged in
        // Let's check the credential;
    $registry = Zend_Registry::getInstance();
        $acl = $registry->get('acl');

        $identity = $auth->getIdentity();
        // role is a column in the user table (database)
        $isAllowed = $acl->isAllowed($identity->role, null,
                                     $request->getModuleName());
        if (!$isAllowed) {
            $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
            $redirector->gotoUrlAndExit('/');
        }
    }
}
}
?>

现在,我的资源名称是当前模块的名称。如果我将acl插入Zend_Navigation,并将菜单项的资源设置为frontoffice,则菜单项对于用户和管理员都会消失,但它们都应该能够查看它。这是bootstrap中的导航代码:

protected function _initNavigation()
{
    $this->bootstrap('layout');
    $layout = $this->getResource('layout');
    $view = $layout->getView();
    $navigation = new Zend_Navigation($this->getOption('navigation'));
    $auth = Zend_Auth::getInstance();
    $role = $auth->getIdentity()->role;
    $view->navigation($navigation)->setAcl(Zend_Registry::get('acl'))
                      ->setRole($role);
}

有没有人建议如何解决这个问题?提前谢谢!

1 个答案:

答案 0 :(得分:0)

据我所知,您无法直接在ACL中定义模块。您必须使用以下语法定义每个模块的每个控制器:

$this->add(new Zend_Acl_Resource("module_name:controller_name");