Yii 2.0 RBAC Rule activation error.

Yii 2.0.13.1

Advanced template

PHP: 7.1.8

extension: mdmsoft/yii2-admin

I used the documentation to create the files need to get my first rule installed for RBAC, database version. The authorization part is working fine already, i just want to add rules now to start detailing the authorization.

I did everything from the documentation here: http://www.yiiframework.com/doc-2.0/guide-security-authorization.html#using-rules

But when i run the RbacController init i get an error in my console:

./yii rbac/init

An Error occurred while handling another error:

ReflectionException: Class user does not exist in /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/di/Container.php:426

Stack trace:

#0 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/di/Container.php(426): ReflectionClass->__construct(‘user’)

#1 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/di/Container.php(364): yii\di\Container->getDependencies(‘user’)

#2 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/di/Container.php(156): yii\di\Container->build(‘user’, Array, Array)

#3 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/di/Instance.php(167): yii\di\Container->get(‘user’)

#4 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/di/Instance.php(137): yii\di\Instance->get(NULL)

#5 /Applications/MAMP/htdocs/advanced/vendor/mdmsoft/yii2-admin/components/AccessControl.php(49): yii\di\Instance::ensure(Object(yii\di\Instance), ‘yii\\web\\User’)

#6 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/base/BaseObject.php(135): mdm\admin\components\AccessControl->getUser()

#7 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/base/Component.php(140): yii\base\BaseObject->__get(‘user’)

#8 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/di/ServiceLocator.php(77): yii\base\Component->__get(‘user’)

#9 /Applications/MAMP/htdocs/advanced/vendor/yii2mod/yii2-rbac/controllers/AssignmentController.php(54): yii\di\ServiceLocator->__get(‘user’)

#10 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/base/BaseObject.php(108): yii2mod\rbac\controllers\AssignmentController->init()

#11 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/base/Controller.php(86): yii\base\BaseObject->__construct(Array)

#12 [internal function]: yii\base\Controller->__construct(‘assignment’, Object(yii2mod\rbac\Module), Array)

#13 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/di/Container.php(375): ReflectionClass->newInstanceArgs(Array)

#14 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/di/Container.php(156): yii\di\Container->build(‘yii2mod\\rbac\\co…’, Array, Array)

#15 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/BaseYii.php(345): yii\di\Container->get(‘yii2mod\\rbac\\co…’, Array)

#16 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/base/Module.php(643): yii\BaseYii::createObject(‘yii2mod\\rbac\\co…’, Array)

#17 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/base/Module.php(596): yii\base\Module->createControllerByID(‘assignment’)

#18 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/base/Module.php(588): yii\base\Module->createController(’’)

#19 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/console/UnknownCommandException.php(79): yii\base\Module->createController(’’)

#20 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/console/ErrorHandler.php(35): yii\console\UnknownCommandException->getSuggestedAlternatives()

#21 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yii\console\ErrorHandler->renderException(Object(yii\console\UnknownCommandException))

#22 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\console\UnknownCommandException))

#23 {main}

Hi MarcoD, welcome to the forum.

I’m sorry, but I don’t get any clue to solve your issue. Maybe you have to elaborate what you have done so far to describe your issue in details.

What is your "first rule"? And what rules do you want to add?

Where is "RbacController" implemented? Is it your own creation?

What is "yii2mod/yii2-rbac"? Is it another extension for handling RBAC?

Hello @softark!!

Thanks for getting back to me, this is really stopping me on this subject.

Below the rule i want to add to see if it works and how i can use it.

Your 3rd statement was very good, i searched for it and i removed it, i tried that plugin but didnt want to use it and forgot to remove it. I removed it and now I get further.

I still use mdmsoft/yii2-admin to manage the users and rules. If i use their web-interface to add a rule i get the message: “Unknown class ‘backend\components\TestRule’”




<?php


namespace backend\components;


use Yii;

use yii\rbac\Rule;


class TestRule extends Rule

{

    const RULE_NAME = 'test_rule';


    /**

     * @inheritdoc

     */

    public $name = self::RULE_NAME;


    /**

     * @inheritdoc

     */

    public function execute($user, $item, $params)

    {

        $routeParams = isset($item->data['params']) ? $item->data['params'] : [];

        foreach ($routeParams as $key => $value) {

            if (!array_key_exists($key, $params) || $params[$key] != $value) {

                return false;

            }

        }

        return true;

    }

}

Also if i try it with the rbac controller with the content below i get the same error:




<?php

namespace console\controllers;


use Yii;

use yii\console\Controller;


class RbacController extends Controller

{

    public function actionInit()

    {

        $auth = Yii::$app->authManager;


        $rule = new \backend\components\TestRule;

        $auth->add($rule);

    }

}







 ./yii rbac/init

Exception 'Error' with message 'Class 'backend\components\TestRule' not found'


in /Applications/MAMP/htdocs/advanced/console/controllers/RbacController.php:41


Stack trace:

#0 [internal function]: console\controllers\RbacController->actionInit()

#1 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)

#2 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)

#3 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/console/Controller.php(135): yii\base\Controller->runAction('init', Array)

#4 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/base/Module.php(528): yii\console\Controller->runAction('init', Array)

#5 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction('rbac/init', Array)

#6 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/console/Application.php(147): yii\console\Application->runAction('rbac/init', Array)

#7 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/base/Application.php(386): yii\console\Application->handleRequest(Object(yii\console\Request))

#8 /Applications/MAMP/htdocs/advanced/yii(27): yii\base\Application->run()

#9 {main}



Probably you have to use common folder instead of backend to store your rules.

P.S.

You are trying to access a class in “backend” from “frontend” app and “console” app. It should be possible by specifying an appropriate namespace, but it’s better to follow the convention.

Thanks for the tip about directory, i move it to common directory. But still the same error:




./yii rbac/init

Exception 'Error' with message 'Class 'common\rules\TestRule' not found'


in /Applications/MAMP/htdocs/advanced/console/controllers/RbacController.php:41


Stack trace:

#0 [internal function]: console\controllers\RbacController->actionInit()

#1 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)

#2 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)

#3 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/console/Controller.php(135): yii\base\Controller->runAction('init', Array)

#4 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/base/Module.php(528): yii\console\Controller->runAction('init', Array)

#5 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction('rbac/init', Array)

#6 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/console/Application.php(147): yii\console\Application->runAction('rbac/init', Array)

#7 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/base/Application.php(386): yii\console\Application->handleRequest(Object(yii\console\Request))

#8 /Applications/MAMP/htdocs/advanced/yii(27): yii\base\Application->run()

#9 {main}



And this is the rule in the common/rules folder:




<?php


namespace common\rules;


use Yii;

use yii\rbac\Rule;


class TestRule extends Rule

{

    const RULE_NAME = 'test_rule';


    /**

     * @inheritdoc

     */

    public $name = self::RULE_NAME;


    /**

     * @inheritdoc

     */

    public function execute($user, $item, $params)

    {

        $routeParams = isset($item->data['params']) ? $item->data['params'] : [];

        foreach ($routeParams as $key => $value) {

            if (!array_key_exists($key, $params) || $params[$key] != $value) {

                return false;

            }

        }

        return true;

    }

}



Probably this?




$auth = Yii::$app->authManager;


// add the rule

$rule = new \common\rules\TestRule;

$auth->add($rule);



I have that in my controller:




<?php

namespace console\controllers;


use Yii;

use yii\console\Controller;


class RbacController extends Controller

{

    public function actionInit()

    {

        $auth = Yii::$app->authManager;      


        $rule = new \common\rules\TestRule;

        $auth->add($rule);

        

        

        $auth = Yii::$app->authManager;


    }

}



But still i get this error:




./yii rbac/init

Exception 'Error' with message 'Class 'common\rules\TestRule' not found'


in /Applications/MAMP/htdocs/advanced/console/controllers/RbacController.php:13


Stack trace:

#0 [internal function]: console\controllers\RbacController->actionInit()

#1 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)

#2 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)

#3 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/console/Controller.php(135): yii\base\Controller->runAction('init', Array)

#4 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/base/Module.php(528): yii\console\Controller->runAction('init', Array)

#5 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction('rbac/init', Array)

#6 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/console/Application.php(147): yii\console\Application->runAction('rbac/init', Array)

#7 /Applications/MAMP/htdocs/advanced/vendor/yiisoft/yii2/base/Application.php(386): yii\console\Application->handleRequest(Object(yii\console\Request))

#8 /Applications/MAMP/htdocs/advanced/yii(27): yii\base\Application->run()

#9 {main}

Marcos-MacBook-Pro-3:advanced marcoduizer$ 



That’s very strange.

It looks like the path alias for ‘common’ is not properly set. You also reported that ‘backend’ was not accessible from the console app…

If you don’t have any clue, I’d suggest you to redo the initialization process of the app-advanced project.

Yes, I really don’t understand why just this part is not working. The rest of the application is working fine, only with the rule in the advanced template folders i have a problem. I can see the rule from a extension, that is working fine.

Are there path settings that i can check? Just reinstall is a bit of big, to keep my changes?

I would implant the following line in RbacController::actionInit() for debugging:




echo Yii::getAlias('@common');



I get this path as result:

/Applications/MAMP/htdocs/advanced/common

I’m running it on a MAc in MAMP.

Is this the correct path it is getting back?

It looks fine.

Are the front-end app and the back-end app running fine with RBAC?

Yes, i use RBAC at front and back. Only the rules are not working.

Well, this might be a silly question, but …

Is your TestRule class stored in a file named "TestRule.php" in the directory of "/Applications/MAMP/htdocs/advanced/common"?

Yes, thats correct

I changed the directory name to basic_rules.php but that doesn’t make a difference

"basic_rules.php" for a directory name? Did you mean to say file name?

The directory name and the file name must be strictly compliant with the rules of Yii Autoloader:

Class Autoloading

http://www.yiiframework.com/doc-2.0/guide-concept-autoloading.html

I have now: common\rules\basic_rules.php as path to php file.

Thanks for the link, good read!!

But with this it still doesn’t work. Also after deployment on the bluehost server it doesn’t work. I created some rules in the code to make it secure but strange i don’t get it to work.