Search code examples
gridviewyii2rolesrbackartik-v

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


I have a "users" table in my DB and I display the content of this table in Kartik's GridView like this:

<?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
   ],
 ]);
?>

I also use RBAC and I have 2 roles (admin and manager).

What I need to do
I need to put in the above GridView the role of each user and also have the possibility to filter the data by that role.
Preferably the class of that "Role" column should be 'kartik\grid\EnumColumn' because I have just 2 roles.

I don't know where to start, any hint could be good.


Solution

  • Solved

    I finally found a working solution, here is the code of the 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
                        ],
                    ]);
                ?>
    

    I added this relation to my User model to retrieve the roles of the users:

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

    In the UserSearch model I added this property and this rule:

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

    And finally I changed like this my search function in the UserSearch model:

    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;
    }