M
M
MR_TOLSTIC2017-02-26 12:43:23
Yii
MR_TOLSTIC, 2017-02-26 12:43:23

Why when doing rbac/init YIi2?

Advanced pattern, could not execute rbac/init. I want to add roles. It keeps saying that the userGroup class does not exist. Or am I missing something in the guides or stupid. In advanced, this crashes:

Exception 'ReflectionException' with message 'Class userGroup does not exist'

in D:\OpenServer\domains\Kinder\vendor\yiisoft\yii2\di\Container.php:424

Stack trace:
#0 D:\OpenServer\domains\Kinder\vendor\yiisoft\yii2\di\Container.php(424): ReflectionClass->__construct('userGroup')
#1 D:\OpenServer\domains\Kinder\vendor\yiisoft\yii2\di\Container.php(364): yii\di\Container->getDependencies('userGroup')
#2 D:\OpenServer\domains\Kinder\vendor\yiisoft\yii2\di\Container.php(156): yii\di\Container->build('userGroup', Array, Array)
#3 D:\OpenServer\domains\Kinder\vendor\yiisoft\yii2\BaseYii.php(340): yii\di\Container->get('userGroup', Array)
#4 D:\OpenServer\domains\Kinder\vendor\yiisoft\yii2\rbac\BaseManager.php(121): yii\BaseYii::createObject('userGroup')
#5 D:\OpenServer\domains\Kinder\console\controllers\RbacController.php(66): yii\rbac\BaseManager->add(Object(yii\rbac\Role))
#6 [internal function]: console\controllers\RbacController->actionInit()
#7 D:\OpenServer\domains\Kinder\vendor\yiisoft\yii2\base\InlineAction.php(57): call_user_func_array(Array, Array)
#8 D:\OpenServer\domains\Kinder\vendor\yiisoft\yii2\base\Controller.php(156): yii\base\InlineAction->runWithParams(Array)
#9 D:\OpenServer\domains\Kinder\vendor\yiisoft\yii2\console\Controller.php(128): yii\base\Controller->runAction('init', Array)
#10 D:\OpenServer\domains\Kinder\vendor\yiisoft\yii2\base\Module.php(523): yii\console\Controller->runAction('init', Array)
#11 D:\OpenServer\domains\Kinder\vendor\yiisoft\yii2\console\Application.php(180): yii\base\Module->runAction('rbac/init', Array)
#12 D:\OpenServer\domains\Kinder\vendor\yiisoft\yii2\console\Application.php(147): yii\console\Application->runAction('rbac/init', Array)
#13 D:\OpenServer\domains\Kinder\vendor\yiisoft\yii2\base\Application.php(380): yii\console\Application->handleRequest(Object(yii\console\Request))
#14 D:\OpenServer\domains\Kinder\yii(27): yii\base\Application->run()
#15 {main}

Here is my RbacController
namespace console\controllers;

use Yii;
use yii\console\Controller;
use \common\rbac\UserGroupRule;


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

        //на всякий случай, удаляем старые данные
        $auth->removeAll();

        //создаем роли
        $guest  = $auth->createRole('guest');
        $parent  = $auth->createRole('parent');
        $coach = $auth->createRole('coach');
        $moderator = $auth->createRole('moderator');
        $admin  = $auth->createRole('admin');

        //создаем разрешения
        $login  = $auth->createPermission('login');
        $logout = $auth->createPermission('logout');
        $error  = $auth->createPermission('error');
        $signUp = $auth->createPermission('sign-up');
        $index  = $auth->createPermission('index');
        $create  = $auth->createPermission('create');
        $view   = $auth->createPermission('view');
        $update = $auth->createPermission('update');
        $delete = $auth->createPermission('delete');

        //добавляем разрешения
        $auth->add($login);
        $auth->add($logout);
        $auth->add($error);
        $auth->add($signUp);
        $auth->add($index);
        $auth->add($create);
        $auth->add($view);
        $auth->add($update);
        $auth->add($delete);

        //добавляем правила, базирующиеся на группах
        $userGroupRule = new UserGroupRule();

        // добавляем правило "UserGroupRule" в роли
        $guest->ruleName  = $userGroupRule->name;
        $parent->ruleName  = $userGroupRule->name;
        $coach->ruleName = $userGroupRule->name;
        $moderator->ruleName = $userGroupRule->name;
        $admin->ruleName  = $userGroupRule->name;

        //Добавляем роли в Yii::$app->authManager
        $auth->add($guest);
        $auth->add($parent);
        $auth->add($coach);
        $auth->add($moderator);
        $auth->add($admin);

        //добавляем потомков
        $auth->addChild($admin, $moderator);

        $auth->addChild($moderator, $parent);
        $auth->addChild($moderator, $coach);

        $auth->addChild($parent, $guest);
        $auth->addChild($coach, $guest);

        $auth->addChild($guest, $view);
        $auth->addChild($guest, $login);
        $auth->addChild($guest, $logout);
        $auth->addChild($guest, $error);
        $auth->addChild($guest, $signUp);
        $auth->addChild($guest, $index);

        $auth->addChild($parent, $create);
        $auth->addChild($coach, $create);

        $auth->addChild($moderator, $delete);
        $auth->addChild($moderator, $update);





        $admin = $auth->createRole('admin');
        $admin->description = 'Администратор';
        $admin->ruleName = $userGroupRule->name;

        $moderator = $auth->createRole('moderator');
        $moderator->description = 'Модератор';
        $moderator->ruleName = $userGroupRule->name;

        $parent = $auth->createRole('parent');
        $parent->description = 'Родитель';
        $parent->ruleName = $userGroupRule->name;

        $coach = $auth->createRole('coach');
        $coach->description = 'Тренер';
        $coach->ruleName = $userGroupRule->name;

        $guest = $auth->createRole('guest');
        $guest->description = 'Гость';
        $guest->ruleName = $userGroupRule->name;
    }

}

here is the config
return [
    'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
    'components' => [
        'authManager' => [
            'class' => 'yii\rbac\DbManager',
            'defaultRoles' => ['guest','coach', 'parent', 'moderator','admin'], //здесь прописываем роли
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
    ],
];

here is the role
namespace common\rbac;

use yii\rbac\Rule;

class UserGroupRule extends Rule
{
    public $name = 'userGroup';

    public function execute($user, $item, $params)
    {
        if (!Yii::$app->user->isGuest) {
            $group = Yii::$app->user->identity->group;
            if ($item->name === 'admin') {
                return $group == 5;
            } elseif ($item->name === 'moderator') {
                return $group == 4 || $group == 5;
            } elseif ($item->name === 'coach') {
                return $group == 3 || $group == 4 || $group == 5;
            } elseif ($item->name === 'parent') {
                return $group == 2 || $group == 4 || $group == 5;
            } elseif ($item->name === 'guest') {
                return $group == 1 || $group == 2 || $group == 3 || $group == 4 || $group == 5;
            }
        }
        return false;
    }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
padlyuck, 2017-02-26
@MR_TOLSTIC

//добавляем правила, базирующиеся на группах
        $userGroupRule = new UserGroupRule();
        $auth->add($userGroupRule);// <-- не добавлено в манагер вот и не находит

D
Dmitry, 2017-02-26
@slo_nik

Good morning.
Maybe it will be more correct like this
Well, you really don’t have a userGroup class, there is a UserGroupRule;

M
Maxim Timofeev, 2017-02-26
@webinar

Are you sure you need yii\rbac\DbManager and not yii\rbac\PhpManager. Since yii\rbac\DbManager is usually not set to 'defaultRoles'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question