1

我正在使用 cakePHP 2.2。

在我的应用控制器之前的过滤器中,我有以下内容:

    // 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 消息? ??

谁能指出我正确的方向?

提前致谢

*添加了蒂姆要求的信息*

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

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

        }

    }
4

1 回答 1

1

虽然我还没有让 AppController 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 和其他各种网站以及 cake docs 之后,我看不出有其他方法可以根据用户组在登录时将 auth 组件重定向到不同的页面。

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

于 2012-12-07T12:38:01.763 回答