M
M
MR_TOLSTIC2017-02-24 23:47:01
Yii
MR_TOLSTIC, 2017-02-24 23:47:01

Why is rbac/init YIi2 not running?

Learning to use RBAC. I've looked at a lot of examples. I do everything exactly as in the example, but rbac / init is not executed
here is my rbac controller

namespace app\commands;

use Yii;
use yii\console\Controller;

class RbacController extends Controller
{
    public function actionInit()
    {
        $auth = Yii::$app->authManager;

        $rule = new \app\rbac\UserGroupRule();
        $auth->add($rule);

        // добавляем разрешение "createPost"
        $createPost = $auth->createPermission('createPost');
        $createPost->description = 'Create a post';
        $auth->add($createPost);

        // добавляем разрешение "updatePost"
        $updatePost = $auth->createPermission('updatePost');
        $updatePost->description = 'Update post';
        $auth->add($updatePost);

        // добавляем роль "author" и даём роли разрешение "createPost"
        $guest = $auth->createRole('guest');
        $guest->ruleName = $rule->name;
        $auth->add($guest);
        $auth->addChild($guest, $createPost);

        //moderator
        $moderator = $auth->createRole('moderator');
        $moderator->ruleName = $rule->name;
        $auth->add($moderator);
        $auth->addChild($moderator, $updatePost);
        $auth->addChild($moderator, $guest);

        // добавляем роль "admin" и даём роли разрешение "updatePost"
        // а также все разрешения роли "author"
        $admin = $auth->createRole('admin');
        $admin->ruleName = $rule->name;
        $auth->add($admin);
        $auth->addChild($admin, $moderator);

        // Назначение ролей пользователям. 1 и 2 это IDs возвращаемые IdentityInterface::getId()
        // обычно реализуемый в модели User.
        $auth->assign($guest, 3);
        $auth->assign($moderator, 2);
        $auth->assign($admin, 1);
    }
}

In console I do rbac/init, it gives an error:
PHP Fatal Error 'yii\base\ErrorException' with message 'Call to a member function add() on null'
in D:\OpenServer\domains\KNDR\commands\RbacController.php: 21
Stack trace:
#0 [internal function]: yii\base\ErrorHandler->handleFatalError()
#1 {main}
Where is the error? I killed half a day, I don’t understand what the problem is at all. Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-02-25
@MR_TOLSTIC

I think that you took the guides on the advanced template and put them in basic. My advice: if you want less hemorrhoids and that all third-party solutions work - don't use basic ever.
B carefully read footnotes like this one:

'components' => [
        'authManager' => [
            'class' => 'yii\rbac\DbManager',
        ],

Note: If you are using yii2-basic-app template, there is a config/console.php configuration file where the authManager needs to be declared additionally to config/web.php. In case of yii2-advanced-app the authManager should be declared only once in common/config/main.php.
from guide www.yiiframework.com/doc-2.0/guide-security-author...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question