chg: Refacto bunch of galaxy clusters files

pull/6120/head
mokaddem 2020-07-07 08:16:46 +02:00
parent f7a3ed1db8
commit b1d9c8efaa
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
23 changed files with 99 additions and 86 deletions

View File

@ -85,9 +85,9 @@ class GalaxyClustersController extends AppController
$searchConditions['GalaxyCluster.galaxy_id'] = $id;
if ($this->_isRest()) {
$clusters = $this->Galaxy->find('all',
$clusters = $this->Galaxy->find(
'all',
array(
// 'recursive' => -1,
'conditions' => array(
'AND' => array($contextConditions, $searchConditions, $aclConditions)
),
@ -388,10 +388,10 @@ class GalaxyClustersController extends AppController
$cluster = array('GalaxyCluster' => $cluster);
}
$errors = array();
if (!isset($cluster['GalaxyCluster']['uuid'])) {
if (!isset($cluster['GalaxyCluster']['uuid'])) {
$cluster['GalaxyCluster']['uuid'] = $this->GalaxyCluster->data['GalaxyCluster']['uuid']; // freeze the uuid
}
if (!isset($cluster['GalaxyCluster']['id'])) {
if (!isset($cluster['GalaxyCluster']['id'])) {
$cluster['GalaxyCluster']['id'] = $id;
}
@ -445,7 +445,6 @@ class GalaxyClustersController extends AppController
}
}
}
} else {
$this->GalaxyCluster->data['GalaxyCluster']['elements'] = json_encode($this->GalaxyCluster->data['GalaxyElement']);
$this->GalaxyCluster->data['GalaxyCluster']['elementsDict'] = $this->GalaxyCluster->data['GalaxyElement'];
@ -739,7 +738,8 @@ class GalaxyClustersController extends AppController
}
}
public function viewGalaxyMatrix($id) {
public function viewGalaxyMatrix($id)
{
if (!$this->request->is('ajax')) {
throw new MethodNotAllowedException('This function can only be reached via AJAX.');
}
@ -882,9 +882,9 @@ class GalaxyClustersController extends AppController
$elements = array();
if (!empty($this->request->data['GalaxyCluster'])) {
foreach ($this->request->data['GalaxyCluster'] as $k => $jElement) {
$element = json_decode($jElement, true);
if (!is_null($element) && $element != 0) {
$elements[] = array(
$element = json_decode($jElement, true);
if (!is_null($element) && $element != 0) {
$elements[] = array(
'key' => $element['key'],
'value' => $element['value'],
);
@ -904,12 +904,12 @@ class GalaxyClustersController extends AppController
}
}
$missingElements = array();
forEach($parentCluster['GalaxyCluster']['GalaxyElement'] as $k => $parentElement) {
foreach ($parentCluster['GalaxyCluster']['GalaxyElement'] as $k => $parentElement) {
$found = false;
forEach($cluster['GalaxyCluster']['GalaxyElement'] as $k => $clusterElement) {
foreach ($cluster['GalaxyCluster']['GalaxyElement'] as $k => $clusterElement) {
if ($parentElement['key'] == $clusterElement['key'] &&
$parentElement['value'] == $clusterElement['value']) {
$found = true;
$found = true;
break; // element exists in parent
}
}

View File

@ -22,7 +22,7 @@
foreach ($clusters as $cluster) {
$cluster = $this->attachOwnerInsideCluster($cluster);
if (!empty($cluster['GalaxyCluster']['GalaxyClusterRelation'])) {
foreach($cluster['GalaxyCluster']['GalaxyClusterRelation'] as $relation) {
foreach ($cluster['GalaxyCluster']['GalaxyClusterRelation'] as $relation) {
$referencedClusterUuid = $relation['referenced_galaxy_cluster_uuid'];
if (!isset($this->lookup[$referencedClusterUuid])) {
$referencedCluster = $this->GalaxyCluster->fetchGalaxyClusters($this->user, array(
@ -71,7 +71,7 @@
)
));
if (!empty($referencingRelations)) {
foreach($referencingRelations as $relation) {
foreach ($referencingRelations as $relation) {
$referencingClusterUuid = $relation['GalaxyClusterRelation']['galaxy_cluster_uuid'];
if (!isset($this->lookup[$referencingClusterUuid])) {
$referencedCluster = $this->GalaxyCluster->fetchGalaxyClusters($this->user, array(

View File

@ -19,7 +19,7 @@
'children' => array()
));
// add relation info between the two clusters
foreach($cluster['GalaxyCluster']['GalaxyClusterRelation'] as $relation) {
foreach ($cluster['GalaxyCluster']['GalaxyClusterRelation'] as $relation) {
if (empty($relation['GalaxyCluster'])) { // unkown cluster, create placeholder
$relation['GalaxyCluster'] = array(
'uuid' => $relation['referenced_galaxy_cluster_uuid'],
@ -41,7 +41,7 @@
'children' => array()
));
if (!empty($cluster['GalaxyCluster']['TargetingClusterRelation'])) {
foreach($cluster['GalaxyCluster']['TargetingClusterRelation'] as $relation) {
foreach ($cluster['GalaxyCluster']['TargetingClusterRelation'] as $relation) {
if (isset($relation['GalaxyCluster'])) { // not set if Cluster is unkown
$tmp = array(
'Relation' => array_diff_key($relation, array_flip(array('GalaxyCluster'))),

View File

@ -64,14 +64,6 @@ class JSONConverterTool
}
}
// Replace is with a special key in galaxyCluster??
// if (isset($event['customGalaxy'])) {
// foreach($event['customGalaxy'] as $k => $customGalaxy) {
// unset($customGalaxy['Galaxy']['org_id']);
// $event['Event']['customGalaxy'][$k] = $customGalaxy;
// }
// }
//
// cleanup the array from things we do not want to expose
//

View File

@ -372,7 +372,8 @@ class Galaxy extends AppModel
return 'Could not attach the cluster';
}
public function detachCluster($user, $target_type, $target_id, $cluster_id) {
public function detachCluster($user, $target_type, $target_id, $cluster_id)
{
$cluster = $this->GalaxyCluster->find('first', array(
'recursive' => -1,
'conditions' => array('id' => $cluster_id),
@ -527,7 +528,6 @@ class Galaxy extends AppModel
if (!isset($galaxy['Galaxy']['kill_chain_order'])) {
throw new MethodNotAllowedException(__("Galaxy cannot be represented as a matrix"));
}
$matrixData = array(
'killChain' => $galaxy['Galaxy']['kill_chain_order'],
@ -594,21 +594,21 @@ class Galaxy extends AppModel
foreach (array_keys($tabs) as $i) {
foreach (array_keys($tabs[$i]) as $j) {
// major ordering based on score, minor based on alphabetical
usort($tabs[$i][$j], function ($a, $b) use($scores) {
usort($tabs[$i][$j], function ($a, $b) use ($scores) {
if ($a['tag_name'] == $b['tag_name']) {
return 0;
}
if (isset($scores[$a['tag_name']]) && isset($scores[$b['tag_name']])) {
if ($scores[$a['tag_name']] < $scores[$b['tag_name']]) {
$ret = 1;
} else if ($scores[$a['tag_name']] == $scores[$b['tag_name']]) {
} elseif ($scores[$a['tag_name']] == $scores[$b['tag_name']]) {
$ret = strcmp($a['value'], $b['value']);
} else {
$ret = -1;
}
} else if (isset($scores[$a['tag_name']])) {
} elseif (isset($scores[$a['tag_name']])) {
$ret = -1;
} else if (isset($scores[$b['tag_name']])) {
} elseif (isset($scores[$b['tag_name']])) {
$ret = 1;
} else { // none are set
$ret = strcmp($a['value'], $b['value']);
@ -664,7 +664,7 @@ class Galaxy extends AppModel
}
if ($pruneRootLeaves) {
foreach($tree as $i => $node) {
foreach ($tree as $i => $node) {
if (empty($node['children'])) {
unset($tree[$i]);
}
@ -677,5 +677,4 @@ class Galaxy extends AppModel
));
return $tree;
}
}

View File

@ -229,7 +229,7 @@ class GalaxyCluster extends AppModel
if ($existingGalaxyCluster) {
if ($existingGalaxyCluster['GalaxyCluster']['galaxy_id'] != $galaxy['id']) { // cluster already exists in another galaxy
$errors[] = __('Cluster already exists in another galaxy');
return $errors;
return $errors;
}
if (!$existingGalaxyCluster['GalaxyCluster']['default']) {
$errors[] = __('Edit not allowed on default clusters');
@ -252,7 +252,6 @@ class GalaxyCluster extends AppModel
$cluster['GalaxyCluster']['collection_uuid'] = '';
}
if (!empty($cluster['GalaxyCluster']['extends_uuid'])) {
$forkedCluster = $this->find('first', array('conditions' => array('GalaxyCluster.uuid' => $cluster['GalaxyCluster']['extends_uuid'])));
if (!empty($forkedCluster) && $forkedCluster['GalaxyCluster']['galaxy_id'] != $galaxy['id']) {
$errors[] = __('Cluster forks always have to belong to the same galaxy as the parent');
@ -267,7 +266,7 @@ class GalaxyCluster extends AppModel
if ($user['Role']['perm_sync']) {
if (isset($cluster['GalaxyCluster']['distribution']) && $cluster['GalaxyCluster']['distribution'] == 4 && !$this->SharingGroup->checkIfAuthorised($user, $cluster['GalaxyCluster']['sharing_group_id'])) {
$errors[] = __('The sync user has to have access to the sharing group in order to be able to edit it');
return $errors;
return $errors;
}
}
@ -296,7 +295,7 @@ class GalaxyCluster extends AppModel
$this->GalaxyClusterRelation->saveRelations($user, $cluster['GalaxyCluster'], $cluster['GalaxyCluster']['GalaxyClusterRelation'], $capture=true);
}
} else {
foreach($this->validationErrors as $validationError) {
foreach ($this->validationErrors as $validationError) {
$errors[] = $validationError[0];
}
}
@ -354,9 +353,8 @@ class GalaxyCluster extends AppModel
if (!empty($cluster['GalaxyCluster']['GalaxyClusterRelation'])) {
$this->GalaxyClusterRelation->saveRelations($user, $cluster['GalaxyCluster'], $cluster['GalaxyCluster']['GalaxyClusterRelation'], $capture=true, $force=true);
}
} else {
foreach($this->validationErrors as $validationError) {
foreach ($this->validationErrors as $validationError) {
$errors[] = $validationError[0];
}
}
@ -394,10 +392,10 @@ class GalaxyCluster extends AppModel
$job->save($data);
$jobId = $job->id;
$process_id = CakeResque::enqueue(
'prio',
'EventShell',
array($function, $clusterId, $jobId, $user['id'], $passAlong),
true
'prio',
'EventShell',
array($function, $clusterId, $jobId, $user['id'], $passAlong),
true
);
$job->saveField('process_id', $process_id);
return $process_id;
@ -516,17 +514,17 @@ class GalaxyCluster extends AppModel
foreach ($clusters as $k => $cluster) {
unset($clusters[$k]['GalaxyCluster']['galaxy_id']);
$modelsToUnset = array('GalaxyCluster', 'Galaxy', 'Org', 'Orgc');
forEach($modelsToUnset as $modelName) {
foreach ($modelsToUnset as $modelName) {
unset($clusters[$k][$modelName]['id']);
}
$modelsToUnset = array('GalaxyClusterRelation', 'TargetingClusterRelation');
forEach($modelsToUnset as $modelName) {
forEach($cluster['GalaxyCluster'][$modelName] as $i => $relation) {
foreach ($modelsToUnset as $modelName) {
foreach ($cluster['GalaxyCluster'][$modelName] as $i => $relation) {
unset($clusters[$k]['GalaxyCluster'][$modelName][$i]['id']);
unset($clusters[$k]['GalaxyCluster'][$modelName][$i]['galaxy_cluster_id']);
unset($clusters[$k]['GalaxyCluster'][$modelName][$i]['referenced_galaxy_cluster_id']);
if (isset($relation['Tag'])) {
forEach($relation['Tag'] as $j => $tags) {
foreach ($relation['Tag'] as $j => $tags) {
unset($clusters[$k]['GalaxyCluster'][$modelName][$i]['Tag'][$j]['id']);
unset($clusters[$k]['GalaxyCluster'][$modelName][$i]['Tag'][$j]['org_id']);
unset($clusters[$k]['GalaxyCluster'][$modelName][$i]['Tag'][$j]['user_id']);
@ -534,7 +532,7 @@ class GalaxyCluster extends AppModel
}
}
}
forEach($cluster['GalaxyCluster']['GalaxyElement'] as $i => $element) {
foreach ($cluster['GalaxyCluster']['GalaxyElement'] as $i => $element) {
unset($clusters[$k]['GalaxyCluster']['GalaxyElement'][$i]['id']);
unset($clusters[$k]['GalaxyCluster']['GalaxyElement'][$i]['galaxy_cluster_id']);
}
@ -633,10 +631,10 @@ class GalaxyCluster extends AppModel
'recursive' => -1
));
if (!empty($cluster['GalaxyCluster']['GalaxyElement'])) {
$this->GalaxyElement->captureElements($user, $cluster['GalaxyCluster']['GalaxyElement'], $savedCluster['GalaxyCluster']['id']);
$this->GalaxyElement->captureElements($user, $cluster['GalaxyCluster']['GalaxyElement'], $savedCluster['GalaxyCluster']['id']);
}
if (!empty($cluster['GalaxyCluster']['GalaxyClusterRelation'])) {
$saveResult = $this->GalaxyClusterRelation->captureRelations($user, $savedCluster, $cluster['GalaxyCluster']['GalaxyClusterRelation'], $fromPull=$fromPull);
$saveResult = $this->GalaxyClusterRelation->captureRelations($user, $savedCluster, $cluster['GalaxyCluster']['GalaxyClusterRelation'], $fromPull=$fromPull);
if ($saveResult['failed'] > 0) {
$results['errors'][] = __('Issues while capturing relations have been logged.');
}
@ -646,7 +644,7 @@ class GalaxyCluster extends AppModel
}
} else {
$results['failed']++;
foreach($this->validationErrors as $validationError) {
foreach ($this->validationErrors as $validationError) {
$results['errors'][] = $validationError[0];
}
}
@ -747,7 +745,7 @@ class GalaxyCluster extends AppModel
public function getClusters($names, $user, $postProcess=true)
{
$conditions = array();
if (count(array_filter($names, 'is_numeric' )) === count($names)) { // all elements are numeric
if (count(array_filter($names, 'is_numeric')) === count($names)) { // all elements are numeric
$conditions[] = array('GalaxyCluster.id' => $names);
} else {
$conditions[] = array('LOWER(GalaxyCluster.tag_name)' => array_map('strtolower', $names));
@ -1410,7 +1408,7 @@ class GalaxyCluster extends AppModel
if (!$found) {
return 403;
}
} else if (empty($cluster['GalaxyCluster']['SharingGroup']['roaming'])) {
} elseif (empty($cluster['GalaxyCluster']['SharingGroup']['roaming'])) {
return 403;
}
}
@ -1455,7 +1453,7 @@ class GalaxyCluster extends AppModel
private function __prepareElementsForSync($cluster, $server)
{
if (!empty($cluster['GalaxyCluster']['GalaxyElement'])) {
foreach($cluster['GalaxyCluster']['GalaxyElement'] as $k => $element) {
foreach ($cluster['GalaxyCluster']['GalaxyElement'] as $k => $element) {
$cluster['GalaxyCluster']['GalaxyElement'][$k] = $this->__updateElementForSync($element, $server);
}
}
@ -1466,7 +1464,7 @@ class GalaxyCluster extends AppModel
{
$this->Event = ClassRegistry::init('Event');
if (!empty($cluster['GalaxyCluster']['GalaxyClusterRelation'])) {
foreach($cluster['GalaxyCluster']['GalaxyClusterRelation'] as $k => $relation) {
foreach ($cluster['GalaxyCluster']['GalaxyClusterRelation'] as $k => $relation) {
$cluster['GalaxyCluster']['GalaxyClusterRelation'][$k] = $this->__updateRelationsForSync($relation, $server);
if (empty($cluster['GalaxyCluster']['GalaxyClusterRelation'][$k])) {
unset($cluster['GalaxyCluster']['GalaxyClusterRelation'][$k]);

View File

@ -106,7 +106,7 @@ class GalaxyClusterRelation extends AppModel
);
if (!empty($options['contain'])) {
$params['contain'] = $options['contain'];
} elseif($full) {
} elseif ($full) {
$params['contain'] = array('SharingGroup', 'SourceCluster', 'TargetCluster');
}
if (isset($options['fields'])) {
@ -160,7 +160,7 @@ class GalaxyClusterRelation extends AppModel
public function saveRelations($user, $cluster, $relations, $capture=false, $force=false)
{
$errors = array();
foreach($relations as $k => $relation) {
foreach ($relations as $k => $relation) {
$saveResult = $this->saveRelation($user, $cluster, $relation, $capture=$capture, $force=$force);
$errors = array_merge($errors, $saveResult);
}
@ -220,7 +220,7 @@ class GalaxyClusterRelation extends AppModel
}
}
} else {
foreach($this->validationErrors as $validationError) {
foreach ($this->validationErrors as $validationError) {
$errors[] = $validationError[0];
}
}
@ -278,7 +278,7 @@ class GalaxyClusterRelation extends AppModel
$saveSuccess = $this->save($relation, array('fieldList' => $fieldList));
if (!$saveSuccess) {
foreach($this->validationErrors as $validationError) {
foreach ($this->validationErrors as $validationError) {
$errors[] = $validationError[0];
}
} else {
@ -377,8 +377,6 @@ class GalaxyClusterRelation extends AppModel
}
} else {
$results['failed']++;
foreach($this->validationErrors as $validationError) {
}
}
}

View File

@ -139,7 +139,7 @@ class Role extends AppModel
'id' => 'RolePermGalaxyEditor',
'text' => 'Galaxy Editor',
'readonlyenabled' => false,
'title' => 'Create or modify MISP Galaxies'
'title' => 'Create or modify MISP Galaxies and MISP Galaxies Clusters'
),
'perm_decaying' => array(
'id' => 'RolePermDecaying',

View File

@ -88,4 +88,4 @@
addNewRow(entry.key, entry.value);
})
}
</script>
</script>

View File

@ -37,4 +37,4 @@
</span>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php endif; ?>

View File

@ -10,6 +10,13 @@
$data = '';
}
}
}
if (is_bool($data)) {
$data = sprintf(
'<i class="black fa fa-%s"></i>',
$data ? 'check' : 'times'
);
$data = '';
} else {
$data = h($data);
if (!empty($field['privacy'])) {

View File

@ -0,0 +1,27 @@
<?php
$inOutCount = Hash::extract($row, $field['data_path']);
$titleOut = __(
'This %s %s %s other %s',
$field['fields']['entity_name'],
$field['fields']['outbound_action_name'],
$inOutCount['out'],
$inOutCount['out'] > 0 ? Inflector::pluralize($field['fields']['entity_name']) : $field['fields']['entity_name']
);
$titleIn = __(
'This %s %s %s other %s',
$field['fields']['entity_name'],
$field['fields']['inbound_action_name'],
$inOutCount['in'],
$inOutCount['in'] > 0 ? Inflector::pluralize($field['fields']['entity_name']) : $field['fields']['entity_name']
);
?>
<span>
<span title="<?= $titleOut ?>" style="margin-right: 3px;">
<i class="<?= $this->FontAwesome->getClass('sign-out-alt') ?> fa-sign-out-alt fa-rotate-270"></i>
<?= isset($inOutCount['out']) ? $inOutCount['out'] : 0 ?>
</span>
<span title="<?= $titleIn ?>">
<i class="<?= $this->FontAwesome->getClass('sign-in-alt') ?> fa-sign-in-alt fa-rotate-90"></i>
<?= isset($inOutCount['in']) ? $inOutCount['in'] : 0 ?>
</span>
</span>

View File

@ -1,13 +0,0 @@
<?php
$relationCount = Hash::extract($row, $field['data_path']);
?>
<span>
<span title="<?= __('This cluster targets %s other clusters', $relationCount['out']) ?>" style="margin-right: 3px;">
<i class="<?= $this->FontAwesome->getClass('sign-out-alt') ?> fa-sign-out-alt fa-rotate-270"></i>
<?= isset($relationCount['out']) ? $relationCount['out'] : 0 ?>
</span>
<span title="<?= __('This cluster is being targeted by %s other clusters', $relationCount['in']) ?>">
<i class="<?= $this->FontAwesome->getClass('sign-in-alt') ?> fa-sign-in-alt fa-rotate-90"></i>
<?= isset($relationCount['in']) ? $relationCount['in'] : 0 ?>
</span>
</span>

View File

@ -203,7 +203,7 @@ function submitFunction(clicked, callback) {
additionalDataOption = options_additionalData[$select.attr('id')];
if (additionalData !== undefined) {
additionalData['itemOptions'] = additionalDataOption;
// check needed if the function name is not defined in the controller but in the JS
// callback function defined in the controller can be overridden in the JS
var dismissId = $clicked.closest('.popover[data-dismissid]').data('dismissid');
var callingButton = $('button[data-dismissid="' + dismissId + '"]');
if (callingButton.data('popover-no-submit') && callingButton.data('popover-callback-function') !== undefined) {

View File

@ -5,4 +5,4 @@
</div>
<?php else: ?>
<?php echo $this->element('GalaxyClusters/relations_graph'); ?>
<?php endif ?>
<?php endif ?>

View File

@ -11,4 +11,4 @@
</div>
<?php else: ?>
<?php echo $this->element('GalaxyClusters/relations_graph'); ?>
<?php endif ?>
<?php endif ?>

View File

@ -106,4 +106,4 @@
$('#GalaxyClusterRelationTags').val(selectedTags.join(', '));
}
</script>
<?php echo $this->Js->writeBuffer(); // Write cached scripts
<?php echo $this->Js->writeBuffer(); // Write cached scripts

View File

@ -101,4 +101,4 @@
});
});
</script>
<?php echo $this->Js->writeBuffer(); // Write cached scripts
<?php echo $this->Js->writeBuffer(); // Write cached scripts

View File

@ -117,7 +117,12 @@
'name' => __('#Relations'),
'class' => 'short',
'data_path' => 'GalaxyCluster.relation_counts',
'element' => 'relation_counts'
'element' => 'in_out_counts',
'fields' => array(
'entity_name' => __('cluster'),
'inbound_action_name' => __('is being targeted by'),
'outbound_action_name' => __('targets'),
)
),
array(
'name' => __('Description'),

View File

@ -116,4 +116,4 @@
</div>
<?php echo $this->element('/genericElements/SideMenu/side_menu', array('menuList' => 'galaxies', 'menuItem' => 'update_cluster')); ?>
</div>
</div>

View File

@ -260,4 +260,4 @@ function toggleLoadingButton(loading) {
$('#buttonAddRelationship > i').removeClass('fa-spinner fa-spin').addClass('fa-plus');
}
}
</script>
</script>

View File

@ -2553,4 +2553,4 @@ table tr:hover .down-expand-button {
display: flex;
flex-direction: column;
flex-grow: 1;
}
}

View File

@ -5088,4 +5088,4 @@ function loadClusterRelations(clusterId) {
}
);
}
}
}