Advanced шаблон, не получалось выполнить rbac/init. Я хочу добавить роли. У меня все время пишет что класс userGroup не существует. Или я чего то в гайдах недочитываю или туплю. В advanced вылетает это:
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}
Вот мой 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;
}
}
вот config
return [
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'components' => [
'authManager' => [
'class' => 'yii\rbac\DbManager',
'defaultRoles' => ['guest','coach', 'parent', 'moderator','admin'], //здесь прописываем роли
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
],
];
вот роль
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;
}
}