Yii Framework Forum: How to get a list of all users assigned to a particular role? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to get a list of all users assigned to a particular role? Rate Topic: ***** 1 Votes

#1 User is offline   kadishmal 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 44
  • Joined: 09-February 11
  • Location:South Korea

Posted 25 December 2011 - 12:27 AM

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.
0

#2 User is offline   Joblo 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 570
  • Joined: 12-September 10
  • Location:Austria

Posted 27 December 2011 - 04:44 AM

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.
0

#3 User is offline   kadishmal 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 44
  • Joined: 09-February 11
  • Location:South Korea

Posted 30 December 2011 - 07:36 AM

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!
0

#4 User is offline   za_al 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 53
  • Joined: 06-March 12

Posted 03 March 2013 - 05:10 AM

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.
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users