应用程序控制器:
class AppController extends Controller {
public $components= array(
'Session',
'Auth' => array(
'loginRedirect' =>array('controller' => 'Item' , 'action' => 'index'),
'logoutRedirect' =>array('controller' => 'Item' , 'action' => 'index'),
'authError' => 'Login Error',
'authorize' => array('Controller')
)
);
public function isAuthorized($user)
{
return true;
}
public function beforeFilter(){
$this->Auth->allow('login');
}
}
用户控制器:
class UsersController extends AppController{
public $name= 'Users';
public function login(){
if($this->request->is('post')){
if($this->Auth->login()){
$this->redirect($this->Auth->Redirect());
}
else{
$this->Session->setFlash('error');
}
}
}
public function logout(){
$this->redirect($this->Auth->logout());
}
public function index(){
$this->User->recursive=0;
$this->set('users',$this->User->find('all'));
}
}
在用户表管理和客户中有两种类型的角色,如果管理员登录,则登录直接为'loginRedirect' =>array('controller' => 'Item' , 'action' => 'index')
如果客户登录'loginRedirect' =>array('controller' => 'customer' , 'action' => 'view') .
用户登录取决于角色。
如何基于角色提供logindirect
尝试在UsersController:中使用此功能
public function beforeFilter() {
parent::beforeFilter();
if($this->Acl->check('role','A1')){
$this->Auth->logoutRedirect = array(
'controller' => 'customer',
'action' => 'view'
);
}elseif($this->Acl->check('role','A2')){
$this->Auth->logoutRedirect = array(
'controller' => 'users',
'action' => 'login'
);
}
}