0
votes

Error: Call to a member function getRoles() on null

comes from: yii2-admin\models\searchs\AuthItem.php

on line 75:

public function search($params)
{
    /* @var \yii\rbac\Manager $authManager */
    $authManager = Configs::authManager();
    if ($this->type == Item::TYPE_ROLE) {
here->  $items = $authManager->getRoles();
    } else {

This is because of yii2-admin\components\configs.php:

on line 148:

public static function instance()
{
    if (self::$_instance === null) {
        $type = ArrayHelper::getValue(Yii::$app->params, 'mdm.admin.configs', []);
        if (is_array($type) && !isset($type['class'])) {
            $type['class'] = static::className();
        }

        return self::$_instance = Yii::createObject($type);
    }

here->  return self::$_instance;
}

it returns a config object where 'authManager' is null

According to: Yii2 RBAC DbManager error Call to a member function getRole() on null

and

https://www.yiiframework.com/doc/guide/2.0/en/security-authorization#configuring-rbac-manager

All that is necessary for the yii2-advanced-app is to add the following:

'authManager' => [
            'class' => 'common\components\extended\rbac\DbManager',           
            'cache' => 'cache',
        ],

to common\config\main.php under components array

That was already there and does not seem to effect the config from rbac. In addition I have tried adding authManager to console, frontend and backend config files with no effect.

Expected result: 'authManager' => 'DbManager or something'

Actual result: 'authManager' => null

The yii2 rbac config file loads in the db connection correctly, but authManager is always null.

This is a problem when using the yii2admin rbac management extention, but not when using yii2-advanced-app in general as the advanced app uses \Yii::$app->authManager and not yii2 rbac Configs::authManager

Any help or pointers would be greatly appreciated. Thank you.

1
maybe this check fails, is your custom DbManager implementing yii\rbac\ManagerInterface?csminb
@csminb Thank you for your fast reply. You are right on point. I was able to get the error out: '"authManager" refers to a common\components\extended\rbac\DbManager component. yii\rbac\ManagerInterface is expected.' But my DbManager extends BaseManager which in turn implements ManagerInterface. Update: Now I changed ManagerInterface in BaseManager to \yii\rbac\ManagerInterface and everything works now! Thank you so much!aalesund

1 Answers

0
votes

(Based on the comment by csminb)

The code was failing on DbManager's implementation of yii\rbac\ManagerInterface.

Dbmanager which is extended in our program, is already extended from BaseManager which in turn implements ManagerInterface.

Since DbManager and BaseManager are extended they pointed to the extended ManagerInterface which never needed extending in the first place. :-(

So the fix is to change BaseManager.php from:

abstract class BaseManager extends Component implements ManagerInterface

to:

abstract class BaseManager extends Component implements \yii\rbac\ManagerInterface

it is important to note that these extended files are in the folder: namespace common\components\extended\rbac;

Thank you again @csminb and stackoverflow for being so very helpful.