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?

谢谢

4

1 回答 1

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

于 2012-04-05T05:19:14.827 回答