Merge branch 'develop' into add-integration-tests

pull/80/head
Luciano Righetti 2022-01-18 16:11:23 +01:00
commit 6e31005d79
2 changed files with 56 additions and 44 deletions

View File

@ -442,6 +442,12 @@ class CRUDComponent extends Component
if (empty($data)) { if (empty($data)) {
throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias)); throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias));
} }
if (isset($params['beforeSave'])) {
$data = $params['beforeSave']($data);
if ($data === false) {
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));
}
}
$this->Controller->set('id', $data['id']); $this->Controller->set('id', $data['id']);
$this->Controller->set('data', $data); $this->Controller->set('data', $data);
$this->Controller->set('bulkEnabled', false); $this->Controller->set('bulkEnabled', false);
@ -453,6 +459,7 @@ class CRUDComponent extends Component
$isBulk = count($ids) > 1; $isBulk = count($ids) > 1;
$bulkSuccesses = 0; $bulkSuccesses = 0;
foreach ($ids as $id) { foreach ($ids as $id) {
$skipExecution = false;
$data = $this->Table->find()->where([$this->Table->getAlias() . '.id' => $id]); $data = $this->Table->find()->where([$this->Table->getAlias() . '.id' => $id]);
if (!empty($params['conditions'])) { if (!empty($params['conditions'])) {
$data->where($params['conditions']); $data->where($params['conditions']);
@ -460,15 +467,24 @@ class CRUDComponent extends Component
if (!empty($params['contain'])) { if (!empty($params['contain'])) {
$data->contain($params['contain']); $data->contain($params['contain']);
} }
$data = $data->first(); if (isset($params['beforeSave'])) {
if (!empty($data)) { $data = $params['beforeSave']($data);
$success = $this->Table->delete($data); if ($data === false) {
$success = true; $skipExecution = true;
} else { $success = false;
$success = false; }
} }
if ($success) { if (!$skipExecution) {
$bulkSuccesses++; $data = $data->first();
if (!empty($data)) {
$success = $this->Table->delete($data);
$success = true;
} else {
$success = false;
}
if ($success) {
$bulkSuccesses++;
}
} }
} }
$message = $this->getMessageBasedOnResult( $message = $this->getMessageBasedOnResult(

View File

@ -57,47 +57,45 @@ class EncryptionKeysController extends AppController
private function buildBeforeSave(array $params, $currentUser, array &$orgConditions, array &$individualConditions, array &$dropdownData): array private function buildBeforeSave(array $params, $currentUser, array &$orgConditions, array &$individualConditions, array &$dropdownData): array
{ {
$orgConditions = [ if (empty($currentUser['role']['perm_admin'])) {
'id' => $currentUser['organisation_id'] $orgConditions = [
]; 'id' => $currentUser['organisation_id']
if (empty($currentUser['role']['perm_org_admin'])) {
$individualConditions = [
'id' => $currentUser['individual_id']
]; ];
} if (empty($currentUser['role']['perm_org_admin'])) {
$params['beforeSave'] = function($entity) use($currentUser) { $individualConditions = [
if ($entity['owner_model'] === 'organisation') { 'id' => $currentUser['individual_id']
$entity['owner_id'] = $currentUser['organisation_id']; ];
} else { }
if ($currentUser['role']['perm_org_admin']) { $params['beforeSave'] = function($entity) use($currentUser) {
$this->loadModel('Alignments'); if ($entity['owner_model'] === 'organisation') {
$validIndividuals = $this->Alignments->find('list', [ if ($entity['owner_id'] !== $currentUser['organisation_id']) {
'keyField' => 'individual_id', throw new MethodNotAllowedException(__('Selected organisation cannot be linked by the current user.'));
'valueField' => 'id',
'conditions' => ['organisation_id' => $currentUser['organisation_id']]
])->toArray();
if (!isset($validIndividuals[$entity['owner_id']])) {
throw new MethodNotAllowedException(__('Selected individual cannot be linked by the current user.'));
} }
} else { } else {
if ($entity['owner_id'] !== $currentUser['id']) { if ($currentUser['role']['perm_org_admin']) {
throw new MethodNotAllowedException(__('Selected individual cannot be linked by the current user.')); $this->loadModel('Alignments');
$validIndividuals = $this->Alignments->find('list', [
'keyField' => 'individual_id',
'valueField' => 'id',
'conditions' => ['organisation_id' => $currentUser['organisation_id']]
])->toArray();
if (!isset($validIndividuals[$entity['owner_id']])) {
throw new MethodNotAllowedException(__('Selected individual cannot be linked by the current user.'));
}
} else {
if ($entity['owner_id'] !== $currentUser['id']) {
throw new MethodNotAllowedException(__('Selected individual cannot be linked by the current user.'));
}
} }
} }
} return $entity;
return $entity; };
}; }
$this->loadModel('Organisations'); $this->loadModel('Organisations');
$this->loadModel('Individuals'); $this->loadModel('Individuals');
$dropdownData = [ $dropdownData = [
'organisation' => $this->Organisations->find('list', [ 'organisation' => $this->Organisations->find('list')->order(['name' => 'asc'])->where($orgConditions)->all()->toArray(),
'sort' => ['name' => 'asc'], 'individual' => $this->Individuals->find('list')->order(['email' => 'asc'])->where($individualConditions)->all()->toArray()
'conditions' => $orgConditions
]),
'individual' => $this->Individuals->find('list', [
'sort' => ['email' => 'asc'],
'conditions' => $individualConditions
])
]; ];
return $params; return $params;
} }
@ -111,9 +109,7 @@ class EncryptionKeysController extends AppController
$params = [ $params = [
'redirect' => $this->referer() 'redirect' => $this->referer()
]; ];
if (empty($currentUser['role']['perm_admin'])) { $params = $this->buildBeforeSave($params, $currentUser, $orgConditions, $individualConditions, $dropdownData);
$params = $this->buildBeforeSave($params, $currentUser, $orgConditions, $individualConditions, $dropdownData);
}
$this->CRUD->add($params); $this->CRUD->add($params);
$responsePayload = $this->CRUD->getResponsePayload(); $responsePayload = $this->CRUD->getResponsePayload();
if (!empty($responsePayload)) { if (!empty($responsePayload)) {