fix: [error] when deleting a role that had users attached to it was cryptic, fixes #180
parent
1c8bcc045e
commit
d799214a41
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue