Yii Framework Forum: Yii 2.0 RBAC Rule activation error. - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

Yii 2.0 RBAC Rule activation error. Rate Topic: -----

#1 User is offline   MarcoD 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 13-November 17

Posted 26 November 2017 - 09:28 AM

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.yiiframew...tml#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}
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,418
  • Joined: 16-February 11
  • Location:Japan

Posted 27 November 2017 - 10:12 AM

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.

Quote

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.yiiframew...tml#using-rules


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

Quote

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


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

Quote

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


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

#3 User is offline   MarcoD 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 13-November 17

Posted 27 November 2017 - 06:10 PM

View Postsoftark, on 27 November 2017 - 10:12 AM, said:

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

0

#4 User is offline   MarcoD 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 13-November 17

Posted 27 November 2017 - 06:16 PM

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}

0

#5 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,418
  • Joined: 16-February 11
  • Location:Japan

Posted 28 November 2017 - 04:32 AM

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.

https://github.com/y...-directories.md
0

#6 User is offline   MarcoD 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 13-November 17

Posted 28 November 2017 - 10:07 AM

View Postsoftark, on 28 November 2017 - 04:32 AM, said:

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.

https://github.com/y...-directories.md


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

0

#7 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,418
  • Joined: 16-February 11
  • Location:Japan

Posted 28 November 2017 - 06:02 PM

Probably this?

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

// add the rule
$rule = new \common\rules\TestRule;
$auth->add($rule);

0

#8 User is offline   MarcoD 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 13-November 17

Posted 29 November 2017 - 11:37 AM

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$ 

0

#9 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,418
  • Joined: 16-February 11
  • Location:Japan

Posted 29 November 2017 - 06:07 PM

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.

https://github.com/y...installation.md
0

#10 User is offline   MarcoD 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 13-November 17

Posted 30 November 2017 - 03:01 PM

View Postsoftark, on 29 November 2017 - 06:07 PM, said:

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.

https://github.com/y...installation.md


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?
0

#11 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,418
  • Joined: 16-February 11
  • Location:Japan

Posted 01 December 2017 - 09:11 AM

I would implant the following line in RbacController::actionInit() for debugging:
echo Yii::getAlias('@common');

0

#12 User is offline   MarcoD 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 13-November 17

Posted 02 December 2017 - 11:21 AM

View Postsoftark, on 01 December 2017 - 09:11 AM, said:

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?
0

#13 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,418
  • Joined: 16-February 11
  • Location:Japan

Posted 02 December 2017 - 05:35 PM

View PostMarcoD, on 02 December 2017 - 11:21 AM, said:

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?
0

#14 User is offline   MarcoD 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 13-November 17

Posted 03 December 2017 - 09:29 PM

View Postsoftark, on 02 December 2017 - 05:35 PM, said:

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.
0

#15 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,418
  • Joined: 16-February 11
  • Location:Japan

Posted 04 December 2017 - 04:17 AM

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"?
0

#16 User is offline   MarcoD 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 13-November 17

Posted 04 December 2017 - 10:18 AM

View Postsoftark, on 04 December 2017 - 04:17 AM, said:

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
0

#17 User is offline   MarcoD 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 13-November 17

Posted 04 December 2017 - 10:21 AM

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

#18 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,418
  • Joined: 16-February 11
  • Location:Japan

Posted 04 December 2017 - 05:24 PM

View PostMarcoD, on 04 December 2017 - 10:21 AM, said:

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.yiiframew...utoloading.html
0

#19 User is offline   MarcoD 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 13-November 17

Posted 04 December 2017 - 07:04 PM

View Postsoftark, on 04 December 2017 - 05:24 PM, said:

"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.yiiframew...utoloading.html

I have now: common\rules\basic_rules.php as path to php file.
Thanks for the link, good read!!
0

#20 User is offline   MarcoD 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 13-November 17

Posted 07 December 2017 - 06:48 PM

View Postsoftark, on 04 December 2017 - 05:24 PM, said:

"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.yiiframew...utoloading.html

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.
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users