你可以使用一个插件:
//This class effectively extends Zend_Controller_Front
//This plugin will dispatch with every request and provide user access control
//based on credentials provided at login.
class MY_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request) {
parent::preDispatch($request);
//setup ACL
$acl = new Zend_Acl();
//add Roles
$acl->addRole('guest');
//add Resources
$acl->add(new Zend_Acl_Resource('default'));
//set up access rules, everyone has access to the index page and the error page.
$acl->allow(NULL, array('index', 'error'));
$acl->allow(NULL, array('index', 'index'));
//a guest can only read content and login
$acl->allow('guest', 'page', array('index'));
//more ...
//fetch current user, $auth is set in loginAction()
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity()) {
$identity = $auth->getIdentity();
$role = strtolower($identity->role);
} else {
$role = 'guest';
}
$module = $request->getModuleName();
$controller = $request->getControllerName();
$action = $request->getActionName();
if (!$acl->isAllowed($role, $module, $controller, $action)) {
if ($role == 'guest') {
$request->setControllerName('user');
$request->setActionName('login');
} else {
$request->setControllerName('error');
$request->setActionName('noauth');
}
}
}
}
在 application.ini 中注册插件:
resources.frontController.plugins.acl = "My_Controller_Plugin_Acl"
这只是一个(简单)实现,有很多方法可以给这只猫剥皮。
希望这可以帮助。