Best Way To Encrypt A Password Please?

Hello,

I am new to Yii, I would like to know if there is a quick, easy but yet secure way to encrypt the passwords directly from the Model Public function rules please:




	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('id, name, surname, email, user_name, user_pass, shop_name', 'required'),

			array('id', 'numerical', 'integerOnly'=>true),

			array('name, surname, email, user_name, user_pass, shop_name', 'length', 'max'=>45),

		

			// The following rule is used by search().

			// @todo Please remove those attributes that should not be searched.

			array('id, name, surname, email, user_name, user_pass, shop_name', 'safe', 'on'=>'search'),

		);

	}






Thank you :slight_smile:

Ben

You don’t want to encrypt passwords but hash them instead. Since the last release of Yii, there is CPasswordHelper for that ;)

Usually you would do it not during validation, but after all data is validated and is going to be saved. For that you need to overwrite beforeSave() method. E.g.




protected function beforeSave(){

	if($this->getIsNewRecord()){

		$this->password = CPasswordHelper::hashPassword($this->password);

	}		

	return parent::beforeSave();

}