Hi,
Below is content of controller for this action :
public function actionCreate()
{
$model=new User('create');
$this->performAjaxValidation($model);
if(isset($_POST['User']))
{
// On récupère les attributs et on met une date de création
$model->attributes=$_POST['User'];
$model->creationDate = date('Y-m-d H:i:s');
// Si l'utilisateur n'est pas SA, le countryid de l'utilisateur est forcément celle du créateur
if (Yii::app()->user->model->role != 'SA')
$model->countryid = Yii::app()->user->model->countryid;
if ($model->validate())
{
// Si tout est correct, on met le mot de passe en md5 dans un premier temps
$password = $model->password;
$model->password = md5($model->password);
if($model->save(false))
{
// On sauvegarde le model, et on créé un has sha256 basé sur l'id de l'utilisateur (c'est pour ca qu'on a que du md5 avant, on avait pas encore l'id de l'utilisateur)
$model->password = hash('sha256', hash('sha256', hash('sha256', ($password.$model->id.preg_replace('#[^a-zA-Z0-9]+#','',$model->creationDate)))));
$model->save(false);
$this->redirect(array('index'));
}
}
}
$this->render('create',array(
'model'=>$model,
));
}
and rules :
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('username, password, email, countryid, creationDate, role', 'required'),
// CountryId must be a correct Country
array('countryid', 'numerical', 'integerOnly'=>true),
array('"countryid"','exists', 'attributeName'=>'"id"', 'className'=>'Country'),
// The username must be unique and between 4 and 50 caracters
array('username', 'length', 'max'=>50, 'min'=>4),
array('username','unique'),
// Password and password confirm must match.
array('password', 'length', 'max'=>64, 'min'=><img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' />,
array('passwordConfirm', 'required', 'on'=>'create'),
array('passwordConfirm', 'compare', 'compareAttribute'=>'password', 'on'=>'create'),
// Those fields are used for setting a new password
array('newPasswordConfirm', 'compare', 'compareAttribute'=>'newPassword', 'on'=>'update'),
array('newPassword', 'length', 'max'=>64, 'min'=>8, 'on'=>'update', 'allowEmpty'=>true),
// Roles : Super Administrator, Administrator for Country, Country Sales
array('role', 'length', 'max'=>2),
array('role', 'in', 'range'=>array('SA','A','C')),
// Use function checkRoleAuthorization to check Role
array('role', 'checkRoleAuthorization'),
// Email
array('email', 'length', 'max'=>150),
array('email','email'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, username, role, email, countryid', 'safe', 'on'=>'search'),
);
}