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