fix: [error] when deleting a role that had users attached to it was cryptic, fixes #180

pull/196/head
iglocska 2024-11-28 17:13:32 +01:00
parent 1c8bcc045e
commit d799214a41
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
2 changed files with 11 additions and 2 deletions

View File

@ -1165,6 +1165,7 @@ class CRUDComponent extends Component
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;
}
}
@ -1182,7 +1183,7 @@ class CRUDComponent extends Component
$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,

View File

@ -78,7 +78,15 @@ class RolesController extends AppController
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;