2012-04-04 197 views
0

我有一個cakephp 2.1和Auth的問題。cakephp 2.1註冊並登錄

在我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 

當用戶首先立即登記$這個 - > Auth->用戶()返回

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

其中password是MD5編碼。如果我註銷並重新登錄,則前一個數組中的密碼將以純文本格式返回,以便User-> find返回false。有沒有辦法爲此做一個單一的功能? 如何知道$ this-> Auth-> user()的密碼是否爲md5?

感謝

回答

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找到它:

居:AuthComponent ::用戶( '用戶名');

+0

由於某種原因AuthComponent :: user('email')&AuthComponent :: user('password')返回null。 – gong 2012-04-05 18:00:09