Yii2 RBAC DbManager 错误 Call to a member function getRole() on null
我已经通过实现 SQL 代码来设置表和 rbac/init 脚本来填充角色/权限来设置数据库等.
I've set up the database etc by having implemented SQL code to set up the tables and the rbac/init script to fill out the roles / permissions.
我在创建用户时有一个assign(),但我一直在getRole() 上收到此错误:
I have an assign() at user creation but I keep receiving this error on the getRole():
yii\base\ErrorException 在 null 上调用成员函数 getRole()
public function addUser()
{
if($this->validate()) {
$user = new User();
$auth_key = Yii::$app->getSecurity()->generateRandomString(32);
$this->password = Yii::$app->getSecurity()->generatePasswordHash($this->password);
$user->email = $this->email;
$user->password = $this->password;
$user->active = $this->active;
$user->firstname = $this->firstname;
$user->lastname = $this->lastname;
// $user->nickname = $this->nickname;
$user->datecreated = time();
$user->auth_key = $auth_key;
$user->save(false);
$auth = Yii::$app->authManager;
$authorRole = $auth->getRole($this->role);
$auth->assign($authorRole, $user->getId());
return $user;
}else{
return false;
}
}
$role 变量与其他用户属性一起通过 $_POST 传递.
the $role variable is passed through $_POST along with the other user attributes.
请帮忙.谢谢.
你在这件事上走错了路.
You've gone the wrong way about it.
这里的问题似乎是 Yii::$app->authManager
没有在应该设置的时候设置.这可能意味着您的 main.php 配置文件不包含正确的信息.它应该包含以下组件:
The issue here seems to be that Yii::$app->authManager
is not set when it should be. This probably means that your main.php configuration file does not contain the correct information.
It should contain the following component:
return [
// ...
'components' => [
'authManager' => [
'class' => 'yii\rbac\DbManager',
],
// ...
],
];
(http://www.yiiframework.com/doc-2.0/guide-security-authorization.html#configuring-rbac-manager)
在上面链接的示例中,使用了 PhpManager,但在您的情况下,您需要使用 yii\rbac\DbManager
In the example from the link above PhpManager is used but in your case you will want to use yii\rbac\DbManager
这样做意味着您将只有一个加载的管理器,并且还将解锁所有操作过滤选项.
Doing it this way means that you will only have one loaded manager and will also unlock all action filtering options.