How to get a list of all users assigned to a particular role?

I am using RBAC. For this I am creating an interface to manage all operations/tasks/roles/assignments.

Already created operations, tasks, and roles. Then assigned some roles to some users.

Now I want to show a list of users who have particular role. For example, I would choose “admin” and want to see all users with this role. How can I accomplish this using Yii native API? Currently the only way I see is to implement a model, which doesn’t sound right for me. There should be an API to get this info. I might have missed some documentation.

Any help is highly appreciated. Thank you.

Maybe you can use the methods of the IAuthManager getAuthItems, getAuthAssignments …

Take a look at the sources of the rbac/rights extensions rbam, rights, srbac too.

Seems like all of them have their own controllers and models implemented to solve this problem. I suppose I will dig some more and perhaps start creating a new Controller and Model. Thanks for your response!

in my application, I was following code to search a particular role in action admin

1-creating model from authassignment

2-define ralation function as follow.more information in[url = "http://www.yiiframework.com/doc/guide/1.1/en

/database.arr"]this wiki[/url]




class User extends CActiveRecord

{

....

public function relations()

{


return array(

'userArticles' => array(self::HAS_MANY, 'UserArticle', 'user_id'),

 'roles' => array(self::HAS_ONE, 'Authassignment', 'userid')//create new property roles


);

}



3-define safe for search in admin action




class User extends CActiveRecord

{...

public function rules()

{


return array(

array('roles', 'safe'),

 .....




4-in search fun




class User extends CActiveRecord

{

.....


public function search()

{

// Warning: Please modify the following code to remove attributes that

// should not be searched.


$criteria = new CDbCriteria;


$criteria->compare('t.id', $this->id);

$criteria->compare('t.username', $this->username, true);

$criteria->compare('t.password', $this->password, true);

$criteria->compare('t.tel', $this->tel, true);


//use roles property

$criteria->compare('roles.itemname', $this->roles, true, 'OR');

$criteria->with = array('roles');


return new CActiveDataProvider($this, array(

'criteria' => $criteria,

));

}






5- in view user/admin :





<?php $this->widget('zii.widgets.grid.CGridView', array(

'id' => 'link-grid',

 'dataProvider' => $model->search(),

 'filter' => $model,

 'columns' => array(

array(

'header' => 'role',

 'filter' => CHtml::activeDropDownList($model, 'roles', $model->getRolesAsListData(), array('empty' => '')),

 'name' => 'roles',

 'value' => '$data->getUserRoleName($data->id)'

,

 ),

 array(

'class' => 'CButtonColumn',

 ),

 )

));

?>




6-getRolesAsListData() and getUserRoleName() in user model





class User extends CActiveRecord

{

.....


public function getUserRoleName($id=Null) {

if(is_null($id))

{$id=$this->id;}

if ($AuthassignmentModel=Authassignment::Model()->findByAttributes(array('userid'=>$id))) {

$roleName = $AuthassignmentModel->itemname;

}


return(isset($roleName)) ? ($roleName) : "-----";

}




public function getRolesAsListData()

{

$roles = Yii::app()->authManager->getRoles();

return CHtml::listData($roles,'name','name');    

}



7- and finamy in action admin




/**

* Manages all models.

*/

public function actionAdmin() {


$model = new User('search');


$model->unsetAttributes(); // clear any default values

if (isset($_GET['User']))

$model->attributes = $_GET['User'];


$this->render('admin', array(

'model' => $model,

));

}



hope it can be useful.