Yii Framework Forum: Only One Assigned Role To User - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Only One Assigned Role To User based on setting role by admin Rate Topic: -----

#1 User is offline   Kostas Apazidis (KonApaz) 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,335
  • Joined: 21-February 11
  • Location:Greece

Posted 21 August 2013 - 09:36 AM

Hi

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:
http://www.yiiframew...__1#entry217992

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
http://www.yiiframew...a-php-file/#hh4
using revoke method

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

Thanks
Yii is the best php framework in the world!
It is also powerful and flexible for large scale websites
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#2 User is offline   rahif 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 179
  • Joined: 11-July 13

Posted 22 August 2013 - 12:34 AM

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

  //$id : User id
  $auth=Yii::app()->authManager;					
  $auth->revoke($old_rol,$id);
  $auth->assign($new_rol,$id);


Authentication is very delicate, luck
0

#3 User is offline   Kostas Apazidis (KonApaz) 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,335
  • Joined: 21-February 11
  • Location:Greece

Posted 22 August 2013 - 02:36 AM

View Postrahif, on 22 August 2013 - 12:34 AM, said:

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

  //$id : User id
  $auth=Yii::app()->authManager;					
  $auth->revoke($old_rol,$id);
  $auth->assign($new_rol,$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 :)
Yii is the best php framework in the world!
It is also powerful and flexible for large scale websites
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users