0
當用戶是訪客,他訪問/admin/
我希望重定向到/admin/login
,並且當用戶處於默認模塊並訪問他無權訪問的資源時,我希望它重定向到/error
。Zend:管理員登錄策略
假設/admin
是一個模塊,我怎麼能做到這一點?這是我到目前爲止有:
<?php
class KG_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
public function __construct(
Zend_Acl $acl,
Zend_Auth $auth
) {
$this->_acl = $acl;
$this->_auth = $auth;
}
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$auth = $this->_auth;
if($auth->hasIdentity()) {
$identity = $auth->getIdentity();
$role = strtolower($identity->role);
}else{
$role = 'guest';
}
$controller = $request->controller;
$action = $request->action;
$module = $request->module;
if ($module == 'admin') {
if (!$this->_acl->isAllowed($role, $controller, $action)) {
if ($role == 'guest') {
$request->setControllerName('index');
$request->setActionName('index');
} else {
$request->setControllerName('login');
$request->setActionName('index');
}
}
} else {
if (!$this->_acl->isAllowed($role, $controller, $action)) {
if ($role == 'guest') {
$request->setControllerName('user');
$request->setActionName('login');
} else {
$request->setControllerName('error');
$request->setActionName('noauth');
}
}
}
}
}
而對於定義角色:
<?php
class Model_Acl extends Zend_Acl {
public function __construct() {
$this->addRole(new Zend_Acl_Role('guest'));
$this->addRole(new Zend_Acl_Role('user'), 'guest');
$this->addRole(new Zend_Acl_Role('administrator'), 'user');
$this->add(new Zend_Acl_Resource('index'));
$this->add(new Zend_Acl_Resource('error'));
$this->add(new Zend_Acl_Resource('admin'));
$this->add(new Zend_Acl_Resource('page'));
$this->add(new Zend_Acl_Resource('news'));
$this->add(new Zend_Acl_Resource('mvc:user_signin'));
$this->add(new Zend_Acl_Resource('mvc:user_signout'));
$this->add(new Zend_Acl_Resource('menu'));
$this->add(new Zend_Acl_Resource('menuitem'));
$this->add(new Zend_Acl_Resource('user'));
$this->add(new Zend_Acl_Resource('search'));
$this->add(new Zend_Acl_Resource('feed'));
$this->add(new Zend_Acl_Resource('bug'));
$this->allow(null, array('index', 'error'));
$this->allow('guest', 'page', array('index', 'open'));
$this->allow('guest', 'menu', array('render'));
$this->allow('guest', 'user', array('login'));
$this->allow('guest', 'search', array('index', 'search'));
$this->allow('guest', 'feed');
$this->allow('guest', 'news');
$this->allow('guest', new Zend_Acl_Resource('mvc:user_signin'), 'navigate');
$this->deny('guest', new Zend_Acl_Resource('mvc:user_signout'), 'navigate');
$this->allow('user', new Zend_Acl_Resource('mvc:user_signout'), 'navigate');
$this->allow('user', 'page', array('list', 'create', 'edit', 'delete'));
$this->allow('administrator', null);
}
}
我試圖->deny('guest', 'admin')
得到它重定向到管理員登錄控制器。希望對此有合適的解決方案提供建議。