- Create a role manually
- Giving role
- Getting user role
- Making some actions public
- Getting all the roles
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 ..
$this->redirect(array('/mailbox'));
}
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
'import'=>array(
'application.modules.rights.components.dataproviders.*',
),
get all roles as a dropdown
<?php
if (Yii::app()->user->isSuperuser) {
$all_roles=new RAuthItemDataProvider('roles', array(
'type'=>2,
));
$data=$all_roles->fetchData();
?>
<div>
<label for="type_id">Type</label>
<?php echo CHtml::dropDownList("Type",'',CHtml::listData($data,'name','name'));?>
</div>
<?php
}
?>
Thank you Chris for the awesome module. and thanks mishamx.
Behaviors
To show user roles you can also use behaviors.
In user`s model add code:
public function behaviors() { Yii::import('application.modules.rights.components.behaviors.*'); return CMap::mergeArray(array( 'RUserBehavior'=>array( 'class'=>'RUserBehavior', 'idColumn'=>'id', 'nameColumn'=>'name', ), ), parent::behaviors()); }
Now you can simply use:
echo $model->getAssignmentsText(CAuthItem::TYPE_ROLE);
good tip
thats good one qwerty
Dropdown where?
Hi!
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.
@ 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!!
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; }
how to assign permissions in yii rights module
how to assign permissions in yii rights module automatically when a user register?
@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.
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?
@Shahzad Thathal
As i said in the previous reply, You can use any authitem . by replacing 'clients' !!
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
@Fire
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);
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?
@hunter_005
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
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?
@hunter_005
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.
@hunter_005
Add in main config file.
'components'=>array( 'user'=>array( 'class'=>'RWebUser', // enable cookie-based authentication 'allowAutoLogin'=>true, ),
change column name
Hi,
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?
@hunter_005
I think you just rename your field.
or If you are using your user table then develop your own user module, thats better!!
how to do it manually?
hi
i did every thing about yii user and rights till this code
//assign role $authorizer = Yii::app()->getModule("rights")->getAuthorizer(); $authorizer->authManager->assign('clients', $model->id);
but actually i dont know where and how to do this
where to put the above code
thanks if there is a complete guide link
@desatir7316
Hi desatir7316,
After user creation you can use this .
or
its better to use at the time of user confirms from email
Add a role to the user.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.