Skip to content

Commit 8cbdbb2

Browse files
committed
changed signature of checkAccess callback
1 parent 87f9349 commit 8cbdbb2

17 files changed

+47
-19
lines changed

CHANGELOG.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,30 @@
3535
- Add support scenario for delete action
3636

3737
1.3.4
38-
- Add aftersave callback for create/update actions that called after save model with all relations
38+
- Add aftersave callback for create/update actions that called after save model with all relations
39+
40+
2.0.0
41+
- Changed signature of checkAccess callbacks, now whole action object passed, instead of action id
42+
Before:
43+
```php
44+
...
45+
'checkAccess' => function(string $action, $model = null) {
46+
if($action === 'create') {
47+
//...
48+
}
49+
}
50+
```
51+
52+
After:
53+
Before:
54+
55+
```php
56+
57+
...
58+
'checkAccess' => function(JsonApiAction $action, $model = null) {
59+
if($action->id === 'create') {
60+
//...
61+
}
62+
63+
}
64+
```

src/ActiveJsonApiController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use insolita\fractal\actions\CreateAction;
1111
use insolita\fractal\actions\DeleteAction;
1212
use insolita\fractal\actions\HasResourceTransformer;
13+
use insolita\fractal\actions\JsonApiAction;
1314
use insolita\fractal\actions\ListAction;
1415
use insolita\fractal\actions\UpdateAction;
1516
use insolita\fractal\actions\ViewAction;
@@ -103,12 +104,12 @@ public function actions()
103104
* to run the specified action against the specified data model.
104105
* If the user does not have access, a [[ForbiddenHttpException]] should be thrown.
105106
*
106-
* @param string $action the ID of the action to be executed
107+
* @param \insolita\fractal\actions\JsonApiAction $action an instance of executed action
107108
* @param object $model the model to be accessed. If null, it means no specific model is being accessed.
108109
* @param array $params additional parameters
109110
* @throws ForbiddenHttpException if the user does not have access
110111
*/
111-
public function checkAccess($action, $model = null, $params = [])
112+
public function checkAccess(JsonApiAction $action, $model = null, $params = [])
112113
{
113114
}
114115
}

src/actions/CountAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function init():void
6969
public function run()
7070
{
7171
if ($this->checkAccess) {
72-
call_user_func($this->checkAccess, $this->id);
72+
call_user_func($this->checkAccess, $this);
7373
}
7474

7575
$query = $this->makeQuery();

src/actions/CountForIdentityAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class CountForIdentityAction extends JsonApiAction
5858
public function run()
5959
{
6060
if ($this->checkAccess) {
61-
call_user_func($this->checkAccess, $this->id);
61+
call_user_func($this->checkAccess, $this);
6262
}
6363

6464
$query = $this->makeQuery();

src/actions/CreateAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function init():void
9393
public function run()
9494
{
9595
if ($this->checkAccess) {
96-
call_user_func($this->checkAccess, $this->id);
96+
call_user_func($this->checkAccess, $this);
9797
}
9898

9999
/* @var $model \yii\db\ActiveRecord */

src/actions/CreateRelationshipAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function run($id)
7777
{
7878
$model = $this->findModel($id);
7979
if ($this->checkAccess) {
80-
call_user_func($this->checkAccess, $this->id, $model);
80+
call_user_func($this->checkAccess, $this, $model);
8181
}
8282
$manager = new RelationshipManager($model, $this->relationName, $this->getResourceData(), $this->pkType);
8383
if ($this->idValidateCallback !== null) {

src/actions/DeleteAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function run($id):void
4848
$model = $this->isParentRestrictionRequired() ? $this->findModelForParent($id) : $this->findModel($id);
4949
$model->setScenario($this->scenario);
5050
if ($this->checkAccess) {
51-
call_user_func($this->checkAccess, $this->id, $model);
51+
call_user_func($this->checkAccess, $this, $model);
5252
}
5353
if ($model->delete() === false) {
5454
throw new ServerErrorHttpException('Failed to delete the object for unknown reason.');

src/actions/DeleteRelationshipAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function run($id):void
6060
{
6161
$model = $this->findModel($id);
6262
if ($this->checkAccess) {
63-
call_user_func($this->checkAccess, $this->id, $model);
63+
call_user_func($this->checkAccess, $this, $model);
6464
}
6565
$manager = new RelationshipManager($model, $this->relationName, $this->getResourceData(), $this->pkType);
6666
if ($this->idValidateCallback !== null) {

src/actions/JsonApiAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class JsonApiAction extends Action
6565
* if the current user has the permission to execute the action. If not set, the access
6666
* check will not be performed. The signature of the callable should be as follows,
6767
* ```php
68-
* function ($action, $model = null) {
68+
* function (JsonApiAction $action, $model = null) {
6969
* // $model is the requested model instance.
7070
* // If null, it means no specific model (e.g. IndexAction)
7171
* }

src/actions/ListAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function init():void
7474
public function run()
7575
{
7676
if ($this->checkAccess) {
77-
call_user_func($this->checkAccess, $this->id);
77+
call_user_func($this->checkAccess, $this);
7878
}
7979

8080
$dataProvider = $this->makeDataProvider();

0 commit comments

Comments
 (0)