Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
checkDeleteAllowed & checkUpdateAllowed
  • Loading branch information
siggi-k committed Dec 18, 2024
commit e6d561df0c2179cf4ff427d8c008dde6a38cae6b
9 changes: 9 additions & 0 deletions src/actions/DeleteAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class DeleteAction extends JsonApiAction
*/
public $scenario = Model::SCENARIO_DEFAULT;

/**
* @var callable|null a PHP callable that checks if deletion is allowed.
*/
public $checkDeleteAllowed;

/**
* @var callable|Closure Callback after save model with all relations
* @example
Expand Down Expand Up @@ -80,6 +85,10 @@ public function run($id):void
call_user_func($this->checkAccess, $this->id, $model);
}

if ($this->checkDeleteAllowed) {
call_user_func($this->checkDeleteAllowed, $this->id, $model);
}

if ($model->delete() === false) {
throw new ServerErrorHttpException('Failed to delete the object for unknown reason.');
}
Expand Down
11 changes: 11 additions & 0 deletions src/actions/UpdateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ class UpdateAction extends JsonApiAction
* ```
*/
public $scenario = Model::SCENARIO_DEFAULT;

/**
* @var callable|null a PHP callable that checks if updating is allowed.
*/
public $checkUpdateAllowed;

/**
* @var callable|Closure Callback after save model with all relations
* @example
Expand All @@ -74,6 +80,7 @@ class UpdateAction extends JsonApiAction
* }
*/
public $afterSave = null;

/**
* @throws \yii\base\InvalidConfigException
*/
Expand Down Expand Up @@ -113,6 +120,10 @@ public function run($id):Item
call_user_func($this->checkAccess, $this->id, $model);
}

if ($this->checkUpdateAllowed) {
call_user_func($this->checkUpdateAllowed, $this->id, $model);
}

$originalModel = clone $model;
RelationshipManager::validateRelationships($model, $this->getResourceRelationships(), $this->allowedRelations);
if (empty($this->getResourceAttributes()) && $this->hasResourceRelationships()) {
Expand Down
Loading