如何为不同的角色调用不同的登录重定向

时间:2015-02-07 11:22:02

标签: cakephp login cakephp-2.x

App Controller:

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'));

    }
}

用户表admin和customer中有两种类型的角色, 如果admin登录loginredirect是'loginRedirect' =>array('controller' => 'Item' , 'action' => 'index')
如果客户已登录'loginRedirect' =>array('controller' => 'customer' , 'action' => 'view') .

用户登录取决于角色。

如何根据角色提供loginredirect

1 个答案:

答案 0 :(得分:2)

尝试在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'
        );
   }


}