Answer the question
In order to leave comments, you need to log in
How to pass the used model in matchCallback?
There is a need to allow editing the post only by a user with the manager role, and also by the post authors, the id of the post author is stored in the autor_id cell, I
try to organize it like this in the controller:
public function behaviors()
{
'access' => [
'class' => AccessControl::className(),
'ruleConfig' => [
'class' => AccessRule::className(),
],
'rules' => [
[
'actions' => ['update'],
'allow' => true,
'roles' => ['manager'],
'matchCallback' => function ($rule, $action,$model) {
return Yii::$app->user->identity->id==$model->autor_id;}
],
]
]
}
Answer the question
In order to leave comments, you need to log in
I think you can already use RBAC
And one more thing. Your problem is solved there
[
'allow' => true,
'actions' => ['update'],
'roles' => ['updatePost'],
'roleParams' => function() {
return ['post' => Post::findOne(Yii::$app->request->get('id'))];
},
],
[
'allow' => true,
'actions' => ['update'],
'roles' => ['updatePost'],
'roleParams' => ['postId' => Yii::$app->request->get('id')];
],
You can make the getModel() method in the controller, which will cache the result (put it in a property). then in the closure you can do $this->getModel()->autor_id.
The problem is that the filters run up to the main action.
but thanks for this solution Post::findOne(Yii::$app->request->get('id')) in principle, that's why I'm assigning the answer.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question