Skip to content

Commit

Permalink
Filter users by role when possible (#6)
Browse files Browse the repository at this point in the history
* add ability to filter by permission in case yii2-rbac module is used

* return empty array when there is no authManager

* allow to use anything that implements ManagerInterface; show filter in grid when it is DbManager

* code style
  • Loading branch information
thyseus authored and SamMousa committed Jun 5, 2017
1 parent 75ebc03 commit ffe298b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
1 change: 1 addition & 0 deletions messages/de/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
'An error occurred processing your request' => 'Ein Fehler ist aufgetreten',
'Assignments' => 'Zuordnungen',
'Are you sure you want to switch to this user for the rest of this Session?' => 'Sind Sie sicher, dass sie für den Rest der Sitzung zu diesem Benutzer wechseln möchten?',
'Auth item' => 'Berechtigung',
'Awesome, almost there. Now you need to click the confirmation link sent to your new email address' => 'Fast geschafft. Nun müssen Sie nur noch den Aktivierungslink besuchen, der an ihre neue E-Mail Adresse gesendet wurde',
'Awesome, almost there. Now you need to click the confirmation link sent to your old email address' => 'Fast geschafft. Nun müssen Sie nur noch den Aktivierungslink besuchen, der an ihre alte E-Mail Adresse gesendet wurde',
'Become this user' => 'Zu diesem Nutzer wechseln',
Expand Down
12 changes: 12 additions & 0 deletions models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ public function getIsAdmin()
|| in_array($this->username, $this->module->admins);
}

/**
* Wrapper function to retrieve authorization items of the current user.
*
* @return mixed return the assigned authorization items of the current user. When no authManager is configured,
* it returns an empty array since by definition there are no authorization items for the current (or any) user.
*/
public function getAuthItems()
{
return \Yii::$app->authManager ? \Yii::$app->authManager->getItemsByUser($this->id) : [];
}

/**
* @return \yii\db\ActiveQuery
*/
Expand Down Expand Up @@ -187,6 +198,7 @@ public function getAuthKey()
public function attributeLabels()
{
return [
'auth_item' => \Yii::t('user', 'Auth item'),
'username' => \Yii::t('user', 'Username'),
'email' => \Yii::t('user', 'Email'),
'registration_ip' => \Yii::t('user', 'Registration ip'),
Expand Down
15 changes: 12 additions & 3 deletions models/UserSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class UserSearch extends Model
/** @var string */
public $registration_ip;

/** @var string in case DbManager is used, we can filter users by auth_item in the admin/index view */
public $auth_item;

/** @var Finder */
protected $finder;

Expand All @@ -56,7 +59,8 @@ public function __construct(Finder $finder, $config = [])
public function rules()
{
return [
'fieldsSafe' => [['id', 'username', 'email', 'registration_ip', 'created_at', 'last_login_at'], 'safe'],

'fieldsSafe' => [['id', 'username', 'email', 'registration_ip', 'created_at', 'last_login_at', 'auth_item'], 'safe'],
'createdDefault' => ['created_at', 'default', 'value' => null],
'lastloginDefault' => ['last_login_at', 'default', 'value' => null],
];
Expand Down Expand Up @@ -93,8 +97,13 @@ public function search($params)
return $dataProvider;
}

$model = $query->modelClass;
$table_name = $model::tableName();
if (\Yii::$app->authManager instanceof yii\rbac\DbManager && $this->auth_item) {
$assignment_table = \Yii::$app->authManager->assignmentTable;
$query->leftJoin($assignment_table, $assignment_table.'.user_id = user.id');
$query->andFilterWhere(['item_name' => $this->auth_item]);
}

$table_name = $query->modelClass::tableName();

if ($this->created_at !== null) {
$date = strtotime($this->created_at);
Expand Down
18 changes: 16 additions & 2 deletions views/admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
*/

use yii\grid\GridView;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\web\View;
use yii\widgets\Pjax;


/**
* @var \yii\web\View $this
* @var \yii\data\ActiveDataProvider $dataProvider
Expand Down Expand Up @@ -62,7 +62,6 @@
}
},
],

[
'attribute' => 'last_login_at',
'value' => function ($model) {
Expand All @@ -75,6 +74,21 @@
}
},
],
[
'attribute' => 'auth_item',
'format' => 'html',
'value' => function ($data) {
return implode(', ', array_map(function ($ai) {
return $ai->name;
}, $data->authItems));
},
'filter' => \Yii::$app->authManager instanceof yii\rbac\DbManager ? ArrayHelper::map(array_merge(
Yii::$app->authManager->getRoles(),
Yii::$app->authManager->getPermissions()
),
'name', 'name') : null,
'visible' => Yii::$app->get('authManager') instanceof yii\rbac\ManagerInterface,
],
[
'header' => Yii::t('user', 'Confirmation'),
'value' => function ($model) {
Expand Down

0 comments on commit ffe298b

Please sign in to comment.