I have created a user controller. on create record its wrking fine but while updating record i dont want to enter password every time means password should be update when i change it other wise not. code is given below
Now the issue is i want to encrypt password before save. i also use beforesave() in model to encrypt password but it always encrypt password either password field is empty or not.
public function beforeSave()
{
if (!empty($this->password))
{ $this->password=md5($this->password);}
// return parent::beforeSave();
}
Can anyone guide what is wrong i am doing here and which encryption we should use MD5 or crypt ?
If update a record the password has the stored from database value. (update action)
With your code you set the password from post form when the field is not empty.
But if it is empty the password remains the same (by the database)
Then in your beforesave code you re-encrypt the old password and then save!
To see if it is empty echo the password ob beforesave like that
public function beforeSave()
{
echo($this->password); die();
if (!empty($this->password))
{ $this->password=md5($this->password);}
// return parent::beforeSave();
}
If the value is not empty then I will thing what you can do
public static $getPass=false;
private $sPassword=null;
public function afterFind() {
if ($this->password) {
$this->sPassword = $this->password;
if (self::$getPass==false) $this->password=null;
}
}
parent::afterFind();
}
//in your protected/components/UserIntentity.php modify the authenticate method like this
public function authenticate() {
User::$getPass = true;
..all left code...
User::$getPass = false;
}