S
S
Sergey Beloventsev2017-01-05 12:33:57
Yii
Sergey Beloventsev, 2017-01-05 12:33:57

How to hide fields from certain GridView users?

I have led the rule `canViewsSite`in users, if the user does not have such permission, then the fields should not be displayed.
tried to do so in the view

$gridColums=[
            [
                'attribute'=>'key_feild',
                'value'    => function($model){
                    return $model->canViewsKeyFeild($model);
                }
          ],
           [
                'attribute'=>'value',
                'value'    => function($model){
                    return $model->canViewsValue($model);
                },
           ],

['class' => 'yii\grid\ActionColumn'],
];
here in the model
public function canViewsKeyFeild($model){
            if($model->key_feild=='site'){
                if(Yii::$app->user->can('canViewsSite')){
                    return $model->key_feild;
                }else{
                    return false;
                }
            }else{
                return $model->key_feild;
            }
        }

but here an error occurs - the field is simply not filled in, and when editing, you can generally go in and see everything. Is it possible to somehow hide the fields if the user does not have a rule `canViewsSite`and a value
keyfeild == 'site'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-01-05
@Sergalas

If I understood the task correctly, then you have a table in which there is a field name (key_feild) and its value (value), respectively, for users who do not have the canViewsSite permission, you need to hide the data with key_field = 'site'
Then it is not clear why this should be done on the GridView level if you can simply select data according to the appropriate conditions from the database. For example like this:

MyModel::find()->where(['not' in, 'key_field', 'site']);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question