chg: [galaxyCluster:crud] Improved how clusters and their linked models

are saved
pull/6120/head
mokaddem 2020-06-09 10:08:36 +02:00
parent ea9e2fba77
commit 3314db6ac0
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
5 changed files with 76 additions and 93 deletions

View File

@ -108,7 +108,7 @@ class GalaxyClusterRelationsController extends AppController
}
if ($this->Auth->user()['Role']['perm_site_admin'] || $clusterSource['GalaxyCluster']['org_id'] != $this->Auth->user()['org_id']) {
$errors = $this->GalaxyClusterRelation->saveRelation($this->Auth->user(), $relation);
$errors = $this->GalaxyClusterRelation->saveRelation($this->Auth->user(), $clusterSource['GalaxyCluster'], $relation);
} else {
$errors = array(__('Only the owner organisation of the source cluster can use it as a source'));
}

View File

@ -206,8 +206,11 @@ class GalaxyClustersController extends AppController
throw new NotFoundException('Cluster not found.');
}
if ($this->_isRest()) {
$cluster['GalaxyCluster']['Galaxy'] = $cluster['Galaxy'];
$cluster['GalaxyCluster']['GalaxyElement'] = $cluster['GalaxyElement'];
$models = array('Galaxy', 'GalaxyElement', 'GalaxyClusterRelation', 'Org', 'Orgc', 'TargettingClusterRelation');
foreach ($models as $model) {
$cluster['GalaxyCluster'][$model] = $cluster[$model];
unset($cluster[$model]);
}
return $this->RestResponse->viewData($cluster, $this->response->type());
// return $this->RestResponse->viewData(array('GalaxyCluster' => $cluster['GalaxyCluster']), $this->response->type());
} else {
@ -272,16 +275,19 @@ class GalaxyClustersController extends AppController
}
if ($this->request->is('post') || $this->request->is('put')) {
$cluster = $this->request->data;
$cluster['GalaxyCluster']['galaxy_id'] = $galaxyId;
$errors = array();
if (empty($cluster['GalaxyCluster']['elements'])) {
$cluster['GalaxyCluster']['elements'] = array();
if (empty($cluster['GalaxyCluster']['GalaxyElement'])) {
$cluster['GalaxyCluster']['GalaxyElement'] = array();
}
} else {
$decoded = json_decode($cluster['GalaxyCluster']['elements'], true);
if (is_null($decoded)) {
$this->GalaxyCluster->validationErrors['values'][] = __('Invalid JSON');
$errors[] = sprintf(__('Invalid JSON'));
}
$cluster['GalaxyCluster']['elements'] = $decoded;
$cluster['GalaxyCluster']['GalaxyElement'] = $decoded;
}
if (!empty($cluster['GalaxyCluster']['extends_uuid'])) {
$extendId = $this->Toolbox->findIdByUuid($this->GalaxyCluster, $cluster['GalaxyCluster']['extends_uuid']);
@ -392,14 +398,14 @@ class GalaxyClustersController extends AppController
$cluster['GalaxyCluster']['id'] = $id;
}
if (empty($cluster['GalaxyCluster']['elements'])) {
$cluster['GalaxyCluster']['elements'] = array();
$cluster['GalaxyCluster']['GalaxyElement'] = array();
} else {
$decoded = json_decode($cluster['GalaxyCluster']['elements'], true);
if (is_null($decoded)) {
$this->GalaxyCluster->validationErrors['values'][] = __('Invalid JSON');
$errors[] = sprintf(__('Invalid JSON'));
}
$cluster['GalaxyCluster']['elements'] = $decoded;
$cluster['GalaxyCluster']['GalaxyElement'] = $decoded;
}
if (empty($cluster['GalaxyCluster']['authors'])) {
$cluster['GalaxyCluster']['authors'] = [];

View File

@ -110,7 +110,6 @@ class Galaxy extends AppModel
'tag_name' => 'misp-galaxy:' . $cluster_package['type'] . '="'
);
$elements = array();
$relations = array();
$temp = $this->GalaxyCluster->find('all', array(
'conditions' => array(
'GalaxyCluster.galaxy_id' => $galaxies[$cluster_package['type']]
@ -196,6 +195,7 @@ class Galaxy extends AppModel
}
}
if (isset($cluster['related'])) {
$relations = array();
foreach ($cluster['related'] as $key => $relation) {
array('', 'referenced_galaxy_cluster_uuid');
$relations[] = array(
@ -206,6 +206,9 @@ class Galaxy extends AppModel
'tags' => $relation['tags'],
);
}
if (!empty($relations)) {
$this->GalaxyCluster->GalaxyClusterRelation->saveRelations($tempUser, $cluster, $relations, $capture=true, $force=true);
}
}
}
$db = $this->getDataSource();
@ -214,8 +217,6 @@ class Galaxy extends AppModel
$db->insertMulti('galaxy_elements', $fields, $elements);
}
$tempUser = array('Role' => array('perm_galaxy_editor' => 1, 'perm_tag_editor' => 1, 'perm_site_admin' => 1)); // only site-admin are authorized to update galaxies
// $this->GalaxyCluster->GalaxyClusterRelation->addRelations($tempUser, $relations, $capture=true);
$this->GalaxyCluster->GalaxyClusterRelation->saveRelation($tempUser, $relations, $capture=true, $force=true);
}
return true;
}

View File

@ -136,10 +136,7 @@ class GalaxyCluster extends AppModel
$cluster = $this->data['GalaxyCluster'];
$this->GalaxyClusterRelation->updateAll(
array('GalaxyClusterRelation.referenced_galaxy_cluster_id' => $cluster['id']),
array(
'GalaxyClusterRelation.referenced_galaxy_cluster_uuid' => $cluster['uuid'],
'GalaxyClusterRelation.referenced_galaxy_cluster_id' => 0,
)
array('GalaxyClusterRelation.referenced_galaxy_cluster_uuid' => $cluster['uuid'])
);
}
@ -149,9 +146,7 @@ class GalaxyCluster extends AppModel
$cluster = $this->data['GalaxyCluster'];
$this->GalaxyClusterRelation->updateAll(
array('GalaxyClusterRelation.referenced_galaxy_cluster_id' => 0),
array(
'GalaxyClusterRelation.referenced_galaxy_cluster_uuid' => $cluster['uuid'],
)
array('GalaxyClusterRelation.referenced_galaxy_cluster_uuid' => $cluster['uuid'])
);
}
@ -162,7 +157,7 @@ class GalaxyCluster extends AppModel
}
// Respecting ACL, save a cluster, its elements and set correct fields
public function saveCluster($user, $cluster, $fromPull=false, $allowEdit=false)
public function saveCluster($user, $cluster, $allowEdit=false)
{
if (!$user['Role']['perm_galaxy_editor'] && !$user['Role']['perm_site_admin']) {
return false;
@ -183,8 +178,8 @@ class GalaxyCluster extends AppModel
if ($existingGalaxyCluster['GalaxyCluster']['galaxy_id'] != $galaxy['id']) { // cluster already exists in another galaxy
return false;
}
if ($fromPull && !$existingGalaxyCluster['GalaxyCluster']['default'] && $allowEdit) {
$errors = $this->editCluster($user, $cluster, $fromPull);
if (!$existingGalaxyCluster['GalaxyCluster']['default'] && $allowEdit) {
$errors = $this->editCluster($user, $cluster);
return empty($errors);
} else {
// Maybe redirect to the correct URL?
@ -198,6 +193,7 @@ class GalaxyCluster extends AppModel
if (!empty($forkedCluster) && $forkedCluster['GalaxyCluster']['galaxy_id'] != $galaxy['id']) {
return false; // cluster forks always have to belong to the same galaxy as the parent
}
$cluster['GalaxyCluster']['org_id'] = $user['Organisation']['id'];
if (!isset($cluster['GalaxyCluster']['orgc_id'])) {
if (isset($cluster['Orgc']['uuid'])) {
$orgc_id = $this->Orgc->find('first', array('conditions' => array('Orgc.uuid' => $cluster['Orgc']['uuid']), 'fields' => array('Orgc.id'), 'recursive' => -1));
@ -207,32 +203,15 @@ class GalaxyCluster extends AppModel
$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'];
if ($user['Role']['perm_sync']) {
if (isset($cluster['GalaxyCluster']['distribution']) && $cluster['GalaxyCluster']['distribution'] == 4 && !$this->SharingGroup->checkIfAuthorised($user, $cluster['GalaxyCluster']['sharing_group_id'])) {
// The sync user has to have access to the sharing group in order to be able to edit it
return false;
}
$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) {
if (!isset($cluster['GalaxyCluster']['version'])) {
$date = new DateTime();
$cluster['GalaxyCluster']['version'] = $date->getTimestamp();
}
@ -244,16 +223,22 @@ class GalaxyCluster extends AppModel
'conditions' => array('id' => $this->id),
'recursive' => -1
));
$elementsToSave = array();
foreach ($cluster['GalaxyCluster']['elements'] as $element) { // transform cluster into Galaxy meta format
$elementsToSave[$element['key']][] = $element['value'];
if (!empty($cluster['GalaxyCluster']['GalaxyElement'])) {
$elementsToSave = array();
foreach ($cluster['GalaxyCluster']['GalaxyElement'] as $element) { // transform cluster into Galaxy meta format
$elementsToSave[$element['key']][] = $element['value'];
}
$this->GalaxyElement->updateElements(-1, $savedCluster['GalaxyCluster']['id'], $elementsToSave);
}
if (!empty($cluster['GalaxyCluster']['GalaxyClusterRelation'])) {
$this->GalaxyClusterRelation->saveRelations($user, $cluster['GalaxyCluster'], $cluster['GalaxyCluster']['GalaxyClusterRelation'], $capture=true);
}
$this->GalaxyElement->updateElements(-1, $savedCluster['GalaxyCluster']['id'], $elementsToSave);
}
return $saveSuccess;
}
public function editCluster($user, $cluster, $fromPull = false, $fieldList = array(), $deleteOldElements=true)
public function editCluster($user, $cluster, $fieldList = array(), $deleteOldElements=true)
{
$this->SharingGroup = ClassRegistry::init('SharingGroup');
$errors = array();
@ -284,20 +269,24 @@ class GalaxyCluster extends AppModel
if (empty($errors)) {
$date = new DateTime();
if (!$fromPull) {
$cluster['GalaxyCluster']['version'] = $date->getTimestamp();
}
$cluster['GalaxyCluster']['version'] = $date->getTimestamp();
$cluster['GalaxyCluster']['default'] = false;
if (empty($fieldList)) {
$fieldList = array('value', 'description', 'version', 'source', 'authors', 'distribution', 'sharing_group_id', 'default');
}
$saveSuccess = $this->save($cluster, array('fieldList' => $fieldList));
if ($saveSuccess) {
$elementsToSave = array();
foreach ($cluster['GalaxyCluster']['elements'] as $element) { // transform cluster into Galaxy meta format
$elementsToSave[$element['key']][] = $element['value'];
if (!empty($cluster['GalaxyCluster']['GalaxyElement'])) {
$elementsToSave = array();
foreach ($cluster['GalaxyCluster']['GalaxyElement'] as $element) { // transform cluster into Galaxy meta format
$elementsToSave[$element['key']][] = $element['value'];
}
$this->GalaxyElement->updateElements($cluster['GalaxyCluster']['id'], $cluster['GalaxyCluster']['id'], $elementsToSave, $delete=$deleteOldElements);
}
$this->GalaxyElement->updateElements($cluster['GalaxyCluster']['id'], $cluster['GalaxyCluster']['id'], $elementsToSave, $delete=$deleteOldElements);
if (!empty($cluster['GalaxyClusterRelation'])) {
$this->GalaxyClusterRelation->saveRelations($user, $cluster['GalaxyCluster'], $cluster['GalaxyClusterRelation'], $capture=true, $force=true);
}
} else {
foreach($this->validationErrors as $validationError) {
$errors[] = $validationError[0];

View File

@ -139,41 +139,6 @@ class GalaxyClusterRelation extends AppModel
$this->deleteAll($conditions, false, false);
}
// public function addRelations($user, $relations, $capture=false)
// {
// $fieldList = array(
// 'galaxy_cluster_uuid',
// 'referenced_galaxy_cluster_uuid',
// 'referenced_galaxy_cluster_type',
// 'default',
// 'distribution',
// 'sharing_group_id',
// );
// foreach ($relations as $k => $relation) {
// $sourceCluster = $this->SourceCluster->fetchGalaxyClusters($user, array('conditions' => array('uuid' => $relation['galaxy_cluster_uuid'])));
// if (empty($sourceCluster)) {
// throw new NotFoundException(__('Invalid galaxy cluster'));
// }
// $relation['galaxy_cluster_id'] = $sourceCluster['GalaxyCluster']['id'];
// if (!isset($relation['referenced_galaxy_cluster_uuid'])) {
// $targetCluster = $this->TargetCluster->fetchGalaxyClusters($user, array('conditions' => array('uuid' => $relation['referenced_galaxy_cluster_uuid'])));
// if (!empty($targetCluster)) { // do not save the relation if referenced cluster does not exists
// $targetCluster = $targetCluster[0];
// $relation['referenced_galaxy_cluster_uuid'] = $targetCluster['GalaxyCluster']['uuid'];
// $relation['referenced_galaxy_cluster_id'] = $targetCluster['GalaxyCluster']['id'];
// $this->create();
// $saveResult = $this->save($relation, array('fieldList' => $fieldList));
// if ($saveResult) {
// $savedId = $this->id;
// $this->GalaxyClusterRelationTag->attachTags($user, $savedId, $relation['tags'], $capture=$capture);
// }
// } else {
// throw new NotFoundException(__('Invalid referenced galaxy cluster'));
// }
// }
// }
// }
public function massageRelationTag($cluster)
{
if (!empty($cluster['GalaxyClusterRelation'])) {
@ -199,13 +164,27 @@ class GalaxyClusterRelation extends AppModel
return $cluster;
}
public function saveRelation($user, $relation, $capture=false, $force=false)
public function saveRelations($user, $cluster, $relations, $capture=false, $force=false)
{
$errors = array();
foreach($relations as $k => $relation) {
$saveResult = $this->saveRelation($user, $cluster, $relation, $capture=$capture, $force=$force);
$errors = array_merge($errors, $saveResult);
}
return $errors;
}
public function saveRelation($user, $cluster, $relation, $capture=false, $force=false)
{
$errors = array();
if (!$user['Role']['perm_galaxy_editor'] && !$user['Role']['perm_site_admin']) {
$errors[] = __('Incorrect permission');
return $errors;
}
if (!isset($relation['GalaxyClusterRelation']) && !empty($relation)) {
$relation = array('GalaxyClusterRelation' => $relation);
}
$relation['GalaxyClusterRelation']['galaxy_cluster_uuid'] = $cluster['uuid'];
if (!empty($relation['GalaxyClusterRelation']['tags'])) {
$tags = explode(',', $relation['GalaxyClusterRelation']['tags']);
@ -236,9 +215,17 @@ class GalaxyClusterRelation extends AppModel
'conditions' => array('id' => $this->id),
'recursive' => -1
));
$tagSaveResults = $this->GalaxyClusterRelationTag->attachTags($user, $this->id, $relation['GalaxyClusterRelation']['tags'], $capture=$capture);
if (!$tagSaveSuccess) {
$errors[] = __('Tags could not be saved');
$tags = array();
if (!empty($relation['GalaxyClusterRelation']['tags'])) {
$tags = $relation['GalaxyClusterRelation']['tags'];
} elseif (!empty($relation['GalaxyClusterRelation']['GalaxyClusterRelationTag'])) {
$tags = $relation['GalaxyClusterRelation']['GalaxyClusterRelationTag'];
}
if (!empty($tags)) {
$tagSaveResults = $this->GalaxyClusterRelationTag->attachTags($user, $this->id, $tags, $capture=$capture);
if (!$tagSaveResults) {
$errors[] = __('Tags could not be saved');
}
}
}
}