- Create a role manually
- Giving role
- Getting user role
- Finding Users with same role
- Making some actions public
- Getting all the roles
- Adding an item to role
yii rights some tips..
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);
echo key($roles) // for single role
foreach($roles as $role) // for multiple role
{
if($role->name == 'clients')
{
//some actions here ..
}
}
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;
}
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
}
?>
Adding an item to role ¶
Example 1
$this->_authorizer->authManager->addItemChild('role name', 'controllername.actionname');
Example 2
$this->_authorizer->authManager->addItemChild('role name', 'controllername.*');
Thank you Chris for the awesome module. and thanks mishamx.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.