CakePHP Auth-> loginRedirect无效

时间:2012-12-06 14:52:46

标签: cakephp

我正在使用cakePHP 2.2。

在我之前的App Controller过滤器中,我有以下内容:

    // Admin
    if($this->Auth->user('group_id') == '12') {
            $this->Auth->allow('admin_index'); 
            $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => false);
            $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'index', 'admin' => TRUE);
            $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => false);

            $this->set("group", "admin");

    // Staff
    } elseif($this->Auth->user('group_id') == '13') {
            $this->Auth->allow('admin_index'); 
            $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => false);
            $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'index', 'admin' => TRUE);
            $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => false);

            $this->set("group", "staff");

    // Users
    } else {

            $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => false);
            $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'index', 'admin' => false);
            $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => false);

            $this->set("group", "user");
        }

第一个和第二个if工作正常并且在登录时将用户重定向到适当的页面,但是第三个重定向根本不起作用,它只是让你在登录页面上,但它确实显示来自的登录页面的flash_success消息登录功能???

有人能指出我正确的方向吗?

提前致谢

*添加了来自Tim *

的信息

过滤器和登录功能之前的用户控制器:

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('logout');
    $this->Auth->allow('login');
    $this->Auth->allow('forgetpwd');
    $this->Auth->allow('reset');
    $this->Auth->allow('initDB');
    $this->Auth->allow('register');
    //$this->Auth->allow('admin_importOldUsers');
    //$this->Auth->allow('*');
}

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

        $userStatusCheck = $this->User->find('first',array('conditions'=>array('User.username'=>$this->request->data['User']['username'])));

        // If user is not deleted
        if ($userStatusCheck['User']['live'] == 1) {

            // Users status.
            /* 1 = Disabled, 2 = Enabled, 3 = Waiting for Auth, 4 = refer to Darren */
            $us = $userStatusCheck['User']['user_status_id'];

            // If account is disabled
            if ($us == 1) {
                $this->Session->setFlash('Your account has been disabled, please conntact our support team.', 'flash_failure');
            } 

            // If account is waiting for authorisation
            else if ($us == 3) {
                $this->Session->setFlash('Your account is waiting to be authorised', 'flash_failure');
            }

            // If accoutn is referred to Darren
            else if ($us == 4) {
                $this->Session->setFlash('Your account is waiting to be authorised', 'flash_failure');
            } 

            // Account is enabled, proceed to login
            else if ($us == 2) {

                if ($this->Auth->login()) {
                    if ($this->Session->read('Auth.User')) {
                        $this->Session->setFlash('You are logged in!', 'flash_success');
                        //$this->redirect($this->Auth->redirect());

                        if($this->Auth->user('group_id') == '12' OR $this->Auth->user('group_id') == '13'){
                            $this->redirect('/admin/');
                        } else {
                            $this->redirect($this->Auth->redirect());
                        }
                    }            
                }
                else {
                    $this->Session->setFlash('Your username or password was incorrect.', 'flash_failure');
                }
            }

        } else {

            $this->Session->setFlash('Your account has been disabled, please conntact our support team.', 'flash_failure');

        }

    }

1 个答案:

答案 0 :(得分:1)

虽然我仍然没有为用户组提供AppFtroller beforefilter auth重定向代码,但我设法使用此代码在usersController的login()函数中捏造它:

if($this->Auth->user('group_id') == '12' OR $this->Auth->user('group_id') == '13'){
$this->redirect('/admin/');
} else {    
    $this->redirect(array('controller' => 'pages', 'action' => 'index', 'admin' => false)); #this does not work...
    //$this->redirect($this->Auth->redirect()); # Does not work
}

但让其他工作的关键是这一行

$this->Auth->allow(array('controller' => 'pages', 'action' => 'index', 'admin' => false));

到AppController beforeFeature制作它:

if($this->Auth->user('group_id') == '14') {
        $this->Auth->allow(array('controller' => 'pages', 'action' => 'index', 'admin' => false));
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => false);
        $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'index', 'admin' => false);
        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => false);

        $this->set("group", "user");
    }

这感觉就像一个完全的软糖,但在花了几天阅读SO和各种其他网站和蛋糕文档后,我看不到其他方法使auth componant在登录时重定向到不同的页面,具体取决于用户组。

如果有人找到更好的方法来做到这一点,我很乐意知道!