chg: [DecayingTool] Added more fields

pull/5032/head
mokaddem 2019-04-03 11:51:29 +02:00
parent 03015aa341
commit 38a7f511d7
6 changed files with 154 additions and 16 deletions

View File

@ -0,0 +1,28 @@
<?php
App::uses('AppController', 'Controller');
class DecayingModelMappingController extends AppController
{
public $components = array('Security' ,'RequestHandler');
public $belongsTo = array(
'DecayingModel' => array(
'className' => 'DecayingModel',
'foreignKey' => 'id'
)
);
public $paginate = array(
'limit' => 50,
'order' => array(
'DecayingModel.name' => 'asc'
)
);
public function viewAssociatedTypes($model_id) {
$associated_types = $this->DecayingModelMapping->getAssociatedTypes($this->Auth->user(), $model_id);
return $this->RestResponse->viewData($associated_types, $this->response->type());
}
}

View File

@ -1102,8 +1102,11 @@ class AppModel extends Model
`uuid` varchar(40) COLLATE utf8_bin DEFAULT NULL,
`name` text,
`parameters` text,
`attribute_types` text,
`description` text,
`org_id` int(11),
`ref` text COLLATE utf8_unicode_ci,
`formula` varchar(255) COLLATE utf8_bin NOT NULL,
`version` varchar(255) COLLATE utf8_bin NOT NULL
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";

View File

@ -23,6 +23,20 @@ class DecayingModel extends AppModel
}
$results[$k]['DecayingModel']['parameters'] = $decoded;
}
if (!empty($v['DecayingModel']['attribute_types'])) {
$decoded = json_decode($v['DecayingModel']['attribute_types'], true);
if ($decoded === null) {
$decoded = array();
}
$results[$k]['DecayingModel']['attribute_types'] = $decoded;
}
if (!empty($v['DecayingModel']['ref'])) {
$decoded = json_decode($v['DecayingModel']['ref'], true);
if ($decoded === null) {
$decoded = array();
}
$results[$k]['DecayingModel']['ref'] = $decoded;
}
}
return $results;
}
@ -36,12 +50,25 @@ class DecayingModel extends AppModel
}
return false;
}
if (!empty($this->data['DecayingModel']['attribute_types']) && !is_array($this->data['DecayingModel']['attribute_types'])) {
$encoded = json_decode($this->data['DecayingModel']['attribute_types'], true);
if ($encoded !== null) {
return true;
}
return false;
}
}
public function beforeSave($options = array()) {
if (isset($this->data['DecayingModel']['parameters']) && is_array($this->data['DecayingModel']['parameters'])) {
$this->data['DecayingModel']['parameters'] = json_encode($this->data['DecayingModel']['parameters']);
}
if (isset($this->data['DecayingModel']['attribute_types']) && is_array($this->data['DecayingModel']['attribute_types'])) {
$this->data['DecayingModel']['attribute_types'] = json_encode($this->data['DecayingModel']['attribute_types']);
}
if (isset($this->data['DecayingModel']['ref']) && is_array($this->data['DecayingModel']['ref'])) {
$this->data['DecayingModel']['ref'] = json_encode($this->data['DecayingModel']['ref']);
}
if (!isset($this->data['DecayingModel']['org_id'])) {
$this->data['DecayingModel']['org_id'] = Configure::read('MISP.host_org_id');
}
@ -62,7 +89,7 @@ class DecayingModel extends AppModel
return $models;
}
public function update($force = false)
public function update($force=false)
{
$new_models = $this->__load_models($force);
$temp = $this->find('all', array(
@ -115,8 +142,6 @@ class DecayingModel extends AppModel
return false;
}
//if the user is a site admin, return the template without question
if ($user['Role']['perm_site_admin']) {
return $decayingModel;

View File

@ -0,0 +1,78 @@
<?php
App::uses('AppModel', 'Model');
class DecayingModelMapping extends AppModel
{
public $actsAs = array('Containable');
public $validate = array(
'org_id' => array(
'valueNotEmpty' => array(
'rule' => array('valueNotEmpty'),
),
),
'attribute_type' => array(
'valueNotEmpty' => array(
'rule' => array('valueNotEmpty'),
),
),
'model_id' => array(
'valueNotEmpty' => array(
'rule' => array('valueNotEmpty'),
),
),
);
public $belongsTo = array(
'DecayingModel',
'Attribute'
);
private $__default_type_mapping = array(
'domain|ip' => 10,
'ip-dst' => 10,
'ip-dst|port' => 10,
'ip-src' => 10,
'ip-src|port' => 10,
);
private $__default_type_mapping_reverse = array();
// private $default_type_mapping_reverse = array_flip($this->__default_type_mapping);
private function __setup() {
foreach ($this->__default_type_mapping as $type => $model_id) {
if (!isset($this->__default_type_mapping_reverse[$model_id])) {
$this->__default_type_mapping_reverse[$model_id] = array();
}
$this->__default_type_mapping_reverse[$model_id][] = $type;
}
}
public function injectDefaultMapping(&$associated_types, $model_id) {
$associated_types = array_merge($associated_types, $this->__default_type_mapping_reverse[$model_id]);
}
public function getAssociatedTypes($user, $model_id) {
$this->__setup();
$decaying_model = $this->DecayingModel->checkAuthorisation($user, $model_id);
if (!$decaying_model) {
$associated_types = array();
} else {
$conditions = array(
'org_id' => $user['Organisation']['id'],
'model_id' => $model_id
);
$associated_types = $this->find('all', array(
'conditions' => $conditions,
'recursive' => -1,
'fields' => array('attribute_type')
));
$this->injectDefaultMapping($associated_types, $model_id);
// if (!empty($associated_types)) {
// $associated_types = $associated_types[0];
// }
}
return $associated_types;
}
}

View File

@ -13,7 +13,9 @@
$table_data[] = array('key' => __('Name'), 'value' => $decaying_model['DecayingModel']['name']);
$table_data[] = array('key' => __('Description'), 'value' => $decaying_model['DecayingModel']['description']);
$table_data[] = array('key' => __('Parameters'), 'value' => json_encode($decaying_model['DecayingModel']['parameters']), 'class' => 'json-transform');
$table_data[] = array('key' => __('Associated types'), 'value' => json_encode($decaying_model['DecayingModelMapping']), 'class' => 'json-transform');
$table_data[] = array('key' => __('Formula'), 'value' => $decaying_model['DecayingModel']['formula']);
$table_data[] = array('key' => __('Reference(s)'), 'html' => implode('<br/>', (empty($decaying_model['DecayingModel']['ref']) ? array() : $decaying_model['DecayingModel']['ref'])));
$table_data[] = array('key' => __('Associated types'), 'value' => json_encode($decaying_model['DecayingModel']['attribute_types']), 'class' => 'json-transform');
?>
<div class='view'>
<div class="row-fluid">

View File

@ -861,17 +861,19 @@
'url' => '/decayingModel/index',
'text' => __('List Decaying Models')
));
if ($isSiteAdmin || $isAclDecayingModel) {
echo $this->element('/genericElements/SideMenu/side_menu_post_link', array(
'event_id' => 'update',
'url' => '/decayingModel/update',
'text' => __('Update Default Models')
));
echo $this->element('/genericElements/SideMenu/side_menu_post_link', array(
'event_id' => 'update',
'url' => '/decayingModel/update/true',
'text' => __('Force Update Default Models')
));
if ($isSiteAdmin) {
if (($menuItem === 'view' || $menuItem === 'index')) {
echo $this->element('/genericElements/SideMenu/side_menu_post_link', array(
'event_id' => 'update',
'url' => '/decayingModel/update',
'text' => __('Update Default Models')
));
echo $this->element('/genericElements/SideMenu/side_menu_post_link', array(
'event_id' => 'update',
'url' => '/decayingModel/update/true',
'text' => __('Force Update Default Models')
));
}
echo $this->element('/genericElements/SideMenu/side_menu_link', array(
'url' => '/decayingModel/add',
'text' => __('Add Decaying Model')
@ -887,7 +889,7 @@
'url' => '/decayingModel/view/' . h($id),
'text' => __('View Decaying Model')
));
if ($isAclDecayingModel) {
if ($isSiteAdmin) {
echo $this->element('/genericElements/SideMenu/side_menu_link', array(
'element_id' => 'edit',
'url' => '/decayingModel/edit/' . h($id),