2019-03-08 16:19:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
App::uses('AppModel', 'Model');
|
|
|
|
|
|
|
|
class DecayingModel extends AppModel
|
|
|
|
{
|
|
|
|
public $actsAs = array('Containable');
|
|
|
|
|
|
|
|
public $hasMany = array(
|
2019-04-03 10:05:45 +02:00
|
|
|
'DecayingModelMapping' => array(
|
|
|
|
'className' => 'DecayingModelMapping',
|
|
|
|
'foreignKey' => 'model_id',
|
|
|
|
'dependent' => true
|
|
|
|
)
|
2019-03-08 16:19:07 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
public function afterFind($results, $primary = false) {
|
|
|
|
foreach ($results as $k => $v) {
|
|
|
|
if (!empty($v['DecayingModel']['parameters'])) {
|
|
|
|
$decoded = json_decode($v['DecayingModel']['parameters'], true);
|
|
|
|
if ($decoded === null) {
|
|
|
|
$decoded = array();
|
|
|
|
}
|
|
|
|
$results[$k]['DecayingModel']['parameters'] = $decoded;
|
|
|
|
}
|
2019-04-03 11:51:29 +02:00
|
|
|
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;
|
|
|
|
}
|
2019-03-08 16:19:07 +01:00
|
|
|
}
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function beforeValidate($options = array()) {
|
2019-04-03 10:05:45 +02:00
|
|
|
parent::beforeValidate();
|
|
|
|
if (!empty($this->data['DecayingModel']['parameters']) && !is_array($this->data['DecayingModel']['parameters'])) {
|
2019-03-08 16:19:07 +01:00
|
|
|
$encoded = json_decode($this->data['DecayingModel']['parameters'], true);
|
|
|
|
if ($encoded !== null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-03 11:51:29 +02:00
|
|
|
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;
|
|
|
|
}
|
2019-03-08 16:19:07 +01:00
|
|
|
}
|
|
|
|
|
2019-04-03 10:05:45 +02:00
|
|
|
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']);
|
|
|
|
}
|
2019-04-03 11:51:29 +02:00
|
|
|
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']);
|
|
|
|
}
|
2019-04-03 10:05:45 +02:00
|
|
|
if (!isset($this->data['DecayingModel']['org_id'])) {
|
|
|
|
$this->data['DecayingModel']['org_id'] = Configure::read('MISP.host_org_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function __load_models($force = false)
|
|
|
|
{
|
|
|
|
$dir = new Folder(APP . 'files' . DS . 'misp-decaying-models' . DS . 'models');
|
|
|
|
$files = $dir->find('.*\.json');
|
|
|
|
$models = array();
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$file = new File($dir->pwd() . DS . $file);
|
|
|
|
$models[] = json_decode($file->read(), true);
|
|
|
|
$file->close();
|
|
|
|
}
|
|
|
|
return $models;
|
|
|
|
}
|
|
|
|
|
2019-04-03 11:51:29 +02:00
|
|
|
public function update($force=false)
|
2019-04-03 10:05:45 +02:00
|
|
|
{
|
|
|
|
$new_models = $this->__load_models($force);
|
|
|
|
$temp = $this->find('all', array(
|
|
|
|
'recursive' => -1
|
|
|
|
));
|
|
|
|
$existing_models = array();
|
|
|
|
foreach ($temp as $k => $model) {
|
|
|
|
$existing_models[$model['DecayingModel']['uuid']] = $model['DecayingModel'];
|
|
|
|
}
|
|
|
|
foreach ($new_models as $k => $new_model) {
|
|
|
|
if (isset($existing_models[$new_model['uuid']])) {
|
|
|
|
$existing_model = $existing_models[$new_model['uuid']];
|
|
|
|
if ($force || $new_model['version'] > $existing_model['version']) {
|
|
|
|
$new_model['id'] = $existing_model['id'];
|
|
|
|
$this->save($new_model);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->create();
|
|
|
|
$this->save($new_model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-08 16:19:07 +01:00
|
|
|
public function fetchAllowedModels($user) {
|
|
|
|
$conditions = array();
|
|
|
|
if (!$user['Role']['perm_site_admin']) {
|
|
|
|
if ($user['Role']['perm_decaying']) {
|
|
|
|
$conditions['org_id'] = $user['Organisation']['id'];
|
|
|
|
} else {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$decayingModel = $this->find('all', array(
|
|
|
|
'conditions' => $conditions,
|
|
|
|
'recursive' => -1,
|
|
|
|
));
|
|
|
|
|
|
|
|
return $decayingModel;
|
|
|
|
}
|
|
|
|
|
2019-04-03 10:05:45 +02:00
|
|
|
public function checkAuthorisation($user, $id, $full=true) {
|
2019-03-08 16:19:07 +01:00
|
|
|
// fetch the bare template
|
|
|
|
$decayingModel = $this->find('first', array(
|
|
|
|
'conditions' => array('id' => $id),
|
2019-04-03 10:05:45 +02:00
|
|
|
// 'recursive' => -1,
|
2019-03-08 16:19:07 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
// if not found return false
|
|
|
|
if (empty($decayingModel)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//if the user is a site admin, return the template without question
|
|
|
|
if ($user['Role']['perm_site_admin']) {
|
|
|
|
return $decayingModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($user['Organisation']['id'] == $decayingModel['DecayingModel']['org_id'] && $user['Role']['perm_decaying']) {
|
|
|
|
return $decayingModel;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|