Answer the question
In order to leave comments, you need to log in
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);
}
}
Answer the question
In order to leave comments, you need to log in
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',
],
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question