Only One Assigned Role To User


Although I have enough experience in Yii (:D lol) I have already forgotten a lot of things of RBAC system

So, I develop an admin panel system to set roles to the users

my previous post about roles is:

Now I am asking you to tell me what is the best way to set only one role for each user

for example in the first time assign the user

Yii::app()->authManager->assign('member', user_id);

now I want modify the role like that

Yii::app()->authManager->assign('author', user_id);

but in this way the user will get two roles! I want to remove the first one before add the second one

How to do that without sql query ?

maybe the solution is in this wiki

using revoke method

if someone has more experience with RBAC or has any suggestion please let posts :)



First, in update user: revoke the old rol and assign the new rol.

  //$id : User id




Authentication is very delicate, luck

Hi rahif

As I posted in the first time the solution found in the wiki.

I quote a part of this wiki

 $assigned_roles = Yii::app()->authManager->getRoles(Yii::app()->user->id); //obtains all assigned roles for this user id

    if(!empty($assigned_roles)) //checks that there are assigned roles


        $auth=Yii::app()->authManager; //initializes the authManager

        foreach($assigned_roles as $n=>$role)


            if($auth->revoke($n,Yii::app()->user->id)) //remove each assigned role for this user

                Yii::app()->authManager->save(); //again always save the result



I am seeking for a simple solution in one line like that

$assigned_roles = Yii::app()->authManager->somethingToClearRoles(Yii::app()->user->id)

In any case thanks for your response :)