mirror of https://github.com/MISP/MISP
chg: [galaxyClusters:add] Created views, controller and models functions
parent
d3b837f947
commit
c314484207
|
@ -83,9 +83,13 @@ class GalaxiesController extends AppController
|
|||
public function view($id)
|
||||
{
|
||||
$id = $this->Toolbox->findIdByUuid($this->Galaxy, $id);
|
||||
$passedArgsArray = array(
|
||||
'context' => isset($this->params['named']['context']) ? $this->params['named']['context'] : 'all'
|
||||
);
|
||||
if (isset($this->params['named']['searchall']) && strlen($this->params['named']['searchall']) > 0) {
|
||||
$this->set('passedArgsArray', array('context' => $this->params['named']['context'], 'searchall' => $this->params['named']['searchall']));
|
||||
$passedArgsArray['searchall'] = $this->params['named']['searchall'];
|
||||
}
|
||||
$this->set('passedArgsArray', $passedArgsArray);
|
||||
if ($this->_isRest()) {
|
||||
$galaxy = $this->Galaxy->find('first', array(
|
||||
'contain' => array('GalaxyCluster' => array('GalaxyElement'/*, 'GalaxyReference'*/)),
|
||||
|
|
|
@ -42,15 +42,15 @@ class GalaxyClustersController extends AppController
|
|||
$contextConditions = array();
|
||||
if ($filters['context'] == 'default') {
|
||||
$contextConditions = array(
|
||||
'Galaxy.default' => true
|
||||
'GalaxyCluster.default' => true
|
||||
);
|
||||
} elseif ($filters['context'] == 'custom') {
|
||||
$contextConditions = array(
|
||||
'GalaxyCluster.default' => false
|
||||
);
|
||||
} elseif ($filters['context'] == 'org') {
|
||||
$contextConditions = array(
|
||||
'Galaxy.org_id' => $this->Auth->user('org_id')
|
||||
);
|
||||
} elseif ($filters['context'] == 'orgc') {
|
||||
$contextConditions = array(
|
||||
'Galaxy.orgc_id' => $this->Auth->user('org_id')
|
||||
'GalaxyCluster.org_id' => $this->Auth->user('org_id')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -214,6 +214,95 @@ class GalaxyClustersController extends AppController
|
|||
}
|
||||
}
|
||||
|
||||
public function add($galaxyId)
|
||||
{
|
||||
$this->loadModel('Attribute');
|
||||
$distributionLevels = $this->Attribute->distributionLevels;
|
||||
unset($distributionLevels[5]);
|
||||
$initialDistribution = 3;
|
||||
$configuredDistribution = Configure::check('MISP.default_attribute_distribution');
|
||||
if ($configuredDistribution != null && $configuredDistribution != 'event') {
|
||||
$initialDistribution = $configuredDistribution;
|
||||
}
|
||||
$this->loadModel('SharingGroup');
|
||||
$sgs = $this->SharingGroup->fetchAllAuthorised($this->Auth->user(), 'name', 1);
|
||||
|
||||
if (isset($this->params['named']['forkUuid'])) {
|
||||
$forkUuid = $this->params['named']['forkUuid'];
|
||||
$origCluster = $this->GalaxyCluster->fetchGalaxyClusters($this->Auth->user(), array(
|
||||
'conditions' => array('uuid' => $forkUuid),
|
||||
), true);
|
||||
if (!empty($origCluster)) {
|
||||
$origCluster = $origCluster[0];
|
||||
$origClusterMeta = $origCluster['GalaxyCluster'];
|
||||
$this->set('forkUuid', $forkUuid);
|
||||
if (empty($this->request->data)) {
|
||||
$this->request->data = $origCluster;
|
||||
unset($this->request->data['GalaxyCluster']['id']);
|
||||
unset($this->request->data['GalaxyCluster']['uuid']);
|
||||
foreach ($origCluster['GalaxyElement'] as $k => $element) {
|
||||
unset($origCluster['GalaxyElement'][$k]['id']);
|
||||
unset($origCluster['GalaxyElement'][$k]['galaxy_cluster_id']);
|
||||
}
|
||||
$this->request->data['GalaxyCluster']['elements'] = json_encode($origCluster['GalaxyElement']);
|
||||
$this->request->data['GalaxyCluster']['authors'] = json_encode($origCluster['GalaxyCluster']['authors']);
|
||||
}
|
||||
$this->set('origCluster', $origCluster);
|
||||
$this->set('origClusterMeta', $origClusterMeta);
|
||||
} else {
|
||||
throw new NotFoundException('Forked cluster not found.');
|
||||
}
|
||||
}
|
||||
if ($this->request->is('post') || $this->request->is('put')) {
|
||||
$cluster = $this->request->data;
|
||||
$errors = array();
|
||||
if (empty($cluster['GalaxyCluster']['elements'])) {
|
||||
$galaxy['Galaxy']['values'] = array();
|
||||
} else {
|
||||
$decoded = json_decode($cluster['GalaxyCluster']['elements'], true);
|
||||
if ($decoded === null) {
|
||||
$decoded = array();
|
||||
}
|
||||
$galaxy['Galaxy']['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'];
|
||||
} else {
|
||||
$cluster['GalaxyCluster']['extends_uuid'] = '';
|
||||
}
|
||||
if ($cluster['GalaxyCluster']['distribution'] != 4) {
|
||||
$cluster['GalaxyCluster']['sharing_group_id'] = null;
|
||||
}
|
||||
$saveSuccess = $this->GalaxyCluster->saveCluster($this->Auth->user(), $cluster);
|
||||
if (!$saveSuccess) {
|
||||
foreach($this->GalaxyCluster->validationErrors as $validationError) {
|
||||
$errors[] = $validationError;
|
||||
}
|
||||
}
|
||||
if (!empty($errors)) {
|
||||
$flashErrorMessage = implode(', ', implode(' ', $errors));
|
||||
$this->Flash->error($flashErrorMessage);
|
||||
} else {
|
||||
$this->redirect(array('controller' => 'galaxy_clusters', 'action' => 'view', $this->GalaxyCluster->id));
|
||||
}
|
||||
}
|
||||
$this->set('galaxy_id', $galaxyId);
|
||||
$this->set('distributionLevels', $distributionLevels);
|
||||
$this->set('initialDistribution', $initialDistribution);
|
||||
$this->set('sharingGroups', $sgs);
|
||||
$this->set('action', 'add');
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function attachToEvent($event_id, $tag_name)
|
||||
{
|
||||
$this->loadModel('Event');
|
||||
|
|
|
@ -89,6 +89,64 @@ class GalaxyCluster extends AppModel
|
|||
$this->GalaxyElement->deleteAll(array('GalaxyElement.galaxy_cluster_id' => $this->id));
|
||||
}
|
||||
|
||||
// Respecting ACL, save a cluster, its elements and set correct fields
|
||||
public function saveCluster($user, $cluster, $fromPull=false)
|
||||
{
|
||||
if (!$user['Role']['perm_galaxy_editor'] && !$user['Role']['perm_site_admin']) {
|
||||
return false;
|
||||
}
|
||||
$galaxy = $this->Galaxy->find('first', array('conditions' => array(
|
||||
'id' => $cluster['GalaxyCluster']['galaxy_id']
|
||||
)));
|
||||
if (empty($galaxy)) {
|
||||
return false;
|
||||
} else {
|
||||
$galaxy = $galaxy['Galaxy'];
|
||||
}
|
||||
unset($cluster['GalaxyCluster']['id']);
|
||||
if (isset($cluster['GalaxyCluster']['uuid'])) {
|
||||
// check if the uuid already exists
|
||||
$existingGalaxyCluster = $this->find('first', array('conditions' => array('GalaxyCluster.uuid' => $cluster['GalaxyCluster']['uuid'])));
|
||||
if ($existingGalaxyCluster) {
|
||||
if ($fromPull && !$existingGalaxyCluster['GalaxyCluster']['default']) {
|
||||
$errors = $this->editCluster($user, $cluster, $fromPull);
|
||||
return empty($errors);
|
||||
} else {
|
||||
// Maybe redirect to the correct URL?
|
||||
}
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
$cluster['GalaxyCluster']['uuid'] = CakeText::uuid();
|
||||
}
|
||||
$cluster['GalaxyCluster']['org_id'] = $user['org_id'];
|
||||
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));
|
||||
} else {
|
||||
$orgc_id = $user['org_id'];
|
||||
}
|
||||
$cluster['GalaxyCluster']['orgc_id'] = $orgc_id;
|
||||
}
|
||||
$cluster['GalaxyCluster']['type'] = $galaxy['type'];
|
||||
if (!$fromPull) {
|
||||
$date = new DateTime();
|
||||
$cluster['GalaxyCluster']['version'] = $date->getTimestamp();
|
||||
}
|
||||
$cluster['GalaxyCluster']['tag_name'] = sprintf('misp-galaxy:%s="%s"', $galaxy['type'], $cluster['GalaxyCluster']['value']);
|
||||
$this->create();
|
||||
$saveSuccess = $this->save($cluster);
|
||||
return $saveSuccess;
|
||||
// $savedCluster = $this->GalaxyCluster->find('first', array(
|
||||
// 'conditions' => array('id' => $this->GalaxyCluster->id),
|
||||
// 'recursive' => -1
|
||||
// ));
|
||||
// $savedCluster['GalaxyCluster']['elements'] = $cluster['GalaxyCluster']['elements'];
|
||||
// $saveSuccess = $this->GalaxyElement->update($savedCluster['GalaxyCluster']['id'], $savedCluster['GalaxyCluster'], true, false);
|
||||
// if(!$saveSuccess) {
|
||||
// $errors[] = array(__('Error while saving cluster\'s elements'));
|
||||
// }
|
||||
}
|
||||
|
||||
// receive a full galaxy and add all new clusters, update existing ones contained in the new galaxy, cull old clusters that are removed from the galaxy
|
||||
public function update($id, $galaxy)
|
||||
|
@ -258,9 +316,12 @@ class GalaxyCluster extends AppModel
|
|||
{
|
||||
$params = array(
|
||||
'conditions' => $this->buildConditions($user),
|
||||
'contain' => array(),
|
||||
'recursive' => -1
|
||||
);
|
||||
$params['contain'] = $options['contain'];
|
||||
if (!empty($options['contain'])) {
|
||||
$params['contain'] = $options['contain'];
|
||||
}
|
||||
if ($full && !in_array('GalaxyElement', $params['contain'])) {
|
||||
$params['contain'][] = 'GalaxyElement';
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@
|
|||
}
|
||||
} else {
|
||||
if ($field['fields']['allow_picture']) {
|
||||
echo $this->OrgImg->getOrgImg(array('name' => $field['fields']['default_org'], 'size' => 24));
|
||||
echo $this->OrgImg->getOrgImg(array('name' => $field['fields']['default_org'], 'size' => 24), false, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1153,7 +1153,7 @@
|
|||
));
|
||||
if ($menuItem === 'view') {
|
||||
echo $this->element('/genericElements/SideMenu/side_menu_link', array(
|
||||
'url' => '/galaxies/add/forkId:' . h($galaxy['Galaxy']['id']),
|
||||
'url' => '/galaxy_clusters/add/' . h($galaxy['Galaxy']['id']),
|
||||
'text' => __('Add Galaxy Cluster')
|
||||
));
|
||||
}
|
||||
|
@ -1209,6 +1209,19 @@
|
|||
}
|
||||
break;
|
||||
|
||||
case 'galaxy_cluster':
|
||||
echo $this->element('/genericElements/SideMenu/side_menu_link', array(
|
||||
'url' => '/galaxies/index',
|
||||
'text' => __('List Galaxies')
|
||||
));
|
||||
echo $this->element('/genericElements/SideMenu/side_menu_divider');
|
||||
echo $this->element('/genericElements/SideMenu/side_menu_link', array(
|
||||
'element_id' => 'view',
|
||||
'url' => '/galaxies/view/' . h($galaxy_id),
|
||||
'text' => __('View Galaxy')
|
||||
));
|
||||
break;
|
||||
|
||||
case 'objectTemplates':
|
||||
echo $this->element('/genericElements/SideMenu/side_menu_link', array(
|
||||
'url' => '/objectTemplates/index',
|
||||
|
|
|
@ -15,6 +15,16 @@
|
|||
'url' => sprintf('%s/galaxies/view/%s/context:all', $baseurl, $galaxy_id),
|
||||
'text' => __('All'),
|
||||
),
|
||||
array(
|
||||
'active' => $context === 'default',
|
||||
'url' => sprintf('%s/galaxies/view/%s/context:default', $baseurl, $galaxy_id),
|
||||
'text' => __('Default Galaxy Clusters'),
|
||||
),
|
||||
array(
|
||||
'active' => $context === 'custom',
|
||||
'url' => sprintf('%s/galaxies/view/%s/context:custom', $baseurl, $galaxy_id),
|
||||
'text' => __('Custom Galaxy Clusters'),
|
||||
),
|
||||
array(
|
||||
'active' => $context === 'org',
|
||||
'url' => sprintf('%s/galaxies/view/%s/context:org', $baseurl, $galaxy_id),
|
||||
|
@ -110,6 +120,34 @@
|
|||
'icon' => 'eye',
|
||||
'dbclickAction' => true
|
||||
),
|
||||
array(
|
||||
'url' => '/galaxy_clusters/add',
|
||||
'url_params_data_paths' => array(
|
||||
'GalaxyCluster.galaxy_id'
|
||||
),
|
||||
'url_named_params_data_paths' => array(
|
||||
'forkUuid' => 'GalaxyCluster.uuid'
|
||||
),
|
||||
'icon' => 'code-branch'
|
||||
),
|
||||
array(
|
||||
'url' => '/galaxy_clusters/edit',
|
||||
'url_params_data_paths' => array(
|
||||
'GalaxyCluster.id'
|
||||
),
|
||||
'icon' => 'edit',
|
||||
'complex_requirement' => array(
|
||||
'function' => function($row, $options) {
|
||||
return ($options['me']['org_id'] == $options['datapath']['org']);
|
||||
},
|
||||
'options' => array(
|
||||
'me' => $me,
|
||||
'datapath' => array(
|
||||
'org' => 'GalaxyCluster.org_id'
|
||||
)
|
||||
)
|
||||
),
|
||||
),
|
||||
array(
|
||||
'title' => 'Delete',
|
||||
'url' => '/galaxy_clusters/delete',
|
||||
|
|
|
@ -2523,6 +2523,14 @@ table tr:hover .down-expand-button {
|
|||
border-radius: 3px;
|
||||
}
|
||||
|
||||
.fork-cluster-preview {
|
||||
display: inline-block;
|
||||
position: absolute;
|
||||
right: 0px;
|
||||
top: 100px;
|
||||
max-width: 600px;
|
||||
}
|
||||
|
||||
.widgetTitle {
|
||||
font-weight: bold;
|
||||
color: #0088cc;
|
||||
|
@ -2540,4 +2548,4 @@ table tr:hover .down-expand-button {
|
|||
display: flex;
|
||||
flex-direction: column;
|
||||
flex-grow: 1;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue