0

使用基于 RBAC 角色的过滤器进行查询的最佳方法是什么。

目的:每个用户角色看到不同的结果。

最好创建一系列“如果”,或者是否有另一个好的组织?

auth_item

id     |  name           
-------+---------
1      |  boss    
2      |  chef 
3      |  employe

桌子contacts

id     |    name     |  id_department |  contact            
-------+-------------+----------------+-------------
1      |    John     |       2        | 999 999 999   
2      |    Angela   |       4        | 999 452 998 
3      |    Bea      |       5        | 999 678 997 
4      |    Monique  |       4        | 999 125 923

我当前的代码:

public function actionIndex()
{
    $searchModel = new Contacts();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    if (\Yii::$app->user->can('employe')) {

        $dataProvider->query->andFilterWhere(['id_department' => 4]);
    } elseif (\Yii::$app->user->can('chef')) {
        $dataProvider->query->andFilterWhere(['id_department' => 2]);
    } elseif (\Yii::$app->user->can('boss')) {
        $dataProvider->query->andFilterWhere(['IN', 'id_department', [1, 2, 3, 4, 5]]); //all results
    }

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}
4

0 回答 0