Yii 1.1: Assigning dynamic roles to a user Using yii Rights module at the time of user creation and using some special advanced features of yii rights


After Installing Yii Users and Rights to Newly Created Yii app we have to assign dynamic roles to a user at the time of user creation .

Create a role manually

first we have to create a role manually and add some operations and/or tasks to the role according to the needs

assume the role created was 'clients'

Giving role

This is very simple , achieved by using two lines of code

save the user(assume $model->id is the user saved/created)

//assign role
$authorizer = Yii::app()->getModule("rights")->getAuthorizer();
$authorizer->authManager->assign('clients', $model->id);

Getting user role

This is also very simple

the signed user id will get using Yii::app()->user->Id

$roles=Rights::getAssignedRoles(Yii::app()->user->Id); // check for single role
foreach($roles as $role)
if($role->name == 'clients')
//some actions here ..

Making some actions public

this should be a common need of an app.

this is also very simple when using yii rights

just use a '-' (minus) operator like

public function filters()
        return array(
            'rights - publicprofile', // perform access control for CRUD operations

here the publicprofile action is public, all other actions in the same controller are under rights.

Getting all the roles

getting all the roles in the application

put this line in protected/config/main


get all roles as a dropdown

if (Yii::app()->user->isSuperuser) {
       $all_roles=new RAuthItemDataProvider('roles', array( 
        <label for="type_id">Type</label>
        <?php echo CHtml::dropDownList("Type",'',CHtml::listData($data,'name','name'));?> 

Thank you Chris for the awesome module. and thanks mishamx.

Total 18 comments

#16538 report it
Rajith R at 2014/03/04 12:57am

I think you just rename your field.

or If you are using your user table then develop your own user module, thats better!!

#16534 report it
hunter_005 at 2014/03/03 05:58am
change column name


I am trying to integrate my site's user table with rights and user module. My user table has got a 'is_admin' field which is equivalent to 'superuser' field in the user table mentioned in the tutorial for installing rights and user module. Because of this name mismatch, I get a 403 error(There must be atleast one superuser!) while installing rights. How can I solve this?. Can I somehow let the rights module know that 'is_admin' and 'superuser' fields are the same?

#16500 report it
Rajith R at 2014/02/28 07:00am

Add in main config file.

            // enable cookie-based authentication
#16497 report it
Rajith R at 2014/02/28 04:29am

Yes you can .

so you just download the rights module , there is an option to set your own user table and user model .

edit these lines

'userIdColumn'=>'id', // Name of the user id column in the database. 
'userNameColumn'=>'username',  // Name of the user name column in the database.
#16496 report it
hunter_005 at 2014/02/28 04:19am
Module with the same name

HI Rajith,

Thanks for the quick reply. But there is an another problem. In my protected/modules folder, I already got a module/folder named "user". When we unzip the yii-user module, its folder name is also "user". I changed the unzipped folder name to "yii-user". This is causing a series of errors during the installation of rights module. What should I do to prevent this conflict?. Also, I already have a "user" table. But its columns are different from the one we create using schema.mysql.sql. Can I integrate my existing "user" table with rights and user modules?

#16494 report it
Rajith R at 2014/02/28 02:59am

Yes you can add new roles manually and can assign tasks&operations to that role.

After installation goto rights module , There are links to Assignments, Permissions, Roles, Tasks, Operations .

Goto Permissions > Generate items for controller actions

then goto Roles > Create a new role

#16492 report it
hunter_005 at 2014/02/28 02:46am
Create Role Manually

Hi Rajith,

Thanks for the tutorial. I am a newbie to yii. So this may appear a dumb question to you. I want to know how we can create a new role manually. Also is it possible to add privileges for a created role?

#15596 report it
Rajith R at 2013/11/27 04:44am

Create a role Registered manually

then use this code dynamically at the time of user creation!!

$authorizer = Yii::app()->getModule("rights")->getAuthorizer();
$authorizer->authManager->assign('Registered', $model->id);
#15595 report it
Fire at 2013/11/27 04:35am
How do you automatically assign a role when new users join your site?

Hi there, I would like to assign the role "Registered" to each new user that successfully registers to the sites.

I am using yii-rights and yii-user

#15022 report it
Rajith R at 2013/09/30 01:32am
@Shahzad Thathal

As i said in the previous reply, You can use any authitem . by replacing 'clients' !!

#15021 report it
Shahzad Thathal at 2013/09/30 01:09am
Role assigned successfully but problem with permissions

My issue is when I create a user,I am assigning a Role 'student' but now I need to automatically assigned authitem to this user,How it is possible?

#15019 report it
Rajith R at 2013/09/30 12:54am
@Shahzad Thathal

assign role $authorizer = Yii::app()->getModule("rights")->getAuthorizer(); $authorizer->authManager->assign('clients', $model->id);

From the above example 'clients' is a role name.

So create roles according to your preference and assign dynamically at the time of user creation .'$model->id' is the user id. you can also assign any authitem like this other than role.

#15014 report it
Shahzad Thathal at 2013/09/29 04:29pm
how to assign permissions in yii rights module

how to assign permissions in yii rights module automatically when a user register?

#14957 report it
Rajith R at 2013/09/24 07:59am
Finding Users with same role
$role = 'role-name' //role name here
$users = AuthAssignment::model()->findAllByAttributes(array('itemname'=>$role));
foreach($users as $user)
 echo $user->userid;
#14330 report it
Rajith R at 2013/08/05 02:00am
@ MissChile

hi MissChile,

If u need to assign a role to a user, u can put that dropdown there and assign using the another code

$authorizer = Yii::app()->getModule("rights")->getAuthorizer();
$authorizer->authManager->assign('clients', $model->id);

replace the 'clients' with the posted dropdown role!!

#14319 report it
MissChile at 2013/08/04 01:42pm
Dropdown where?


Thanks for this tutorial. When you say "get all roles as a dropdown", where exactly do I have to put that code? In a new file? Thanks in advance!

PS: I'm new. Sorry if it's a stupid question.

#13990 report it
Rajith R at 2013/07/11 12:44am
good tip

thats good one qwerty

#13987 report it
qwerty at 2013/07/10 06:44pm

To show user roles you can also use behaviors. In user`s model add code:

public function behaviors()
    return CMap::mergeArray(array(
    ),  parent::behaviors());

Now you can simply use:

echo $model->getAssignmentsText(CAuthItem::TYPE_ROLE);

Leave a comment

Please to leave your comment.

Write new article