mirror of https://github.com/MISP/MISP
new: [decaying] Added models import and export feature
parent
5857fdb7aa
commit
e1d9d53390
|
@ -27,13 +27,58 @@ class DecayingModelController extends AppController
|
||||||
} else {
|
} else {
|
||||||
$this->Flash->success($message);
|
$this->Flash->success($message);
|
||||||
$this->redirect(array('controller' => 'decayingModel', 'action' => 'index'));
|
$this->redirect(array('controller' => 'decayingModel', 'action' => 'index'));
|
||||||
// return $this->RestResponse->viewData($message, $this->response->type());
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new MethodNotAllowedException(__("This method is not allowed"));
|
throw new MethodNotAllowedException(__("This method is not allowed"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function export($model_id)
|
||||||
|
{
|
||||||
|
$model = $this->DecayingModel->checkAuthorisation($this->Auth->user(), $model_id, true);
|
||||||
|
if (!$this->_isSiteAdmin() && !$decModel) {
|
||||||
|
throw new MethodNotAllowedException(__('No Decaying Model with the provided ID exists, or you are not authorised to view it.'));
|
||||||
|
}
|
||||||
|
unset($model['DecayingModel']['id']);
|
||||||
|
unset($model['DecayingModel']['org_id']);
|
||||||
|
unset($model['DecayingModelMapping']);
|
||||||
|
return $this->RestResponse->viewData($model, $this->response->type());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function import()
|
||||||
|
{
|
||||||
|
if ($this->request->is('post') || $this->request->is('put')) {
|
||||||
|
$data = $this->request->data['DecayingModel'];
|
||||||
|
if ($data['submittedjson']['name'] != '' && $data['json'] != '') {
|
||||||
|
throw new MethodNotAllowedException(__('Only one import field can be used'));
|
||||||
|
}
|
||||||
|
if ($data['submittedjson']['size'] > 0) {
|
||||||
|
$filename = basename($data['submittedjson']['name']);
|
||||||
|
$file_content = file_get_contents($data['submittedjson']['tmp_name']);
|
||||||
|
if ((isset($data['submittedjson']['error']) && $data['submittedjson']['error'] == 0) ||
|
||||||
|
(!empty($data['submittedjson']['tmp_name']) && $data['submittedjson']['tmp_name'] != '')
|
||||||
|
) {
|
||||||
|
if (!$file_content) {
|
||||||
|
throw new InternalErrorException(__('PHP says file was not uploaded. Are you attacking me?'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$text = $file_content;
|
||||||
|
} else {
|
||||||
|
$text = $data['json'];
|
||||||
|
}
|
||||||
|
$json = json_decode($text, true);
|
||||||
|
if ($json === null) {
|
||||||
|
throw new MethodNotAllowedException(__('Error while decoding JSON'));
|
||||||
|
}
|
||||||
|
if ($this->DecayingModel->save($json)) {
|
||||||
|
$this->Flash->success(__('The model has been saved.'));
|
||||||
|
} else {
|
||||||
|
$this->Flash->error(__('Error while saving model.'));
|
||||||
|
}
|
||||||
|
$this->redirect(array('action' => 'index'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function view($id)
|
public function view($id)
|
||||||
{
|
{
|
||||||
if (!$this->request->is('get')) {
|
if (!$this->request->is('get')) {
|
||||||
|
|
|
@ -158,8 +158,9 @@ class DecayingModel extends AppModel
|
||||||
$decayingModels = $this->find('all', array(
|
$decayingModels = $this->find('all', array(
|
||||||
'conditions' => $conditions,
|
'conditions' => $conditions,
|
||||||
'recursive' => -1,
|
'recursive' => -1,
|
||||||
'contain' => 'DecayingModelMapping',
|
'contain' => 'DecayingModelMapping', // not needed
|
||||||
));
|
));
|
||||||
|
// #FIXME should be already done
|
||||||
foreach ($decayingModels as $i => $decayingModel) {
|
foreach ($decayingModels as $i => $decayingModel) {
|
||||||
$decayingModels[$i]['DecayingModel']['attribute_types'] = Hash::extract($decayingModels[$i]['DecayingModelMapping'], '{n}.attribute_type');
|
$decayingModels[$i]['DecayingModel']['attribute_types'] = Hash::extract($decayingModels[$i]['DecayingModelMapping'], '{n}.attribute_type');
|
||||||
unset($decayingModels[$i]['DecayingModelMapping']);
|
unset($decayingModels[$i]['DecayingModelMapping']);
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
<div class="form">
|
||||||
|
<?php echo $this->Form->create('DecayingModel', array('enctype' => 'multipart/form-data'));?>
|
||||||
|
<fieldset>
|
||||||
|
<legend><?php echo __('Import model data');?></legend>
|
||||||
|
<p><?php echo __('Paste a MISP model JSON or provide a JSON file below to add models.');?></p>
|
||||||
|
<div>
|
||||||
|
<?php
|
||||||
|
echo $this->Form->input('json', array(
|
||||||
|
'div' => 'input clear',
|
||||||
|
'label' => __('JSON'),
|
||||||
|
'placeholder' => __('Model JSON'),
|
||||||
|
'class' => 'form-control span6',
|
||||||
|
'type' => 'textarea',
|
||||||
|
'rows' => 18
|
||||||
|
));
|
||||||
|
echo $this->Form->input('submittedjson', array(
|
||||||
|
'div' => 'input clear',
|
||||||
|
'label' => __('JSON file'),
|
||||||
|
'type' => 'file'
|
||||||
|
));
|
||||||
|
?>
|
||||||
|
</div>
|
||||||
|
</fieldset>
|
||||||
|
<?php
|
||||||
|
echo $this->Form->button(__('Add'), array('class' => 'btn btn-primary'));
|
||||||
|
echo $this->Form->end();
|
||||||
|
?>
|
||||||
|
</div>
|
||||||
|
<?php
|
||||||
|
echo $this->element('/genericElements/SideMenu/side_menu', array('menuList' => 'decayingModel', 'menuItem' => 'import'));
|
|
@ -50,6 +50,7 @@ foreach ($decayingModel as $item): ?>
|
||||||
<td class="short action-links">
|
<td class="short action-links">
|
||||||
<?php echo $this->Html->link('', array('action' => 'view', $item['DecayingModel']['id']), array('class' => 'icon-list-alt', 'title' => 'View'));?>
|
<?php echo $this->Html->link('', array('action' => 'view', $item['DecayingModel']['id']), array('class' => 'icon-list-alt', 'title' => 'View'));?>
|
||||||
<?php echo $this->Html->link('', array('action' => 'edit', $item['DecayingModel']['id']), array('class' => 'icon-edit', 'title' => 'Edit'));?>
|
<?php echo $this->Html->link('', array('action' => 'edit', $item['DecayingModel']['id']), array('class' => 'icon-edit', 'title' => 'Edit'));?>
|
||||||
|
<?php echo $this->Html->link('', array('action' => 'export', $item['DecayingModel']['id'] . '.json'), array('download' => true, 'class' => 'fa fa-cloud-download-alt', 'title' => __('Download model')));?>
|
||||||
<?php echo $this->Form->postLink('', array('action' => 'delete', $item['DecayingModel']['id']), array('class' => 'icon-trash', 'title' => 'Delete'), __('Are you sure you want to delete DecayingModel #' . $item['DecayingModel']['id'] . '?'));?>
|
<?php echo $this->Form->postLink('', array('action' => 'delete', $item['DecayingModel']['id']), array('class' => 'icon-trash', 'title' => 'Delete'), __('Are you sure you want to delete DecayingModel #' . $item['DecayingModel']['id'] . '?'));?>
|
||||||
</td>
|
</td>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
|
|
@ -899,6 +899,10 @@
|
||||||
'text' => __('Force Update Default Models')
|
'text' => __('Force Update Default Models')
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
echo $this->element('/genericElements/SideMenu/side_menu_link', array(
|
||||||
|
'url' => '/decayingModel/import',
|
||||||
|
'text' => __('Import Decaying Model')
|
||||||
|
));
|
||||||
echo $this->element('/genericElements/SideMenu/side_menu_link', array(
|
echo $this->element('/genericElements/SideMenu/side_menu_link', array(
|
||||||
'url' => '/decayingModel/add',
|
'url' => '/decayingModel/add',
|
||||||
'text' => __('Add Decaying Model')
|
'text' => __('Add Decaying Model')
|
||||||
|
|
Loading…
Reference in New Issue