2014-03-07 41 views
0

使用AuthComponent我有密碼問題。調試中的一個顯示與保存在數據庫中不同。CakePHP使用AuthComponent的不同密碼

  1. 1密碼保存在數據庫:

d074dc36936aeb8fdc709112969425f71eedc694

  1. 2.password在控制器

    debuged

    aa979656c76b9974130ad2698a22 1d4dd93ca4ca

用戶模型

<?php 
App::uses('AuthComponent', 'Controller/Component'); 
class User extends AppModel { 

public $validate = array(
    'name' => array(
     'rule' => array('between', 3, 32), 
     'required' => true, 
     'allowEmpty' => false, 
     'message' => 'Podaj poprawne imię!' 
    ), 
    'last_name' => array(
     'rule' => array('between', 3, 32), 
     'required' => true, 
     'allowEmpty' => false, 
     'message' => 'Podaj poprawne nazwisko!' 
    ), 
    'password' => array(
     'rule' => array('minLength', 6), 
     'required' => true, 
     'message' => 'Hasło powinno mieć minimum 6 znaków!' 
    ), 
    're_password' => array(
     'rule' => 'equalToPassword', 
     'required' => true, 
     'on' => 'create', 
     'message' => 'Hasła nie są identyczne!' 
    ) 
); 

public function equalToPassword() { 
    if (isset($this->data[$this->alias]['password']) && isset($this->data[$this->alias]['re_password'])) { 
     return $this->data[$this->alias]['password'] == $this->data[$this->alias]['re_password']; 
    } 
} 

public function beforeSave($options = array()) { 
    if (isset($this->data[$this->alias]['password'])) { 
    $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']); 
} 
return true; 
} 

} 

UsersController

<?php 

class UsersController extends AppController { 

public $helpers = array('Html', 'Form'); 

public function beforeFilter() { 
    parent::beforeFilter(); 
    $this->Auth->allow('admin_login', 'admin_logout','admin_add'); 
} 
public function login() 
{ 
    $this->redirect(array('controller'=>'users','action'=>'admin_login')); 
} 
public function admin_login() { 
    if ($this->request->is('post')) { 
     debug(AuthComponent::password($this->data[$this->alias]['password'])); 
     //$this->request->data['User']['password'] = AuthComponent::password($this->request->data['User']['password']); 
     if ($this->Auth->login()) { 
      return $this->redirect($this->Auth->redirect()); 
     } 
     $this->Session->setFlash(__('Invalid username or password, try again')); 
    } 
} 

public function admin_logout() { 
    return $this->redirect($this->Auth->logout()); 
} 

public function admin_add() { 
    if ($this->request->is('post')) { 
     $this->User->create(); 
     //$this->request->data['User']['craeted'] ; 
     if ($this->User->save($this->request->data)) { 
      $this->Session->setFlash(__('Users has been created!')); 
      return $this->redirect(array('controller' => 'users', 'action' => 'admin_index')); 
     } 
     $this->Session->setFlash(__('User can not be save!')); 
    } 
} 

add.ctp

<?php 
    echo $this->Form->create('User'); 
    echo $this->Form->input('name',array('label'=>'imię/login','class'=>'form-control')); 
    echo $this->Form->input('last_name',array('label'=>'nazwisko','class'=>'form-control')); 
    echo $this->Form->input('password',array('label'=>'hasło','class'=>'form-control')); 
    echo $this->Form->input('re_password',array('label'=>'powtórz hasło','class'=>'form-control')); 
    echo $this->Form->input('role',array('options'=>array('admin'=>'Admin','user'=>'Bez praw'))); 
    echo $this->Form->submit('Zapisz',array('class'=>'btn btn-info')); 
    echo $this->Form->end(); 
?> 

login.ctp

<?php 
    echo $this->Form->create('User'); 
    echo $this->Form->input('name',array('label'=>'imię/login','class'=>'form-control')); 
    echo $this->Form->input('password',array('label'=>'hasło','class'=>'form-control')); 
    echo $this->Form->submit('Login',array('class'=>'btn btn-info')); 
    echo $this->Form->end(); 
?> 

回答

0

你不必散列密碼蛋糕會爲你

所以當你

$this->request->data['User']['password'] = AuthComponent::password($this->request->data['User']['password']) 

你哈希將被蛋糕保存或自動散列密碼當檢查用戶登錄時。

另外:AuthComponent::password被廢棄了,因爲2.4

看到http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#hashing-passwords

+0

如您對本教程說我改變了這種AuthComponent http://book.cakephp.org/2.0/en/tutorials-and-examples/ blog-auth-example/auth.html btu我的密碼仍然不同,我無法登錄。我從昨天起開始處理int,它非常令人沮喪... –

+0

如果您在保存時散列了密碼,則密碼已被散列兩次。我建議再次保存密碼或更改它們,如果你沒有任何其他需要,可以讓它們爲你做蛋糕散列。 – arilia

+0

我清除了我的CakePHP緩存,它終於可以在不改變任何東西的情況下工作 –

相關問題