Skip to content

Commit fe7317d

Browse files
authored
Merge pull request #10 from siggi-k/checkDeleteAllowed
checkDeleteAllowed & checkUpdateAllowed
2 parents 83eac62 + c7f54e3 commit fe7317d

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

Diff for: src/actions/DeleteAction.php

+24
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ class DeleteAction extends JsonApiAction
3737
*/
3838
public $scenario = Model::SCENARIO_DEFAULT;
3939

40+
/**
41+
* @var callable|null A PHP callable that will be called to determine
42+
* whether the deletion of a model is allowed. If not set, no deletion
43+
* check will be performed. The callable should have the following signature:
44+
*
45+
* @example
46+
* ```php
47+
* function ($action, $model) {
48+
* // $model is the model instance being deleted.
49+
*
50+
* // If the deletion is not allowed, an error should be thrown. For example:
51+
* if ($model->status !== 'draft') {
52+
* throw new MethodNotAllowedHttpException('The model can only be deleted if its status is "draft".');
53+
* }
54+
* }
55+
* ```
56+
*/
57+
public $checkDeleteAllowed;
58+
59+
4060
/**
4161
* @var callable|Closure Callback after save model with all relations
4262
* @example
@@ -80,6 +100,10 @@ public function run($id):void
80100
call_user_func($this->checkAccess, $this->id, $model);
81101
}
82102

103+
if ($this->checkDeleteAllowed) {
104+
call_user_func($this->checkDeleteAllowed, $this->id, $model);
105+
}
106+
83107
if ($model->delete() === false) {
84108
throw new ServerErrorHttpException('Failed to delete the object for unknown reason.');
85109
}

Diff for: src/actions/JsonApiAction.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,20 @@ class JsonApiAction extends Action
6161
public $findModel;
6262

6363
/**
64-
* @var callable a PHP callable that will be called when running an action to determine
65-
* if the current user has the permission to execute the action. If not set, the access
66-
* check will not be performed. The signature of the callable should be as follows,
64+
* @var callable|null A PHP callable that will be called when running an action to determine
65+
* whether the current user has permission to execute the action. If not set, no access
66+
* check will be performed. The callable should have the following signature:
67+
*
68+
* @example
6769
* ```php
6870
* function ($action, $model = null) {
6971
* // $model is the requested model instance.
70-
* // If null, it means no specific model (e.g. IndexAction)
72+
* // If null, it indicates no specific model (e.g., IndexAction).
73+
*
74+
* // If the user does not have the required permissions, an error should be thrown. For example:
75+
* if (!Yii::$app->user->can('admin')) {
76+
* throw new ForbiddenHttpException();
77+
* }
7178
* }
7279
* ```
7380
*/

Diff for: src/actions/UpdateAction.php

+25
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,26 @@ class UpdateAction extends JsonApiAction
6666
* ```
6767
*/
6868
public $scenario = Model::SCENARIO_DEFAULT;
69+
70+
/**
71+
* @var callable|null A PHP callable that will be called to determine
72+
* whether the update of a model is allowed. If not set, no update
73+
* check will be performed. The callable should have the following signature:
74+
*
75+
* @example
76+
* ```php
77+
* function ($action, $model) {
78+
* // $model is the model instance being updated.
79+
*
80+
* // If the update is not allowed, an error should be thrown. For example:
81+
* if ($model->status === 'archived') {
82+
* throw new MethodNotAllowedHttpException('The model cannot be updated when its status is "archived".');
83+
* }
84+
* }
85+
* ```
86+
*/
87+
public $checkUpdateAllowed;
88+
6989
/**
7090
* @var callable|Closure Callback after save model with all relations
7191
* @example
@@ -74,6 +94,7 @@ class UpdateAction extends JsonApiAction
7494
* }
7595
*/
7696
public $afterSave = null;
97+
7798
/**
7899
* @throws \yii\base\InvalidConfigException
79100
*/
@@ -113,6 +134,10 @@ public function run($id):Item
113134
call_user_func($this->checkAccess, $this->id, $model);
114135
}
115136

137+
if ($this->checkUpdateAllowed) {
138+
call_user_func($this->checkUpdateAllowed, $this->id, $model);
139+
}
140+
116141
$originalModel = clone $model;
117142
RelationshipManager::validateRelationships($model, $this->getResourceRelationships(), $this->allowedRelations);
118143
if (empty($this->getResourceAttributes()) && $this->hasResourceRelationships()) {

0 commit comments

Comments
 (0)