fix: [encryptions] fixed adding encryption keys

pull/79/head
iglocska 2021-12-01 15:24:08 +01:00
parent 332f374e01
commit 4c7dc85d0e
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
1 changed files with 8 additions and 5 deletions

View File

@ -62,16 +62,18 @@ class EncryptionKeysController extends AppController
'id' => $currentUser['individual_id'] 'id' => $currentUser['individual_id']
]; ];
} }
$params['beforeSave'] = function($entity) { $params['beforeSave'] = function($entity) use($currentUser) {
if ($entity['owner_model'] === 'organisation') { if ($entity['owner_model'] === 'organisation') {
$entity['owner_id'] = $currentUser['organisation_id']; $entity['owner_id'] = $currentUser['organisation_id'];
} else { } else {
if ($currentUser['role']['perm_org_admin']) { if ($currentUser['role']['perm_org_admin']) {
$validIndividuals = $this->Organisations->find('list', [ $this->loadModel('Alignments');
'fields' => ['distinct(id)'], $validIndividuals = $this->Alignments->find('list', [
'keyField' => 'individual_id',
'valueField' => 'id',
'conditions' => ['organisation_id' => $currentUser['organisation_id']] 'conditions' => ['organisation_id' => $currentUser['organisation_id']]
]); ])->toArray();
if (!in_array($entity['owner_id'], $validIndividuals)) { if (!isset($validIndividuals[$entity['owner_id']])) {
throw new MethodNotAllowedException(__('Selected individual cannot be linked by the current user.')); throw new MethodNotAllowedException(__('Selected individual cannot be linked by the current user.'));
} }
} else { } else {
@ -80,6 +82,7 @@ class EncryptionKeysController extends AppController
} }
} }
} }
return $entity;
}; };
} }
$this->CRUD->add($params); $this->CRUD->add($params);