new: [decaying] Added models import and export feature

pull/5032/head
mokaddem 2019-07-24 10:51:58 +02:00
parent 5857fdb7aa
commit e1d9d53390
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
5 changed files with 83 additions and 2 deletions

View File

@ -27,13 +27,58 @@ class DecayingModelController extends AppController
} else {
$this->Flash->success($message);
$this->redirect(array('controller' => 'decayingModel', 'action' => 'index'));
// return $this->RestResponse->viewData($message, $this->response->type());
}
} else {
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)
{
if (!$this->request->is('get')) {

View File

@ -158,8 +158,9 @@ class DecayingModel extends AppModel
$decayingModels = $this->find('all', array(
'conditions' => $conditions,
'recursive' => -1,
'contain' => 'DecayingModelMapping',
'contain' => 'DecayingModelMapping', // not needed
));
// #FIXME should be already done
foreach ($decayingModels as $i => $decayingModel) {
$decayingModels[$i]['DecayingModel']['attribute_types'] = Hash::extract($decayingModels[$i]['DecayingModelMapping'], '{n}.attribute_type');
unset($decayingModels[$i]['DecayingModelMapping']);

View File

@ -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'));

View File

@ -50,6 +50,7 @@ foreach ($decayingModel as $item): ?>
<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' => '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'] . '?'));?>
</td>
<?php endif; ?>

View File

@ -899,6 +899,10 @@
'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(
'url' => '/decayingModel/add',
'text' => __('Add Decaying Model')