title: Cakephp hashing password while changing it
body: <p>I try to make hashing while changing password correctly , but i cant, always after changing password, it's hashing twice or i don;t know. Please tell me what am i doing wrong.</p>
  
  <pre><code>class User extends AppModel
  </code></pre>
  
  <p>{</p>
  
  <pre><code>var $validate = array(
      'name' =&gt; array(
          'length' =&gt; array(
              'rule'      =&gt; array('minLength', 5),
              'message'   =&gt; 'Please enter your full name (more than 5 chars)',
              'required'  =&gt; true,
          ),
      ),
      'username' =&gt; array(
          'length' =&gt; array(
              'rule'      =&gt; array('minLength', 5),
              'message'   =&gt; 'Must be more than 5 characters',
              'required'  =&gt; true,
          ),
          'alphanum' =&gt; array(
              'rule'      =&gt; 'alphanumeric',
              'message'   =&gt; 'May only contain letters and numbers',
          ),
          'unique' =&gt; array(
              'rule'      =&gt; 'isUnique',
              'message'   =&gt; 'Already taken',
          ),
      ),
      'email' =&gt; array(
          'email' =&gt; array(
              'rule'      =&gt; 'email',
              'message'   =&gt; 'Must be a valid email address',
          ),
          'unique' =&gt; array(
              'rule'      =&gt; 'isUnique',
              'message'   =&gt; 'Already taken',
          ),
      ),
      'password' =&gt; array(
          'empty' =&gt; array(
              'rule'      =&gt; 'notEmpty',
              'message'   =&gt; 'Must not be blank',
              'required'  =&gt; true,
          ),
      ),        
      'password_confirm' =&gt; array(
          'required'    =&gt; array(
              'rule'      =&gt; array('equalToField', 'password', true),
              'message'   =&gt; 'The password you entered does not match',
  
          ),
          'length' =&gt; array(
              'rule'      =&gt; array('between', 6, 20),
              'message'   =&gt; 'Use between 6 and 20 characters',
          ),
          'empty' =&gt; array(
              'rule'      =&gt; 'notEmpty',
              'message'   =&gt; 'Must not be blank',
          ),
      ),
  );
  
  function equalToField($array, $field) {
          return strcmp($this-&gt;data[$this-&gt;alias][key($array)], $this-&gt;data[$this-&gt;alias][$field]) == 0;
  }
  
  
  var $validateChangePassword = array(
      '_import' =&gt; array('password', 'password_confirm'),
      'password_old' =&gt; array(
          'correct' =&gt; array(
              'rule'      =&gt; 'password_old',
              'message'   =&gt; 'Does not match',
              'required'  =&gt; true,
          ),
          'empty' =&gt; array(
              'rule'      =&gt; 'notEmpty',
              'message'   =&gt; 'Must not be blank',
          ),
      ),
  );
  
  
  function useValidationRules($key)
  {
      $variable = 'validate' . $key;
      $rules = $this-&gt;$variable;
  
      if (isset($rules['_import'])) {
          foreach ($rules['_import'] as $key) {
              $rules[$key] = $this-&gt;validate[$key];
          }
          unset($rules['_import']);
      }
  
      $this-&gt;validate = $rules;
  }
  
  
  function password_old($data)
  {
      $password = $this-&gt;field('password',
          array('User.id' =&gt; $this-&gt;id));
      return $password ===
          Security::hash($data['password_old'], null, true);
  }
  
   public function beforeSave($options = array()) {
      if (isset($this-&gt;data[$this-&gt;alias]['password'])) {
          $this-&gt;data[$this-&gt;alias]['password'] = AuthComponent::password($this-&gt;data[$this-   &gt;alias]['password']);
      }
      return true;
  }
  </code></pre>
  
  <p>And then my controller:</p>
  
  <pre><code>class UsersController extends AppController
  </code></pre>
  
  <p>{</p>
  
  <pre><code> var $components = array('Email');
  
  
    /**
   * Account details page (change password)
   */
  function account()
  {
      // Set User's ID in model which is needed for validation
      $this-&gt;User-&gt;id = $this-&gt;Auth-&gt;user('id');
  
      // Load the user (avoid populating $this-&gt;data)
      $current_user = $this-&gt;User-&gt;findById($this-&gt;User-&gt;id);
      $this-&gt;set('current_user', $current_user);
  
      $this-&gt;User-&gt;useValidationRules('ChangePassword');
      $this-&gt;User-&gt;validate['password_confirm']['compare']['rule'] =
          array('equalToField', 'password', false);
  
      $this-&gt;User-&gt;set($this-&gt;data);
      if (!empty($this-&gt;data) &amp;&amp; $this-&gt;User-&gt;validates()) {
          $password = $this-&gt;Auth-&gt;password($this-&gt;data['User']['password']);
          $this-&gt;User-&gt;saveField('password', $password);
  
          $this-&gt;Session-&gt;setFlash('Your password has been updated');
          $this-&gt;redirect(array('action' =&gt; 'account'));
      }        
  }
  
  
  
  /**
   * Registration page for new users
   */
  // function register()
  // {
      // if (!empty($this-&gt;data)) {
          // $this-&gt;User-&gt;create();
          // if ($this-&gt;User-&gt;save($this-&gt;data)) {
              // $this-&gt;Session-&gt;setFlash(__('Your account has been created.', true));
              // $this-&gt;redirect('/');
          // } else {
              // $this-&gt;Session-&gt;setFlash(__('Your account could not be created.', true));
          // }
      // }
  // }
  
  public function register(){
  
      if($this-&gt;request-&gt;is('post')){
          $this-&gt;User-&gt;create();
          if($this-&gt;User-&gt;save($this-&gt;request-&gt;data)){
              $this-&gt;Session-&gt;setFlash(__('Użytkownik został zapisany', 'success'));
              $this-&gt;redirect(array('controller'=&gt;'ads', 'action'=&gt;'index'));
          } else {
              $this-&gt;Session-&gt;setFlash(__('Błąd zapisu'), 'error');
          }
      }
  
  }
  
  /**
   * Log a user out
   */
  function logout()
  {
     return $this-&gt;redirect($this-&gt;Auth-&gt;logout());
  }
  
      /**
   * Ran directly after the Auth component has executed
   */
  function login()
  {
      // Check for a successful login
      if($this-&gt;request-&gt;is('post')){
  
  
  
          if($this-&gt;Auth-&gt;login()){
              $this-&gt;User-&gt;id = $this-&gt;Auth-&gt;user('id'); // zapisuje date logowania
              $this-&gt;User-&gt;saveField('lastlogin', date(DATE_ATOM)); // zapisuje date logowania
  
              $this-&gt;redirect($this-&gt;Auth-&gt;redirect());
          } else {
              $this-&gt;Session-&gt;setFlash(__('Nieprawidłowy login lub hasło'), 'error');
          }
      }
  }
  </code></pre>
  
  <p>and the view:</p>
  
  <pre><code>echo $this-&gt;Form-&gt;create(array('action' =&gt; 'account'));
  echo $this-&gt;Form-&gt;input('password_old',     array('label' =&gt; 'Old password', 'type' =&gt; 'password', 'autocomplete' =&gt; 'off'));
  echo $this-&gt;Form-&gt;input('password_confirm', array('label' =&gt; 'New password', 'type' =&gt; 'password', 'autocomplete' =&gt; 'off'));
  echo $this-&gt;Form-&gt;input('password',         array('label' =&gt; 'Re-enter new password', 'type' =&gt; 'password', 'autocomplete' =&gt; 'off'));
  echo $this-&gt;Form-&gt;end('Update Password');
  </code></pre>