Cakephp Auth-> loginredirect问题

时间:2013-04-08 06:58:25

标签: cakephp-2.0

我做了一个简单的cakephp应用程序。目前我正在使用auth组件 根据他们的用户将用户发送到各自的页面。对于ex,如果role = 1发送到管理页面,否则如果role = 2发送到管理员页面。我正在使用session和auth组件来查看它们的工作方式并将数据保存在其中。下面是usercontroller登录操作的代码

public function login(){

$this->Session->setFlash($this->Auth->user('role'));//checks for data in auth component if any



    if($this->request->is('post') ){


         $results = $this->User->findByEmail($this->request->data['User']['username']);
        if($results &&$results['User']['password']== md5($this->request->data['User']['password']))
        {
            $this->Session->write('user',$results['User']);
            $this->Auth->login($results['User']);
            $this->Session->setFlash('User logged in successfully'.$this->Auth->user('role'));
            return $this->redirect($this->Auth->redirect());
        }
        else
        {
            $this->Session->setFlash('Login is incorrect');
        }
    }


}

问题是登录工作正常所有数据都存储在session和auth变量中,但loginredirect表现得很奇怪。在我的Chrome浏览器中。它总是重定向到管理页面,无论角色是什么,但它闪烁正确的消息,我在闪存中设置。 appcontroller中的beforefilter代码

public function beforeFilter(){

    $this->Auth->allow('display'); 

    $this->Auth->loginAction = array('controller' => 'Users', 'action' => 'login');

    $this->Auth->logoutRedirect = array('controller' => 'Users', 'action' => 'login');

    if($this->Auth->user('role') == '1'){
            $this->Session->setFlash($this->Auth->user('role').'adminnnnnnnnnnnnnnnnnnnnn');
    $this->Auth->loginRedirect = '/admins/index';
    }

    if($this->Auth->user('role') == '2'){
        $this->Session->setFlash('moderatorrrrrrrrrrrrrrrrr');
        $this->Auth->loginRedirect = '/users/index';
    }
}

所以问题是循环在过滤器之前运行良好,setflash显示用户是管理员还是管理员,但由于某种原因它无论是谁登录,它都会重定向到只有单页的admins / index page或users / index page 。这是Chrome浏览器上的行为。 在firefox上,loginredirects将用户发送到webroot / index页面,但flash消息再次正确。

我不确定我的错误是我的代码中有问题还是cakephp 2.0 auth组件有测量错误。

1 个答案:

答案 0 :(得分:1)

用户登录后,

通过Auth-> loginRedirect重定向到仪表板(),在这里我检查用户角色并使用重定向将特定用户发送到确切位置

  function dashboard() {
  //get user's group (role)
    //$role = $this->Session->read('user.role');
    $role=$this->Auth->user('role');
        //user selection logic here
    if($role== '1'){
        $this->redirect(array('controller' => 'users','action' => 'admin_index','admin' => false));

    }
    else if($role == '2'){
        $this->redirect(array('controller' => 'users','action' => 'admin_index', 'admin' => false));

    }
    else if($role == '9'){
        $this->redirect(array('controller' => 'users', 'action' => 'index', 'admin' => false));
        $this->Session->setFlash('3');
    }

   }

这只是解决问题的另一种方法我在用户控制器中包含了仪表板功能,并且从appcontroller进行了auth登录重定向到此功能。 希望它能解决面临问题的其他人的问题。感谢

相关问题