![](/img/trans.png)
[英]Yii2 RBAC DbManager error Call to a member function getRole() on null
[英]Error Rule not found on RBAC DbManager Yii2
我遵循了本指南( http://www.yiiframework.com/doc-2.0/guide-security-authorization.html ),但仍然出現錯誤。
我已經創建了auth_item,auth_item_chid,auth_assignment和auth_rule表
當我將此行添加到我的控制器時
if (\Yii::$app->user->can('createPost')) { //mycode ... }
我收到錯誤->找不到規則:作者
protected function executeRule($user, $item, $params)
{
if ($item->ruleName === null) {
return true;
}
$rule = $this->getRule($item->ruleName);
if ($rule instanceof Rule) {
return $rule->execute($user, $item, $params);
} else {
throw new InvalidConfigException("Rule not found: {$item->ruleName}");
}
}
我已在auth_item表中為該用戶分配了角色,auth_item已經具有auth_item_chid。 在Yii2上,所有auth_item應該設置為rule_name。 多數民眾贊成在與以前的版本不同。
您可以擴展DbManager修復錯誤
<?php
namespace app\components;
use yii\base\InvalidConfigException;
use yii\rbac\Rule;
class DbManager extends \yii\rbac\DbManager
{
protected function executeRule($user, $item, $params)
{
if (empty($item->ruleName)) {
return true;
}
return parent::executeRule($user, $item, $params);
}
}
並像這樣更改config / web.php
'authManager' => [
'class' => 'app\components\DbManager'
],
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.