mirror of https://github.com/MISP/MISP
chg: [decayingMapping] Refacto - Comments and code optimization
parent
457e556dcb
commit
3a64405bd9
|
@ -20,7 +20,7 @@ class DecayingModelController extends AppController
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->request->is('post') || $this->request->is('put')) {
|
if ($this->request->is('post') || $this->request->is('put')) {
|
||||||
$this->DecayingModel->update($force);
|
$this->DecayingModel->update($force, $this->Auth->user());
|
||||||
$message = __('Default decaying models updated');
|
$message = __('Default decaying models updated');
|
||||||
if ($this->_isRest()) {
|
if ($this->_isRest()) {
|
||||||
return $this->RestResponse->saveSuccessResponse('DecayingModel', 'update', false, $this->response->type(), $message);
|
return $this->RestResponse->saveSuccessResponse('DecayingModel', 'update', false, $this->response->type(), $message);
|
||||||
|
|
|
@ -38,7 +38,7 @@ class DecayingModelMappingController extends AppController
|
||||||
unset($this->request->data['DecayingModelMapping']['attributetypes']);
|
unset($this->request->data['DecayingModelMapping']['attributetypes']);
|
||||||
}
|
}
|
||||||
|
|
||||||
$response = $this->DecayingModelMapping->resetMappingForModel($this->request->data['DecayingModelMapping']);
|
$response = $this->DecayingModelMapping->resetMappingForModel($this->request->data['DecayingModelMapping'], $this->Auth->user());
|
||||||
return $this->RestResponse->viewData($response, $this->response->type());
|
return $this->RestResponse->viewData($response, $this->response->type());
|
||||||
} else {
|
} else {
|
||||||
$this->set('model_id', $model_id);
|
$this->set('model_id', $model_id);
|
||||||
|
|
|
@ -1215,7 +1215,6 @@ class AppModel extends Model
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
|
||||||
$sqlArray[] = "CREATE TABLE IF NOT EXISTS decaying_model_mappings (
|
$sqlArray[] = "CREATE TABLE IF NOT EXISTS decaying_model_mappings (
|
||||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
`org_id` int(11),
|
|
||||||
`attribute_type` varchar(255) COLLATE utf8_bin NOT NULL,
|
`attribute_type` varchar(255) COLLATE utf8_bin NOT NULL,
|
||||||
`model_id` int(11) NOT NULL,
|
`model_id` int(11) NOT NULL,
|
||||||
PRIMARY KEY (id)
|
PRIMARY KEY (id)
|
||||||
|
|
|
@ -134,7 +134,7 @@ class DecayingModel extends AppModel
|
||||||
return $models;
|
return $models;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update($force=false)
|
public function update($force=false, $user)
|
||||||
{
|
{
|
||||||
$new_models = $this->__load_models($force);
|
$new_models = $this->__load_models($force);
|
||||||
$temp = $this->find('all', array(
|
$temp = $this->find('all', array(
|
||||||
|
@ -150,13 +150,13 @@ class DecayingModel extends AppModel
|
||||||
if ($force || $new_model['version'] > $existing_model['version']) {
|
if ($force || $new_model['version'] > $existing_model['version']) {
|
||||||
$new_model['id'] = $existing_model['id'];
|
$new_model['id'] = $existing_model['id'];
|
||||||
$this->save($new_model);
|
$this->save($new_model);
|
||||||
$this->DecayingModelMapping->resetMappingForModel($new_model);
|
$this->DecayingModelMapping->resetMappingForModel($new_model, $user);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$this->create();
|
$this->create();
|
||||||
$this->save($new_model);
|
$this->save($new_model);
|
||||||
$new_model['id'] = $this->Model->id;
|
$new_model['id'] = $this->Model->id;
|
||||||
$this->DecayingModelMapping->resetMappingForModel($new_model);
|
$this->DecayingModelMapping->resetMappingForModel($new_model, $user);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -210,7 +210,7 @@ class DecayingModel extends AppModel
|
||||||
try {
|
try {
|
||||||
$model = $this->fetchModel($user, $id, $full, $conditions);
|
$model = $this->fetchModel($user, $id, $full, $conditions);
|
||||||
$models[] = $model;
|
$models[] = $model;
|
||||||
} catch (MethodNotAllowedException $e) {
|
} catch (NotFoundException $e) {
|
||||||
// Just don't add the model to the result
|
// Just don't add the model to the result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -246,7 +246,7 @@ class DecayingModel extends AppModel
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($full) {
|
if ($full) {
|
||||||
$decayingModel['DecayingModel']['attribute_types'] = $this->DecayingModelMapping->getAssociatedTypes($user, $decayingModel['DecayingModel']['id']);
|
$decayingModel['DecayingModel']['attribute_types'] = $this->DecayingModelMapping->getAssociatedTypes($user, $decayingModel);
|
||||||
}
|
}
|
||||||
return $decayingModel;
|
return $decayingModel;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,11 +6,6 @@ class DecayingModelMapping extends AppModel
|
||||||
public $actsAs = array('Containable');
|
public $actsAs = array('Containable');
|
||||||
|
|
||||||
public $validate = array(
|
public $validate = array(
|
||||||
'org_id' => array(
|
|
||||||
'valueNotEmpty' => array(
|
|
||||||
'rule' => array('valueNotEmpty'),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
'attribute_type' => array(
|
'attribute_type' => array(
|
||||||
'valueNotEmpty' => array(
|
'valueNotEmpty' => array(
|
||||||
'rule' => array('valueNotEmpty'),
|
'rule' => array('valueNotEmpty'),
|
||||||
|
@ -30,14 +25,8 @@ class DecayingModelMapping extends AppModel
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
private $__default_type_mapping_reverse = array();
|
public function resetMappingForModel($new_model, $user) {
|
||||||
|
|
||||||
public function resetMappingForModel($new_model) {
|
|
||||||
if (!isset($new_model['org_id'])) {
|
|
||||||
$new_model['org_id'] = null;
|
|
||||||
}
|
|
||||||
$this->deleteAll(array(
|
$this->deleteAll(array(
|
||||||
'DecayingModelMapping.org_id' => $new_model['org_id'],
|
|
||||||
'model_id' => $new_model['model_id']
|
'model_id' => $new_model['model_id']
|
||||||
));
|
));
|
||||||
|
|
||||||
|
@ -47,9 +36,6 @@ class DecayingModelMapping extends AppModel
|
||||||
'attribute_type' => $type,
|
'attribute_type' => $type,
|
||||||
'model_id' => $new_model['model_id']
|
'model_id' => $new_model['model_id']
|
||||||
);
|
);
|
||||||
if (!is_null($new_model['org_id'])) {
|
|
||||||
$to_save['org_id'] = $new_model['org_id'];
|
|
||||||
}
|
|
||||||
$data[] = $to_save;
|
$data[] = $to_save;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,47 +49,47 @@ class DecayingModelMapping extends AppModel
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAssociatedTypes($user, $model_id) {
|
public function getAssociatedTypes($user, $model) {
|
||||||
$decaying_model = $this->DecayingModel->find('first', array(
|
if (is_numeric($model)) {
|
||||||
'conditions' => array('id' => $model_id),
|
$model = $this->DecayingModel->fetchModel($user, $model, false);
|
||||||
'recursive' => -1,
|
}
|
||||||
));
|
$decaying_model = isset($model['DecayingModel']) ? $model['DecayingModel'] : $model;
|
||||||
if (empty($decaying_model)) {
|
if ($decaying_model['isDefault']) {
|
||||||
$associated_types = array();
|
|
||||||
} else {
|
|
||||||
$decaying_model = $decaying_model['DecayingModel'];
|
|
||||||
$associated_types = $decaying_model['attribute_types'];
|
$associated_types = $decaying_model['attribute_types'];
|
||||||
|
} else {
|
||||||
$temp = $this->find('list', array(
|
$temp = $this->find('list', array(
|
||||||
'conditions' => array(
|
'conditions' => array(
|
||||||
'OR' => array(
|
'model_id' => $decaying_model['id']
|
||||||
array('org_id' => $user['Organisation']['id']),
|
|
||||||
array('org_id' => null),
|
|
||||||
),
|
|
||||||
'model_id' => $model_id
|
|
||||||
),
|
),
|
||||||
'recursive' => -1,
|
'recursive' => -1,
|
||||||
'fields' => array('attribute_type')
|
'fields' => array('attribute_type')
|
||||||
));
|
));
|
||||||
$associated_types = array_values(array_unique(array_merge($associated_types, $temp)));
|
$associated_types = array_values($temp);
|
||||||
}
|
}
|
||||||
return $associated_types;
|
return $associated_types;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAssociatedModels($user, $attribute_type = array()) {
|
public function getAssociatedModels($user, $attribute_type = array()) {
|
||||||
$conditions = array(
|
|
||||||
'OR' => array(
|
|
||||||
array('org_id' => $user['Organisation']['id']),
|
|
||||||
array('org_id' => null),
|
|
||||||
)
|
|
||||||
);
|
|
||||||
if (!empty($attribute_type)) {
|
|
||||||
$conditions['attribute_type'] = $attribute_type;
|
|
||||||
}
|
|
||||||
$associated_models = $this->find('all', array(
|
$associated_models = $this->find('all', array(
|
||||||
'conditions' => $conditions,
|
'conditions' => array(
|
||||||
|
'attribute_type' => $attribute_type,
|
||||||
|
'OR' => array(
|
||||||
|
'DecayingModel.org_id' => $user['org_id'],
|
||||||
|
'DecayingModel.all_orgs' => true
|
||||||
|
)
|
||||||
|
),
|
||||||
'recursive' => -1,
|
'recursive' => -1,
|
||||||
// 'group' => 'attribute_type',
|
'fields' => array('attribute_type', 'model_id'),
|
||||||
'fields' => array('attribute_type', 'model_id')
|
'joins' => array( // joins has to be done to enforce ACL
|
||||||
|
array(
|
||||||
|
'table' => 'decaying_models',
|
||||||
|
'alias' => 'DecayingModel',
|
||||||
|
'type' => 'INNER',
|
||||||
|
'conditions' => array(
|
||||||
|
'DecayingModel.id = DecayingModelMapping.model_id'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
));
|
));
|
||||||
$associated_models = Hash::combine($associated_models, '{n}.DecayingModelMapping.model_id', '{n}.DecayingModelMapping.model_id', '{n}.DecayingModelMapping.attribute_type');
|
$associated_models = Hash::combine($associated_models, '{n}.DecayingModelMapping.model_id', '{n}.DecayingModelMapping.model_id', '{n}.DecayingModelMapping.attribute_type');
|
||||||
return $associated_models;
|
return $associated_models;
|
||||||
|
|
Loading…
Reference in New Issue