如何在 Kartik 的 GridView - Yii2 中显示 RBAC 用户角色

How to display RBAC user role in Kartik's GridView - Yii2

我的数据库中有一个“用户”table,我在 Kartik 的 GridView 中显示此 table 的内容,如下所示:

<?php
 $gridColumns = [
   [
     'class' => 'kartik\grid\SerialColumn',
     'width' => '20px',
   ],
   'email',
   'name',
   'surname',
   [
     'class' => 'kartik\grid\BooleanColumn',
     'label' => 'Disabled?',
     'attribute' => 'isDisabled',
     'trueLabel' => 'Yes',
     'falseLabel' => 'No'
   ],
   [
     'class' => 'kartik\grid\ActionColumn',
     'width' => '100px',
   ],
 ];
 echo GridView::widget([
   'dataProvider' => $dataProvider,
   'filterModel' => $searchModel,
   'columns' => $gridColumns,
   'pjax' => true,
   'bordered' => true,
   'striped' => false,
   'condensed' => false,
   'responsive' => true,
   'hover' => true,
   'panel' => [
     'type' => GridView::TYPE_PRIMARY,
     'heading' => "<h3 class=\"panel-title\"><i class=\"glyphicon glyphicon-user\"></i>$this->title </h3>",
     'after' => false,
     'before' => false
   ],
 ]);
?>

我也使用 RBAC,我有 2 个角色(管理员和经理)。

我需要做什么
我需要在上面的 GridView 中放入每个用户的角色,并且还可以按该角色过滤数据。
最好该“角色”列的 class 应该是 'kartik\grid\EnumColumn',因为我只有 2 个角色。

我不知道从哪里开始,任何提示都可以。

已解决

我终于找到了一个可行的解决方案,这里是 GridView 的代码:

           <?php
                $gridColumns = [
                    [
                        'class' => 'kartik\grid\SerialColumn',
                        'width' => '20px',
                    ],
                    'email',
                    'name',
                    'surname',
                    [
                        'class' => 'kartik\grid\EnumColumn',
                        'enum' => ['admin', 'manager'],
                        'filter' => [
                            'admin' => 'admin',
                            'manager' => 'manager',
                        ],
                        'label' => 'Role',
                        'attribute' => 'authAssignment',
                        'value' => 'authAssignment.item_name'
                    ],
                    [
                        'class' => 'kartik\grid\BooleanColumn',
                        'label' => 'Disabled?',
                        'attribute' => 'isDisabled',
                        'trueLabel' => 'Yes',
                        'falseLabel' => 'No'
                    ],
                    [
                        'class' => 'kartik\grid\ActionColumn',
                        'width' => '100px',
                    ],
                ];
                echo GridView::widget([
                    'dataProvider' => $dataProvider,
                    'filterModel' => $searchModel,
                    'columns' => $gridColumns,
                    'pjax' => true,
                    'bordered' => true,
                    'striped' => false,
                    'condensed' => false,
                    'responsive' => true,
                    'hover' => true,
                    'panel' => [
                        'type' => GridView::TYPE_PRIMARY,
                        'heading' => "<h3 class=\"panel-title\"><i class=\"glyphicon glyphicon-user\"></i> $this->title </h3>",
                        'after' => false,
                        'before' => false
                    ],
                ]);
            ?>

我将此关系添加到我的用户模型以检索用户的角色:

public function getAuthAssignment(){
    return $this->hasOne(AuthAssignment::className(), ['user_id'=>'id']);
}

在 UserSearch 模型中,我添加了这个 属性 和这个规则:

public $authAssignment;
public function rules()
{
    return [
        ...
        [['authAssignment'], 'safe'],
    ];
}

最后我像这样更改了 UserSearch 模型中的搜索功能:

public function search($params)
{
    $query = User::find();
    $query->leftJoin('public.auth_assignment', 'id=user_id::integer');
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    $dataProvider->sort->attributes['authAssignment'] = [
        'asc' => ['auth_assignment.item_name' => SORT_ASC],
        'desc' => ['auth_assignment.item_name' => SORT_DESC],
    ];

    if (!($this->load($params) && $this->validate())) {
        return $dataProvider;
    }

    // grid filtering conditions
    $query->andFilterWhere([
        'isDisabled' => $this->isDisabled,
        'auth_assignment.item_name' => $this->authAssignment,
    ]);

    $query->andFilterWhere(['ilike', 'email', $this->email])
        ->andFilterWhere(['ilike', 'name', $this->name])
        ->andFilterWhere(['ilike', 'surname', $this->surname]);

    return $dataProvider;
}