使用cakephp登录重定向

时间:2012-07-31 02:36:51

标签: cakephp login

我目前正在使用Usermgmt插件进行登录功能和用户管理。我想要做的是在登录后根据group_id重定向特定页面。我现在迷失了蛋糕。

这是来自AppController的代码。

    var $helpers = array('Form', 'Html', 'Session', 'Js', 'Usermgmt.UserAuth');
    public $components = array('Session','RequestHandler', 'Usermgmt.UserAuth');
    function beforeFilter(){
        $this->userAuth();
    }

    private function userAuth(){
        $this->UserAuth->beforeFilter($this);
    }

这是UsersController的登录功能。

  public function login() {
    if ($this->request -> isPost()) {
        $this->User->set($this->data);
        if($this->User->LoginValidate()) {
            $email  = $this->data['User']['email'];
            $password = $this->data['User']['password'];

            $user = $this->User->findByUsername($email);
            if (empty($user)) {
                $user = $this->User->findByEmail($email);
                if (empty($user)) {
                    $this->Session->setFlash(__('Incorrect Email/Username or Password'));
                    return;
                }
            }
            // check for inactive account
            if ($user['User']['id'] != 1 and $user['User']['active']==0) {
                $this->Session->setFlash(__('Your registration has not been confirmed please verify your email or contact to Administrator'));
                return;
            }
            $hashed = md5($password);
            if ($user['User']['password'] === $hashed) {
                $this->UserAuth->login($user);
                $remember = (!empty($this->data['User']['remember']));
                if ($remember) {
                    $this->UserAuth->persist('2 weeks');
                }
                $OriginAfterLogin=$this->Session->read('Usermgmt.OriginAfterLogin');
                $this->Session->delete('Usermgmt.OriginAfterLogin');
                $redirect = (!empty($OriginAfterLogin)) ? $OriginAfterLogin : loginRedirectUrl;
                $this->redirect($redirect);
            } else {
                $this->Session->setFlash(__('Incorrect Email/Username or Password'));
                return;
            }
        }
    }
}

感谢任何帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

如果要将它们重定向到其他位置,请更改重定向行。这假设您的用户模型与组模型相关,并且递归级别允许您的查找调用拉取数据。

// original
$redirect = (!empty($OriginAfterLogin)) ? $OriginAfterLogin : loginRedirectUrl;
// new redirect, eg: /groups/view/3
$redirect = array(
  'controller' => 'groups', 
  'action' => 'view', 
  $user['Group']['id']
);
$this->redirect($redirect);