Snippets

Sergio de EOM CakePHP 2.x function check old Password to Hash blowfish

Created by Sergio N. last modified
<?php
App::uses('AppModel', 'Model');

/**
 * Users Model
 */
class Users extends AppModel
{
    .........
    
    public function beforeSave($options = array()) {
        parent::beforeSave($options);
        // Save new password is exist..?
        if (isset($this->data[$this->alias]['password'])==true) {
            // Security bcrypt Blowfish
            App::uses('Security', 'Utility');
            $hash = Security::hash(trim($this->data[$this->alias]['password']), 'blowfish');
            $this->data[$this->alias]['password'] = $hash;
        }
        return true;
    }
    
    public function password_check($user_id = null, $password_check = null) {
        // Get password old
        $hash_old = $this->field('password',array('id'=>trim($user_id)));
        // Security bcrypt Blowfish
        App::uses('Security', 'Utility');
        $hash_new_check = Security::hash(trim($password_check), 'blowfish', trim($hash_old));
        // Son iguales
        if($hash_new_check == trim($hash_old)){
            return true;
        }
        return false;
    }

    public function password_update($user_id = null, $password_new = null) {
        // Update new password
        if($this->save(array('id'=>trim($user_id), 'password'=>trim($password_new)))){
            return true;
        }
        return false;
    }
    
    .........
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.