Skip to content

Commit

Permalink
fix: [error] when deleting a role that had users attached to it was c…
Browse files Browse the repository at this point in the history
…ryptic, fixes #180
  • Loading branch information
iglocska committed Nov 28, 2024
1 parent 1c8bcc0 commit d799214
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Controller/Component/CRUDComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,7 @@ public function delete($id = false, $params = []): void
throw new NotFoundException(__('Could not save {0} due to the input failing to meet expectations. Your input is bad and you should feel bad.', $this->ObjectAlias));
}
} catch (\Exception $e) {
$exceptionMessage = $e->getMessage();
$entity = false;
}
}
Expand All @@ -1182,7 +1183,7 @@ public function delete($id = false, $params = []): void
$isBulk,
__('{0} deleted.', $this->ObjectAlias),
__('All selected {0} have been deleted.', Inflector::pluralize($this->ObjectAlias)),
__('Could not delete {0}.', $this->ObjectAlias),
$exceptionMessage ?? __('Could not delete {0}.', $this->ObjectAlias),
__(
'{0} / {1} {2} have been deleted.',
$bulkSuccesses,
Expand Down
10 changes: 9 additions & 1 deletion src/Controller/RolesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,15 @@ public function edit($id)

public function delete($id)
{
$this->CRUD->delete($id);
$this->CRUD->delete($id, [
'beforeSave' => function ($data) {
$userCount = $this->Roles->Users->find()->where(['role_id' => $data['id']])->count();
if ($userCount > 0) {
throw new ForbiddenException(__('You cannot delete a role that has users assigned to it.'));
}
return true;
}
]);
$responsePayload = $this->CRUD->getResponsePayload();
if (!empty($responsePayload)) {
return $responsePayload;
Expand Down

0 comments on commit d799214

Please sign in to comment.