CakePHP使用AuthComponent的不同密码

时间:2014-03-07 13:36:32

标签: php cakephp hash xdebug

使用AuthComponent我遇到密码问题。调试中的一个显示的数据与保存在数据库中的数据不同。

  1. 1个密码保存在数据库中:
  2.   

    d074dc36936aeb8fdc709112969425f71eedc694

    1. 2.password在Controller

      中出现
        

      aa979656c76b9974130ad2698a221d4dd93ca4ca

    2.      用户模型

      <?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();
      ?>
      

1 个答案:

答案 0 :(得分: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