【发布时间】:2014-11-10 23:30:58
【问题描述】:
我已经通过实现 SQL 代码来设置表和 rbac/init 脚本来填写角色/权限来设置数据库等。
我在创建用户时有一个 assign(),但我在 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 传递。
请帮忙。谢谢。
【问题讨论】: