R
R
Rustam Akimov2019-11-05 20:05:22
Yii
Rustam Akimov, 2019-11-05 20:05:22

How does Rbac work in Yii2?

Such a question, for example, is the role of the author, the author can add a record and edit his records, there is an admin role, the admin can add records and edit all records.
Code example:

$auth = Yii::$app->authManager;

$createPost = $auth->createPermission('createPost');
$updatePost = $auth->createPermission('updatePost');
$updateOwnPost = new UpdateOwnPostRule();

$auth->add($createPost);
$auth->add($updatePost);
$auth->add($updateOwnPost);

$auth->addChild($updateOwnPost, $updatePost);

$author = $auth->createRole('author');
$admin = $auth->createRole('admin');
$auth->add($author);
$auth->add($admin);

$auth->addChild($author, $createPost);
$auth->addChild($author, $updateOwnPost);

$auth->addChild($admin, $author);
$auth->addChild($admin, $updatePost);

And here I have two questions:
1. Will updatePost or updateOwnPost work for the admin?
2. If I add any permission for the author after this code, will the admin get them?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2019-11-05
@Inogami

Study it already) Full of examples and descriptions!
https://habr.com/ru/post/235485/
https://anart.ru/yii2/2016/04/11/yii2-rbac-ponyatn...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question