I would like to display the role as a simple string of each user in a griedview (that use a User model related to the yii RBAC tables).
I would like to display a dropdown list of all roles with the one of the current edited user already selected.
I would like to save the role after the user edition is completed
For now, in my User model, I add a default role after a user creation :
protected function afterSave()
{
// If the record is new, grant 'user' role to the userid
if($this->isNewRecord)
{
Yii::app()->authManager->assign('user',$this->username);
}
parent::afterSave();
}
class User extends CActiveRecord
{
public $roleType;//virtual attribute
public function rules()
{
return array(
array('roleType', 'safe','on'=>'create'),//must declered in rule
....
}
........
public function getRolesAsListData()
{
$roles = Yii::app()->authManager->getRoles();
return CHtml::listData($roles,'name','name');
}
}
ansd in actionCreate(controller/UserController.php)
public function actionCreate()
{
$model=new User;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['User']))
{
$model->attributes=$_POST['User'];
if($model->save())
{
Yii::app()->authManager->assign($model->roleType,$model->id);
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('create',array(
'model'=>$model,
));
}