ognistyi
(Ognistyi)
October 24, 2014, 4:44pm
1
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?
evercode
(Ikon321)
October 24, 2014, 6:19pm
2
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.
ognistyi
(Ognistyi)
October 31, 2014, 9:04pm
3
evercode:
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.
Scott_Huang
(Zhiliang Huang)
November 1, 2014, 2:57am
4
Use Yii2-admin to setup roles in database.
then, if (Yii::$app->user->can(‘aRoleAccesss’) and Yii::$app->user->can(‘bRoleAccesss’)) {…}