fix: [individuals:delete] Gracefully catches deletion of individuals associated to a user
parent
3ca6b68429
commit
acb66ac4a0
|
@ -851,6 +851,9 @@ class CRUDComponent extends Component
|
|||
$query->contain($params['contain']);
|
||||
}
|
||||
$data = $query->first();
|
||||
if (isset($params['afterFind'])) {
|
||||
$data = $params['afterFind']($data, $params);
|
||||
}
|
||||
if (empty($data)) {
|
||||
throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias));
|
||||
}
|
||||
|
@ -873,6 +876,9 @@ class CRUDComponent extends Component
|
|||
$query->contain($params['contain']);
|
||||
}
|
||||
$data = $query->first();
|
||||
if (isset($params['afterFind'])) {
|
||||
$data = $params['afterFind']($data, $params);
|
||||
}
|
||||
if (isset($params['beforeSave'])) {
|
||||
try {
|
||||
$data = $params['beforeSave']($data);
|
||||
|
|
|
@ -94,7 +94,16 @@ class IndividualsController extends AppController
|
|||
|
||||
public function delete($id)
|
||||
{
|
||||
$this->CRUD->delete($id);
|
||||
$params = [
|
||||
'contain' => ['Users'],
|
||||
'afterFind' => function($data, $params) {
|
||||
if (!empty($data['user'])) {
|
||||
throw new ForbiddenException(__('Individual associated to a user cannot be deleted.'));
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
];
|
||||
$this->CRUD->delete($id, $params);
|
||||
$responsePayload = $this->CRUD->getResponsePayload();
|
||||
if (!empty($responsePayload)) {
|
||||
return $responsePayload;
|
||||
|
|
|
@ -103,6 +103,9 @@ echo $this->element('genericElements/IndexTable/index_table', [
|
|||
'icon' => 'trash',
|
||||
'complex_requirement' => [
|
||||
'function' => function ($row, $options) use ($loggedUser) {
|
||||
if (!empty($row['user'])) { // cannot delete individuals with associated user(s)
|
||||
return false;
|
||||
}
|
||||
return (bool)$loggedUser['role']['perm_admin'];
|
||||
}
|
||||
]
|
||||
|
|
Loading…
Reference in New Issue