B
B
BloodVIRUS2021-09-21 08:00:25
PHP
BloodVIRUS, 2021-09-21 08:00:25

How to make different permissions for action in one controller?

Hello! I understand Yii2, and got to the separation of rights. Found and dealt with RBAC. Handy thing, very. But all the examples that I found - they show how to allow access to some action in the controller for a user group. For example here:

'actions' => ['index','delete','update'],
                    'allow' => true,
                    'roles' => ['root'],


In this case, only those users who have the root role have access to the allowed functions. And it so happened that all the other actions in the controller are not available to anyone at all ..

And if I need to differentiate access to root, users and autors in one controller?
Ie for ruts it is possible actionCreateCategory, but only for ruts. For authors, actionCreatePost is available, and for users, only actionCommentPost is available. How to set it all up in one controller without spreading it all over different ones?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
My joy, 2021-09-21
@t-alexashka

rules is an array with rules. add as many as you like. For example:

//...
'rules' => [
  [
    'allow' => true,
    'actions' => ['action1',],
    'roles' => ['root', ],
  ],
  [
    'allow' => true,
    'actions' => ['action2', 'actionN', ],
    'roles' => ['admin', '?', '@'],
  ],
  //...
],
//...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question