fix: [galaxyCluster:CRUD] Fixed fields not being saved correctly and improved API feedback

pull/6120/head
mokaddem 2020-06-05 11:00:02 +02:00
parent 17d12c6ffa
commit 28252e1d1a
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
3 changed files with 90 additions and 29 deletions

View File

@ -250,9 +250,6 @@ class GalaxyClustersController extends AppController
if (!empty($origCluster)) {
$origCluster = $origCluster[0];
$origClusterMeta = $origCluster['GalaxyCluster'];
$forkVersion = $origCluster['GalaxyCluster']['version'];
$this->set('forkUuid', $forkUuid);
$this->set('forkVersion', $forkVersion);
if (empty($this->request->data)) {
$this->request->data = $origCluster;
unset($this->request->data['GalaxyCluster']['id']);
@ -261,6 +258,8 @@ class GalaxyClustersController extends AppController
unset($origCluster['GalaxyElement'][$k]['id']);
unset($origCluster['GalaxyElement'][$k]['galaxy_cluster_id']);
}
$this->request->data['GalaxyCluster']['extends_uuid'] = $origCluster['GalaxyCluster']['uuid'];
$this->request->data['GalaxyCluster']['extends_version'] = $origCluster['GalaxyCluster']['version'];
$this->request->data['GalaxyCluster']['elements'] = json_encode($origCluster['GalaxyElement']);
$this->request->data['GalaxyCluster']['elementsDict'] = $origCluster['GalaxyElement'];
$this->request->data['GalaxyCluster']['authors'] = json_encode($origCluster['GalaxyCluster']['authors']);
@ -275,21 +274,29 @@ class GalaxyClustersController extends AppController
$cluster = $this->request->data;
$errors = array();
if (empty($cluster['GalaxyCluster']['elements'])) {
$galaxy['Galaxy']['values'] = array();
$cluster['GalaxyCluster']['elements'] = array();
} else {
$decoded = json_decode($cluster['GalaxyCluster']['elements'], true);
if ($decoded === null) {
$decoded = array();
if (is_null($decoded)) {
$this->GalaxyCluster->validationErrors['values'][] = __('Invalid JSON');
$errors[] = sprintf(__('Invalid JSON'));
}
$galaxy['Galaxy']['elements'] = $decoded;
$cluster['GalaxyCluster']['elements'] = $decoded;
}
$extendId = $this->Toolbox->findIdByUuid($this->GalaxyCluster, $cluster['GalaxyCluster']['forkUuid']);
$extendedCluster = $this->GalaxyCluster->fetchGalaxyClusters(
$this->Auth->user(),
array('conditions' => array('GalaxyCluster.id' => $extendId))
);
if (!empty($extendedCluster)) {
$cluster['GalaxyCluster']['extends_uuid'] = $extendedCluster[0]['GalaxyCluster']['uuid'];
if (!empty($cluster['GalaxyCluster']['extends_uuid'])) {
$extendId = $this->Toolbox->findIdByUuid($this->GalaxyCluster, $cluster['GalaxyCluster']['extends_uuid']);
$extendedCluster = $this->GalaxyCluster->fetchGalaxyClusters(
$this->Auth->user(),
array('conditions' => array('GalaxyCluster.id' => $extendId))
);
if (!empty($extendedCluster)) {
$cluster['GalaxyCluster']['extends_uuid'] = $extendedCluster[0]['GalaxyCluster']['uuid'];
if (empty($cluster['GalaxyCluster']['extends_version'])) {
$cluster['GalaxyCluster']['extends_version'] = $extendedCluster[0]['GalaxyCluster']['version'];
}
} else {
$cluster['GalaxyCluster']['extends_uuid'] = '';
}
} else {
$cluster['GalaxyCluster']['extends_uuid'] = '';
}
@ -302,11 +309,22 @@ class GalaxyClustersController extends AppController
$errors[] = $validationError;
}
}
if (!empty($errors)) {
$flashErrorMessage = implode(', ', implode(' ', $errors));
$this->Flash->error($flashErrorMessage);
$message = implode(', ', implode(' ', $errors));
if ($this->_isRest()) {
return $this->RestResponse->saveFailResponse('GalaxyCluster', 'add', $this->GalaxyCluster->id, $message, $this->response->type());
} else {
$this->Flash->error($message);
}
} else {
$this->redirect(array('controller' => 'galaxy_clusters', 'action' => 'view', $this->GalaxyCluster->id));
$message = __('Galaxy cluster saved');
if ($this->_isRest()) {
return $this->RestResponse->saveSuccessResponse('GalaxyCluster', 'add', $this->GalaxyCluster->id, $this->response->type());
} else {
$this->Flash->success($message);
$this->redirect(array('controller' => 'galaxy_clusters', 'action' => 'view', $this->GalaxyCluster->id));
}
}
}
$this->set('galaxy_id', $galaxyId);
@ -394,17 +412,32 @@ class GalaxyClustersController extends AppController
}
$cluster['GalaxyCluster']['authors'] = json_encode($cluster['GalaxyCluster']['authors']);
if (!empty($errors)) {
$flashErrorMessage = implode(', ', $errors);
$this->Flash->error($flashErrorMessage);
$message = implode(', ', implode(' ', $errors));
if ($this->_isRest()) {
return $this->RestResponse->saveFailResponse('GalaxyCluster', 'edit', $cluster['GalaxyCluster']['id'], $message, $this->response->type());
} else {
$this->Flash->error($message);
}
} else {
$errors = $this->GalaxyCluster->editCluster($this->Auth->user(), $cluster);
if (!empty($errors)) {
$flashErrorMessage = implode(', ', $errors);
$this->Flash->error($flashErrorMessage);
$message = implode(', ', implode(' ', $errors));
if ($this->_isRest()) {
return $this->RestResponse->saveFailResponse('GalaxyCluster', 'edit', $cluster['GalaxyCluster']['id'], $message, $this->response->type());
} else {
$this->Flash->error($message);
}
} else {
$this->redirect(array('controller' => 'galaxy_clusters', 'action' => 'view', $id));
$message = __('Galaxy cluster saved');
if ($this->_isRest()) {
return $this->RestResponse->saveSuccessResponse('GalaxyCluster', 'edit', $cluster['GalaxyCluster']['id'], $this->response->type());
} else {
$this->Flash->success($message);
$this->redirect(array('controller' => 'galaxy_clusters', 'action' => 'view', $this->GalaxyCluster->id));
}
}
}
} else {
$this->GalaxyCluster->data['GalaxyCluster']['elements'] = json_encode($this->GalaxyCluster->data['GalaxyElement']);
$this->GalaxyCluster->data['GalaxyCluster']['elementsDict'] = $this->GalaxyCluster->data['GalaxyElement'];

View File

@ -178,12 +178,37 @@ class GalaxyCluster extends AppModel
}
if (!isset($cluster['GalaxyCluster']['orgc_id'])) {
if (isset($cluster['Orgc']['uuid'])) {
$orgc_id = $this->Orgc->find('first', array('conditions' => array('Orgc.uuid' => $user['Orgc']['uuid']), 'fields' => array('Orgc.id'), 'recursive' => -1));
$orgc_id = $this->Orgc->find('first', array('conditions' => array('Orgc.uuid' => $cluster['Orgc']['uuid']), 'fields' => array('Orgc.id'), 'recursive' => -1));
} else {
$orgc_id = $user['org_id'];
}
$cluster['GalaxyCluster']['orgc_id'] = $orgc_id;
}
if (!isset($cluster['GalaxyCluster']['org_id'])) {
if (isset($cluster['Org']['uuid'])) {
$org_id = $this->Org->find('first', array('conditions' => array('Org.uuid' => $cluster['Org']['uuid']), 'fields' => array('Org.id'), 'recursive' => -1));
} else {
$org_id = $user['org_id'];
}
$cluster['GalaxyCluster']['org_id'] = $org_id;
}
if (
$cluster['GalaxyCluster']['orgc_id'] === $user['org_id'] ||
($user['Role']['perm_sync'] && $existingCluster['GalaxyCluster']['locked']) ||
$user['Role']['perm_site_admin']
) {
if ($user['Role']['perm_sync']) {
if (isset($cluster['GalaxyCluster']['distribution']) && $cluster['GalaxyCluster']['distribution'] == 4 && !$this->SharingGroup->checkIfAuthorised($user, $cluster['GalaxyCluster']['sharing_group_id'])) {
// $errors[] = array(__('Galaxy Cluster could not be saved: The sync user has to have access to the sharing group in order to be able to edit it.'));
return false;
}
}
} else {
// $errors[] = array(__('Galaxy Cluster could not be saved: The user used to edit the cluster is not authorised to do so. This can be caused by the user not being of the same organisation as the original creator of the cluster whilst also not being a site administrator.'));
return false;
}
$cluster['GalaxyCluster']['type'] = $galaxy['type'];
if (!$fromPull) {
$date = new DateTime();
@ -197,7 +222,11 @@ class GalaxyCluster extends AppModel
'conditions' => array('id' => $this->id),
'recursive' => -1
));
$this->GalaxyElement->updateElements(-1, $savedCluster['GalaxyCluster']['id'], $cluster['GalaxyCluster']['elements']);
$elementsToSave = array();
foreach ($cluster['GalaxyCluster']['elements'] as $element) { // transform cluster into Galaxy meta format
$elementsToSave[$element['key']][] = $element['value'];
}
$this->GalaxyElement->updateElements(-1, $savedCluster['GalaxyCluster']['id'], $elementsToSave);
}
return $saveSuccess;
}
@ -244,7 +273,7 @@ class GalaxyCluster extends AppModel
if ($saveSuccess) {
$elementsToSave = array();
foreach ($cluster['GalaxyCluster']['elements'] as $element) { // transform cluster into Galaxy meta format
$elementsToSave[$element['key']] = $element['value'];
$elementsToSave[$element['key']][] = $element['value'];
}
$this->GalaxyElement->updateElements($cluster['GalaxyCluster']['id'], $cluster['GalaxyCluster']['id'], $elementsToSave);
} else {
@ -334,6 +363,7 @@ class GalaxyCluster extends AppModel
$existingGalaxyCluster = $this->find('first', array('conditions' => array(
'GalaxyCluster.uuid' => $cluster['GalaxyCluster']['uuid']
)));
$cluster['GalaxyCluster']['tag_name'] = sprintf('misp-galaxy:%s="%s"', $cluster['GalaxyCluster']['type'], $cluster['GalaxyCluster']['uuid']);
if (empty($existingGalaxyCluster)) {
$this->create();
$saveSuccess = $this->save($cluster);

View File

@ -46,14 +46,12 @@
'default' => $galaxy_id
),
array(
'field' => 'forkUuid',
'field' => 'extends_uuid',
'type' => 'hidden',
'default' => isset($forkUuid) ? $forkUuid : ''
),
array(
'field' => 'forkVersion',
'field' => 'extends_version',
'type' => 'hidden',
'default' => isset($forkVersion) ? $forkVersion : ''
),
array(
'field' => 'description',