RBAC & Role edition

Hello,

I’m kind of lost with role edition.

  • 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();

    }



Thanks,

Maxime.

up :)

No one has done role edition ? :(

getRoles() - this method should give you all the information for the first and the second task.

For the third task you have already used in your code assign() method, so you should know it.

Hello and thank you for your reply.

What I don’t understand is how I can display the roles in a dropdown list :




    $roles = Yii::app()->authManager->getRoles();

    $ld=CHtml::listData($roles,'type','name');

    echo CHtml::dropDownList('test,'',$ld); ?>



This only return me one role in the dropdown (I have 3 roles in the database). But the sql request return me 3 roles.

Thanks.

You have the same type for all your roles: CAuthItem::TYPE_ROLE, that’s why array’s key is overwritten all the time, try to use something like


CHtml::listData($roles,'name','name');

instead

Whooa, your are so right ! Sometimes, I am totally blind :blink:

Last question. How should I handle the role translations ? The roles are stored in Yii tables. And I don’t know how to translate them.

Thanks a lot !

Welcome :)

There’re may be different approaches for the roles translation, I prefer to use Yii message translation, for example:


Yii::t('text', $role); // $role = 'user'

in message files


'user'=>'Пользователь'

Hello,

Ok, here what I did




$user_roles = CHtml::listData(Yii::app()->authManager->getRoles(), 'name', 'name');

foreach ($user_roles as $value=>$text)

{

    $user_roles[$value]=Yii::t('user', 'controller.user.role.').$text;

}



If you have a better solution :)

Yeah, in this case I’d use something like this, maybe wrapped into a separate method.

Similar situation for AR model I’d solve using virtual attribute.

yes I have created a getRolesAsListData() method in my User model :)

as follow:

in user class(models/User.php)





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');    

       

   }

}




in _form.php





   <div class="row">

		<?php echo $form->labelEx($model,'roleType'); ?>

                <?php echo $form->dropDownList( $model,'roleType', $model->getRolesAsListData(),array('empty' => '')); ?>

		<?php echo $form->error($model,'roleType'); ?>

	</div>




    



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,

		));

	}



hope this usefull.