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));
|
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) {
|
} catch (\Exception $e) {
|
||||||
|
$exceptionMessage = $e->getMessage();
|
||||||
$entity = false;
|
$entity = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1182,7 +1183,7 @@ class CRUDComponent extends Component
|
||||||
$isBulk,
|
$isBulk,
|
||||||
__('{0} deleted.', $this->ObjectAlias),
|
__('{0} deleted.', $this->ObjectAlias),
|
||||||
__('All selected {0} have been deleted.', Inflector::pluralize($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.',
|
'{0} / {1} {2} have been deleted.',
|
||||||
$bulkSuccesses,
|
$bulkSuccesses,
|
||||||
|
|
|
@ -78,7 +78,15 @@ class RolesController extends AppController
|
||||||
|
|
||||||
public function delete($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();
|
$responsePayload = $this->CRUD->getResponsePayload();
|
||||||
if (!empty($responsePayload)) {
|
if (!empty($responsePayload)) {
|
||||||
return $responsePayload;
|
return $responsePayload;
|
||||||
|
|
Loading…
Reference in New Issue