Error in Access Control at use function "can"

Hi,

I am try implementation RBAC, using DbManager.

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

But when trying to verify access with:


if (\Yii::$app->user->can('createPost')) {

    // create post

}

Show this error:


Call to a member function checkAccess() on a non-object

How do I find out the cause of the problem?

Yhanks

Could you show us the stack traces?

I’m sorry, but I’m newbie :)

What is "stack trace" and how do I get it?

This is my page error:


 PHP Fatal Error – yii\base\ErrorException

Call to a member function checkAccess() on a non-object


    1. in C:\Wamp\www\basic\vendor\yiisoft\yii2\web\User.php at line 661

    652653654655656657658659660661662663664665666667668669670


         * [[\yii\rbac\ManagerInterface::checkAccess()]] to obtain the up-to-date access result. Note that this

         * caching is effective only within the same request and only works when `$params = []`.

         * @return boolean whether the user can perform the operation as specified by the given permission.

         */

        public function can($permissionName, $params = [], $allowCaching = true)

        {

            if ($allowCaching && empty($params) && isset($this->_access[$permissionName])) {

                return $this->_access[$permissionName];

            }

            $access = $this->getAuthManager()->checkAccess($this->getId(), $permissionName, $params);

            if ($allowCaching && empty($params)) {

                $this->_access[$permissionName] = $access;

            }

     

            return $access;

        }

     

        /**

         * Returns auth manager associated with the user component.


    2. in C:\Wamp\www\basic\vendor\yiisoft\yii2\base\InlineAction.php at line 55 – app\controllers\PostController::actionCreate()

    3. in C:\Wamp\www\basic\vendor\yiisoft\yii2\base\InlineAction.php at line 55 – call_user_func_array:{C:\Wamp\www\basic\vendor\yiisoft\yii2\base\InlineAction.php:55}()

    4. in C:\Wamp\www\basic\vendor\yiisoft\yii2\base\Controller.php at line 151 – yii\base\InlineAction::runWithParams()

    5. in C:\Wamp\www\basic\vendor\yiisoft\yii2\base\Module.php at line 455 – yii\base\Controller::runAction()

    6. in C:\Wamp\www\basic\vendor\yiisoft\yii2\web\Application.php at line 84 – yii\base\Module::runAction()

    7. in C:\Wamp\www\basic\vendor\yiisoft\yii2\base\Application.php at line 375 – yii\web\Application::handleRequest()

    8. in C:\Wamp\www\basic\web\index.php at line 12 – yii\base\Application::run()

    6789101112


     

    require(__DIR__ . '/../vendor/autoload.php');

    require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

     

    $config = require(__DIR__ . '/../config/web.php');

     

    (new yii\web\Application($config))->run();




check once main.php file, these lines are available or not.

I am get same error because of this.




return [     

    'components' => [         

        'authManager' => [             

            'class' => 'yii\rbac\DbManager',         

        ],          

    ], 

];



You got it and showed it. :)

It enables you to locate the exact place where the error occurred.

The following are the important lines:

It tells that "$this->getAuthManager()" returned null.

As @ilaiya has suggested, it seems like that you have not configured the auth manager correctly.

Exactly!


'authManager' => [

            'class' => 'yii\rbac\DbManager',

        ],

I had put this in "config/console.php" had to be in "config/web.php".