在我的源代码中,我有这段代码用于检查用户是否具有权限。为此,我使用RBAC。在我的控制器中,我有这个:
public function behaviors()
{
$behaviors['access'] = [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@'],
'matchCallback' => function ($rule, $action) {
$module = Yii::$app->controller->module->id;
$action = Yii::$app->controller->action->id;
$controller = Yii::$app->controller->id;
$route = "$module/$controller/$action";
$post = Yii::$app->request->post();
if (\Yii::$app->user->can($route)) {
return true;
} else {
Yii::$app->session->setFlash('error', 'Your user does not have access to this module.');
return $this->redirect('dashboard');
}
}
],
],
];
return $behaviors;
}
我的问题是当我使用函数
时if (\Yii::$app->user->can($route)) { ....
Yii显示以下错误:
PHP Warning – yii\base\ErrorException
in_array() expects parameter 2 to be array, string given ....
........
in /lxcshared/yii-develop/sacyii.git/vendor/yiisoft/yii2/rbac/DbManager.php at line 196
........
if (isset($assignments[$itemName]) || in_array($itemName, $this->defaultRoles)) {
return true;
}
我遵循本指南Role Based Access Control (RBAC),但我不知道为什么会遇到这个问题。
答案 0 :(得分:2)
检查您的配置authManager->defaultRoles
。它必须是数组。
'authManager' => [
...
'defaultRoles' => [...]
]