You are viewing revision #2 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.
- Create a role manually
- Giving role
- Getting user role
- Making some actions public
- Getting all the roles
implementing some features of yii user and rights.
some tips..
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.
RBAC
Why you don't use RBAC?
Secure
i think user+rights is more secure and easy to implement than RABC, and they are error free built-in modules
Add extra field value in authitem table
I add company field in authitem. now i want to add current logged in company_id in authitem at the time of role creation.
How can i add extra field value in authitem table.?
@Dhara
Please keep these type of identifiers with user table or user related tables company_id
if you need to add to authitem then find its dataprovider/model class
@ Rajith R
Ohk i will do it. but plz can you tell me at the time of data fetching how can i give criteria for data fetching?
In RAssignmentDataProvider following function used for User data fetching. How to apply Condition?
public function __construct($config=array()) { $module = Rights::module(); $userClass = $module->userClass; parent::__construct($userClass, $config); $this->_authorizer = $module->getAuthorizer(); //echo '<pre>'; //print_r($this->_authorizer); }
I want to display users as created by logged in user in Assignment form.
@Dhara
Try with this, I am not sure , add 'type'=>2,'your_attribute'=>$value
$all_roles=new RAuthItemDataProvider('roles', array( 'type'=>2, )); $data=$all_roles->fetchData();
I am not sure about this.
@Dhara
some more tips here
http://www.yiiframework.com/wiki/770/yii-rights-management-tips/
@Rajith R
Thanx... i will try it. And your given link help me for other issues.
Getting the solution
In AssignmentController i have to pass only criteria like following.
public function actionView() { $criteria=new CDbCriteria(); $criteria->compare('created_by',Yii::app()->user->id); // Create a data provider for listing the users $dataProvider = new RAssignmentDataProvider(array('criteria'=>$criteria,'pagination'=>array('pageSize'=>50,),)); // Render the view $this->render('view', array( 'dataProvider'=>$dataProvider, )); }
And it will gives users created by logged in user
@Dhara
You need user list for a certain role?
@Rajith R
No i want all users list created by loggedin user. for that i add created_by field in users table. so that i fetched data from user table created by logged in user. and that users displayed in Assignment form, Not all users. There are multiple admin thats why i have to modify it. :)
@Dhara
Good
Getting roles using:
$authorizer = Yii::app()->getModule("rights")->getAuthorizer(); $authorizer->authManager->assign('clients', $model->id);
worked well for me. This will allow assignmentcore accomplish programming homework for me.
Hi,
Actually I have a question, how can I give role to multiple users?
$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'));
}
Is it right to use array for this?
Program Coordinator at Nursing Assignment Writers UK (Student Agency in UK)
You can use a foreach loop with user ids, use below code
//assign role
$authorizer = Yii::app()->getModule("rights")->getAuthorizer();
$authorizer->authManager->assign('clients', $model->id);`
$model->id is the user id.
Example
foreach($users as $user)
{
$authorizer = Yii::app()->getModule("rights")->getAuthorizer();
$authorizer->authManager->assign('clients', $user); } ``
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.