Question

I have a User Model, a UsersController and an AccountController which uses the User Model (the account controller is used when an account is created, login, logout).

Everything works fine, except the beforeSave function in AccountController. I'm trying to use beforeSave to hash my password but it doesn't work (the password is saved un-hashed in the database).

public function beforeSave() {
    parent::beforeSave();   

    if (isset($this->request->data['User']['password'])) {
        $this->request->data['User']['password'] = sha1($this->request->data['User']['password']);
    }

    return true;
}

A few notes:

  • I use "sha1" to hash the password because I'm converting an old system to CakePHP (the old system used sha1 to hash the passwords)
  • Even if I return false; the save function is still executed (which I thought should not be the case
  • calling debug($this->request->data) gives me nothing
  • I added the same beforeSave function in my UsersController but it didn't work as well

I think in my case beforeSave is not being called, I just can't figure out why.


Solved: The beforeSave function has to go inside the model, this is my beforeSave function now:

public function beforeSave($options = array()) {
        parent::beforeSave();
        $this->data['User']['password'] = sha1($this->data['User']['password']);
        return true;
    }
Was it helpful?

Solution

beforeSave is a Model callback, so define it in your Model(s).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top