cakephp 2.1注册和登录

时间:2012-04-04 18:54:52

标签: cakephp-2.0

我遇到了cakephp 2.1和Auth。

的问题

在我的AppControlles中,我有一个函数getUserdetails()

if (($user = $this->Auth->user()) != null)
    {
      $this->loadModel('User');
      $tmp = $this->User->find('first',array(
          'conditions' => array('username' => $user['User']['username'], 
                       'password'=> $user['User']'password'],
                'active' => 1),
          'recursive' => -1));

      if(!isset($tmp['User']))
        return null;

      $this->_userDetails = $tmp['User'];
      $this->set('userDetails', $this->_userDetails);
    }
    else
      return null

当用户首次注册$ this-> Auth-> user()返回时

array(
    'User' => array(
        'password' => '*****',
        'username' => 'me',
        'remember_me' => '1'
    )
)

其中密码是md5编码的。如果我注销并再次登录,则前一个数组中的密码以纯文本形式返回,因此User-> find返回false。有没有办法为此制作单一功能? 如何知道来自$ this-> Auth-> user()的密码是否为md5?

感谢

1 个答案:

答案 0 :(得分:0)

试试这个:

public function login() {
    //If a user is already logged in, redirect him
    if ($this->Session->read('Auth.User')) {
        //$this->Session->setFlash('You are logged in!');
        $this->redirect(array('controller' => 'showspage', 'action' => 'home'));
    }

    if ($this->request->is('post')) {
        $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);

        if ($this->Auth->login()) {
            $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash('Wrong username or password');
            $this->redirect(array('controller' => 'showspage', 'action' => 'home'));
        }
    }
    $this->Session->setFlash('You aren't legged-in!');
    $this->redirect(array('controller' => 'showspage', 'action' => 'home'));
}

有关用户的所有信息都在会话中,您可以使用AuthComponent找到它:

Es:AuthComponent :: user('username');