Hi
I’m building an app with an authentication based on the Blog Demo of Yii.
The DB table "users" have fields id, username, password and salt.
I want to permit to admin to manage this table. So, with Gii, I generate CRUD for this table.
Naturally, on "create" or "update", the CRUD does not apply MD5 to password nor generate the salt key.
I can’t find out where I change the data to be sended to DB. I thought that was on the model but I don’t see where I can do that:
<?php
class Users extends CActiveRecord
{
/**
* The followings are the available columns in table 'tbl_user':
* @var integer $id
* @var string $username
* @var string $password
* @var string $salt
*/
/**
* Returns the static model of the specified AR class.
* @return CActiveRecord the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'users';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('username, password', 'required'),
array('username, password', 'length', 'max'=>128),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'Id',
'username' => 'Username',
'password' => 'Password',
'salt' => 'Salt',
);
}
/**
* Checks if the given password is correct.
* @param string the password to be validated
* @return boolean whether the password is valid
*/
public function validatePassword($password)
{
return $this->hashPassword($password,$this->salt)===$this->password;
}
/**
* Generates the password hash.
* @param string password
* @param string salt
* @return string hash
*/
public function hashPassword($password,$salt)
{
return md5($salt.$password);
}
/**
* Generates a salt that can be used to generate a password hash.
* @return string the salt
*/
protected function generateSalt()
{
return uniqid('',true);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('userid',$this->userid,true);
$criteria->compare('username',$this->username,true);
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
));
}
}
I believe that this is a rookie thing, but if someone can help me, I will apreciate.
Thanks