douzhao7014 2014-11-10 23:30
浏览 283
已采纳

Yii2 RBAC DbManager错误在null上调用成员函数getRole()

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.

I have an assign() at user creation but I keep receiving this error on the getRole():

yii\base\ErrorException Call to a member function getRole() on null

   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;
    }
}

the $role variable is passed through $_POST along with the other user attributes.

Please help. Thanks.

  • 写回答

2条回答 默认 最新

  • douba5540 2014-11-13 11:14
    关注

    You've gone the wrong way about it.

    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' => 'yiibac\DbManager',
            ],
            // ...
        ],
    ];
    

    (http://www.yiiframework.com/doc-2.0/guide-security-authorization.html#configuring-rbac-manager)

    In the example from the link above PhpManager is used but in your case you will want to use yiibac\DbManager

    Doing it this way means that you will only have one loaded manager and will also unlock all action filtering options.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B