Search code examples
codeigniterpasswordspassword-encryptioncodeigniter-datamapper

Encrypt password custom rule in DataMapper and Codeigniter


I am using Codeigniter 2.0.3 with DataMapper ORM 1.6.0. Integration DataMapper in CI has been implemented successfully and everything works fine, except password encryption.

I have model called users.php with class Users. I also have set validation rules:

var $validation = array(
    array(
        'field' => 'password',
        'label' => 'Password',
        'rules' => array('required', 'trim', 'unique', 'min_length' => 5, 'encrypt'),
    ),
    array(
        'field' => 'email',
        'label' => 'Email Address',
        'rules' => array('required', 'trim', 'valid_email')
    )
);

My encrypt function in validation rule:

function _encrypt($field)
{
    // Don't encrypt an empty string
    if (!empty($this->{$field}))
    {
        // Generate a random salt if empty
        if (empty($this->salt))
        {
            $this->salt = md5(uniqid(rand(), true));
        }

        $this->{$field} = sha1($this->salt . $this->{$field});
    }
}

MySQL users table structure:

id
email
password
last_login
created_on

I simple create new user via controller:

$u = new Users();
$u->email = '[email protected]';
$u->password = 'mario';
$u->save();

EDIT 1:

Full code of users.php model here.


User successfully stored in the database but with original password not encrypted.

What am I doing wrong?

Thanks, Regards Mario


Solution

  • I solved problem with upgrading DataMapper to 1.8.x version. Now, it works just fine!