Merge branch 'develop'

pull/170/merge
iglocska 2024-08-28 16:19:28 +02:00
commit 52e80af061
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
3 changed files with 71 additions and 8 deletions

View File

@ -432,10 +432,10 @@ class CRUDComponent extends Component
if ($metaFieldsEnabled) { if ($metaFieldsEnabled) {
$metaTemplates = $this->getMetaTemplates(); $metaTemplates = $this->getMetaTemplates();
$data = $this->attachMetaTemplatesIfNeeded($data, $metaTemplates->toArray()); $data = $this->attachMetaTemplatesIfNeeded($data, $metaTemplates->toArray());
}
if (isset($params['afterFind'])) { if (isset($params['afterFind'])) {
$data = $params['afterFind']($data, $params); $data = $params['afterFind']($data, $params);
} }
}
if ($this->request->is('post')) { if ($this->request->is('post')) {
$patchEntityParams = [ $patchEntityParams = [
'associated' => [], 'associated' => [],
@ -458,6 +458,9 @@ class CRUDComponent extends Component
$massagedData = $this->massageMetaFields($data, $input, $metaTemplates); $massagedData = $this->massageMetaFields($data, $input, $metaTemplates);
unset($input['MetaTemplates']); // Avoid MetaTemplates to be overriden when patching entity unset($input['MetaTemplates']); // Avoid MetaTemplates to be overriden when patching entity
$data = $massagedData['entity']; $data = $massagedData['entity'];
if (isset($input['meta_fields'])) {
unset($input['meta_fields']);
}
} }
$data = $this->Table->patchEntity($data, $input, $patchEntityParams); $data = $this->Table->patchEntity($data, $input, $patchEntityParams);
if (isset($params['beforeSave'])) { if (isset($params['beforeSave'])) {
@ -568,10 +571,35 @@ class CRUDComponent extends Component
$this->Table->saveMetaFields($id, $input, $this->Table); $this->Table->saveMetaFields($id, $input, $this->Table);
} }
// prune empty values and marshall fields private function handleSimpleMetaFieldInput($allMetaTemplates, $input, &$cleanupMetaFields)
public function massageMetaFields($entity, $input, $allMetaTemplates = [])
{ {
if (empty($input['MetaTemplates']) || !$this->metaFieldsSupported()) { $input['MetaTemplates'] = [];
foreach ($input['meta_fields'] as $metaField) {
foreach ($allMetaTemplates as $metaTemplate) {
if ($metaTemplate->uuid != $metaField['template_uuid'] || $metaTemplate->version != $metaField['template_version']) {
continue;
}
foreach ($metaTemplate['meta_template_fields'] as $mtp) {
if ($mtp->field == $metaField['field']) {
// I feel dirty. I'm sorry.
$input['MetaTemplates'][$metaTemplate->id]['meta_template_fields'][$mtp->id]['metaFields']['new'][] = $metaField['value'];
$cleanupMetaFields[] = [
'scope' => $this->Table->getBehavior('MetaFields')->getScope(),
'field' => $mtp->field,
'meta_template_field_id' => $mtp->id,
'meta_template_id' => $metaTemplate->id
];
}
}
}
}
return $input;
}
// prune empty values and marshall fields
public function massageMetaFields($entity, $input, $allMetaTemplates = [], &$cleanupMetaFields = [])
{
if ((empty($input['MetaTemplates']) && !isset($input['meta_fields'])) || !$this->metaFieldsSupported()) {
return ['entity' => $entity, 'metafields_to_delete' => []]; return ['entity' => $entity, 'metafields_to_delete' => []];
} }
@ -580,6 +608,11 @@ class CRUDComponent extends Component
if (empty($metaTemplates)) { if (empty($metaTemplates)) {
$allMetaTemplates = $this->getMetaTemplates()->toArray(); $allMetaTemplates = $this->getMetaTemplates()->toArray();
} }
// handle simpler format of meta_templates via the API
// the idea is that integrators shouldn't have to mimic the complex format used by the UI.
if (isset($input['meta_fields'])) {
$input = $this->handleSimpleMetaFieldInput($allMetaTemplates, $input, $cleanupMetaFields);
}
if (!empty($entity->meta_fields)) { if (!empty($entity->meta_fields)) {
foreach ($entity->meta_fields as $i => $metaField) { foreach ($entity->meta_fields as $i => $metaField) {
$metaFieldsIndex[$metaField->id] = $i; $metaFieldsIndex[$metaField->id] = $i;
@ -738,11 +771,28 @@ class CRUDComponent extends Component
throw new NotFoundException(__('Could not save {0} due to the marshaling failing. Your input is bad and you should feel bad.', $this->ObjectAlias)); throw new NotFoundException(__('Could not save {0} due to the marshaling failing. Your input is bad and you should feel bad.', $this->ObjectAlias));
} }
} }
$cleanupMetaFields = [];
if ($metaFieldsEnabled) { if ($metaFieldsEnabled) {
$massagedData = $this->massageMetaFields($data, $input, $metaTemplates); $massagedData = $this->massageMetaFields($data, $input, $metaTemplates, $cleanupMetaFields);
if (!empty($cleanupMetaFields)) {
foreach ($cleanupMetaFields as $cleanupMetaField) {
$this->Table->MetaFields->deleteAll([
'AND' => [
'scope' => $cleanupMetaField['scope'],
'field' => $cleanupMetaField['field'],
'meta_template_field_id' => $cleanupMetaField['meta_template_field_id'],
'meta_template_id' => $cleanupMetaField['meta_template_id'],
'parent_id' => $id
]
]);
}
}
unset($input['MetaTemplates']); // Avoid MetaTemplates to be overriden when patching entity unset($input['MetaTemplates']); // Avoid MetaTemplates to be overriden when patching entity
$data = $massagedData['entity']; $data = $massagedData['entity'];
$metaFieldsToDelete = $massagedData['metafields_to_delete']; $metaFieldsToDelete = $massagedData['metafields_to_delete'];
if (isset($input['meta_fields'])) {
unset($input['meta_fields']);
}
} }
$data = $this->Table->patchEntity($data, $input, $patchEntityParams); $data = $this->Table->patchEntity($data, $input, $patchEntityParams);
if (isset($params['beforeSave'])) { if (isset($params['beforeSave'])) {

View File

@ -62,7 +62,16 @@ class IndividualsController extends AppController
public function add() public function add()
{ {
$this->CRUD->add(); $currentUser = $this->ACL->getUser();
$params = [
'afterSave' => function($data) use ($currentUser) {
if (empty($currentUser['role']['perm_community_admin'])) {
$this->Individuals->Alignments->setAlignment($currentUser['organisation_id'], $data->id, 'Member');
}
return $data;
}
];
$this->CRUD->add($params);
$responsePayload = $this->CRUD->getResponsePayload(); $responsePayload = $this->CRUD->getResponsePayload();
if (!empty($responsePayload)) { if (!empty($responsePayload)) {
return $responsePayload; return $responsePayload;

View File

@ -156,6 +156,10 @@ class UsersController extends AppController
if (Configure::read('keycloak.enabled')) { if (Configure::read('keycloak.enabled')) {
$this->Users->enrollUserRouter($data); $this->Users->enrollUserRouter($data);
} }
if ($data['individual_id']) {
$data['individual'] = $this->Users->Individuals->find('all')->where(['id' => $data['individual_id']])->contain(['Alignments' => 'Organisations'])->first();
}
return $data;
}, },
'afterFind' => function ($user, &$params) use ($currentUser) { 'afterFind' => function ($user, &$params) use ($currentUser) {
if (!empty($user)) { // We don't have a 404 if (!empty($user)) { // We don't have a 404