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.
Can anyone guide what is wrong i am doing here and which encryption we should use MD5 or crypt ?
<?php
if(isset($_POST['User']))
{
//$model->attributes=$_POST['User'];
if(!empty($_POST['User']['password']))
{
$model->password=$_POST['User']['password'];
}
$model->username=$_POST['User']['username'];
$model->firstname=$_POST['User']['firstname'];
$model->lastname=$_POST['User']['lastname'];
$model->email=$_POST['User']['email'];
$model->status=$_POST['User']['status'];
$model->role=$_POST['User']['role'];
if($model->save())
{
Yii::app()->user->setFlash('success', "Record Has Been Updated");
$this->redirect(array('admin'));
}
} ?>
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 ?