我已经设置 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');
因此,“管理员”角色可以访问所有内容,“用户”只能访问前台。Frontoffice 是模块的名称,backoffice 是模块的名称。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,则用户和管理员的菜单项都会消失,但他们都应该能够查看它。这是引导程序中的导航代码:
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);
}
有人对如何解决这个问题有建议吗?提前致谢!