Combine RBAC roles in Controller AccessControl

Hello,

I need to check access for two permissions in some action




                'class' => AccessControl::className(),

                'rules' => [

                    [

                        'actions' => ['index'],

                        'allow' => true,

                        'roles' => [Role::MANAGER, Role::IN_DEPARTMENT],  // returns true if one of roles returns true

                    ],



But is problem I need to check is user have rights for both Role::MANAGER AND Role::IN_DEPARTMENT !

Is this possible with AccessControl?

If the method returns true or false, you could use matchCallback. Something like:




  'class' => AccessControl::className(),

                'rules' => [

                    [

                        'actions' => ['index'],

                        'allow' => true,

                        'roles' =>[ '@']

                        'matchCallback' => function ($rule, $action) {

                            return Role::MANAGER && Role::IN_DEPARTMENT;

                        }                    

                    ],






I should mention that I don’t currently use the built-in Yii 2 RBAC, so I do not know if that will work, just a suggestion. I use something like the above in my application and it works great, but I believe the methods have to return true or false for this to work.

I think it’s can’t work correctly.

Because, "return Role::MANAGER && Role::IN_DEPARTMENT" always return his string representation converted into boolean.

Use Yii2-admin to setup roles in database.

then, if (Yii::$app->user->can(‘aRoleAccesss’) and Yii::$app->user->can(‘bRoleAccesss’)) {…}