Yii2使用不同模型的访问规则

时间:2015-07-23 09:36:41

标签: yii2 access-rules role-based-access-control yii2-model

我在使用Yii2基于角色的访问控制时遇到问题。在通常的设置中,身份验证规则在当前用户的身份时发生。喜欢写在文档中。 Authorization

在我的情况下,如何使用另一组模型设置授权(除基本功能外)。这是我的设置。

来自rbac迁移的表auth_assignment [item_nameuser_id], 来自yii2迁移的user [id]。 我创建了一个新表assignment [user_iduser相关,rec_idrecognition的{​​{1}}相关。

这就是场景。我有角色organizationadminorganization-head。如何检查memberorganization-head是否属于他们自己的识别模块;不是其他组织负责人的其他模块吗?

我还使用peixoto的上下文访问控制过滤器。

这是我的检查代码。 RecognitionRule检查用户member是否等于用户的身份;并且user_id等于account_id。第二个条件告诉他是否属于组织

rec_id

但是,我不允许执行此操作。有线索吗?

1 个答案:

答案 0 :(得分:0)

我得到了答案。我的答案基于AccessRule behavior and rbac\Rule $params

RecognitionRule的片段

/**
 * @param string|integer $user the user ID.
 * @param Item $item the role or permission that this rule is associated with
 * @param array $params parameters passed to ManagerInterface::checkAccess().
 * @return boolean a value indicating whether the rule permits the role or permission it is associated with.
 */
public function execute($user, $item, $params)
{
    if(isset($params['recognition'])){ //Directly specify the model you plan to use via param
        $model = $params['recognition']; 
    } else{ //Use the controller findModel method to get the model - this is what executes via the behaviour/rules
        $id = Yii::$app->request->get('id'); //Note, this is an assumption on your url structure.
    }

    return \common\models\Assignment::find()->where(['rec_id' => $id, 'user_id' => $user])->exists();
}
}
?>

RecognitionController

                [
                    'class' => 'common\rbac\ContextAccessRule',
                    'modelClass' => 'frontend\models\recognition',
                    'allow' => true,
                    'actions' => ['view','update'],
                    'roles' => ['viewOwnRecognition', 'updateOwnRecognition'],
                ],
            ],
        ],
    ];
相关问题