Merge pull request #7609 from JakubOnderka/code-cleanup-vol6

Code cleanup vol6
pull/7610/head
Jakub Onderka 2021-07-27 19:11:28 +02:00 committed by GitHub
commit c6183dbb98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 36 deletions

View File

@ -790,7 +790,7 @@ class AttributesController extends AppController
} }
} }
if ($existingAttribute['Attribute']['object_id']) { if ($existingAttribute['Attribute']['object_id']) {
$result = $this->Attribute->save($this->request->data, array('fieldList' => $this->Attribute->editableFields)); $result = $this->Attribute->save($this->request->data, array('fieldList' => Attribute::EDITABLE_FIELDS));
if ($result) { if ($result) {
$this->Attribute->AttributeTag->handleAttributeTags($this->Auth->user(), $this->request->data['Attribute'], $attribute['Event']['id'], $capture=true); $this->Attribute->AttributeTag->handleAttributeTags($this->Auth->user(), $this->request->data['Attribute'], $attribute['Event']['id'], $capture=true);
} }

View File

@ -24,6 +24,7 @@ App::uses('Model', 'Model');
App::uses('LogableBehavior', 'Assets.models/behaviors'); App::uses('LogableBehavior', 'Assets.models/behaviors');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth'); App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');
App::uses('RandomTool', 'Tools'); App::uses('RandomTool', 'Tools');
class AppModel extends Model class AppModel extends Model
{ {
public $name; public $name;
@ -33,13 +34,8 @@ class AppModel extends Model
*/ */
private $loadedPubSubTool; private $loadedPubSubTool;
public $loadedKafkaPubTool = false; /** @var KafkaPubTool */
public $loadedKafkaPubTool;
public $start = 0;
public $assetCache = [];
public $inserted_ids = array();
/** @var null|Redis */ /** @var null|Redis */
private static $__redisConnection = null; private static $__redisConnection = null;
@ -61,7 +57,7 @@ class AppModel extends Model
// deprecated, use $db_changes // deprecated, use $db_changes
// major -> minor -> hotfix -> requires_logout // major -> minor -> hotfix -> requires_logout
public $old_db_changes = array( const OLD_DB_CHANGES = array(
2 => array( 2 => array(
4 => array( 4 => array(
18 => false, 19 => false, 20 => false, 25 => false, 27 => false, 18 => false, 19 => false, 20 => false, 25 => false, 27 => false,
@ -78,7 +74,7 @@ class AppModel extends Model
) )
); );
public $db_changes = array( const DB_CHANGES = array(
1 => false, 2 => false, 3 => false, 4 => true, 5 => false, 6 => false, 1 => false, 2 => false, 3 => false, 4 => true, 5 => false, 6 => false,
7 => false, 8 => false, 9 => false, 10 => false, 11 => false, 12 => false, 7 => false, 8 => false, 9 => false, 10 => false, 11 => false, 12 => false,
13 => false, 14 => false, 15 => false, 18 => false, 19 => false, 20 => false, 13 => false, 14 => false, 15 => false, 18 => false, 19 => false, 20 => false,
@ -107,14 +103,6 @@ class AppModel extends Model
), ),
); );
public function afterSave($created, $options = array())
{
if ($created) {
$this->inserted_ids[] = $this->getInsertID();
}
return true;
}
public function isAcceptedDatabaseError($errorMessage, $dataSource) public function isAcceptedDatabaseError($errorMessage, $dataSource)
{ {
$isAccepted = false; $isAccepted = false;
@ -2375,12 +2363,12 @@ class AppModel extends Model
} }
} }
public function findUpgrades($db_version) protected function findUpgrades($db_version)
{ {
$updates = array(); $updates = array();
if (strpos($db_version, '.')) { if (strpos($db_version, '.')) {
$version = explode('.', $db_version); $version = explode('.', $db_version);
foreach ($this->old_db_changes as $major => $rest) { foreach (self::OLD_DB_CHANGES as $major => $rest) {
if ($major < $version[0]) { if ($major < $version[0]) {
continue; continue;
} elseif ($major == $version[0]) { } elseif ($major == $version[0]) {
@ -2403,7 +2391,7 @@ class AppModel extends Model
} }
$db_version = 0; $db_version = 0;
} }
foreach ($this->db_changes as $db_change => $requiresLogout) { foreach (self::DB_CHANGES as $db_change => $requiresLogout) {
if ($db_version < $db_change) { if ($db_version < $db_change) {
$updates[$db_change] = $requiresLogout; $updates[$db_change] = $requiresLogout;
} }

View File

@ -13,6 +13,7 @@ App::uses('ComplexTypeTool', 'Tools');
* @property Event $Event * @property Event $Event
* @property AttributeTag $AttributeTag * @property AttributeTag $AttributeTag
* @property Sighting $Sighting * @property Sighting $Sighting
* @property MispObject $Object
* @property-read array $typeDefinitions * @property-read array $typeDefinitions
* @property-read array $categoryDefinitions * @property-read array $categoryDefinitions
*/ */
@ -45,11 +46,21 @@ class Attribute extends AppModel
'distribution' => array('desc' => 'Describes who will have access to the attribute.') 'distribution' => array('desc' => 'Describes who will have access to the attribute.')
); );
public $defaultFields = array( const EDITABLE_FIELDS = [
'id', 'event_id', 'object_id', 'object_relation', 'category', 'type', 'value', 'to_ids', 'uuid', 'timestamp', 'distribution', 'sharing_group_id', 'comment', 'deleted', 'disable_correlation', 'first_seen', 'last_seen' 'timestamp',
); 'category',
'value',
public $editableFields = array('timestamp', 'category', 'value', 'value1', 'value2', 'to_ids', 'comment', 'distribution', 'sharing_group_id', 'deleted', 'disable_correlation', 'first_seen', 'last_seen'); 'value1',
'value2',
'to_ids',
'comment',
'distribution',
'sharing_group_id',
'deleted',
'disable_correlation',
'first_seen',
'last_seen',
];
public $distributionDescriptions = array( public $distributionDescriptions = array(
0 => array('desc' => 'This field determines the current distribution of the event', 'formdesc' => "This setting will only allow members of your organisation on this server to see it."), 0 => array('desc' => 'This field determines the current distribution of the event', 'formdesc' => "This setting will only allow members of your organisation on this server to see it."),
@ -3789,7 +3800,7 @@ class Attribute extends AppModel
$attribute['distribution'] = 5; $attribute['distribution'] = 5;
} }
} }
$fieldList = $this->editableFields; $fieldList = self::EDITABLE_FIELDS;
if (empty($existingAttribute)) { if (empty($existingAttribute)) {
$addableFieldList = array('event_id', 'type', 'uuid'); $addableFieldList = array('event_id', 'type', 'uuid');
$fieldList = array_merge($fieldList, $addableFieldList); $fieldList = array_merge($fieldList, $addableFieldList);
@ -3857,12 +3868,8 @@ class Attribute extends AppModel
return true; return true;
} }
public function deleteAttribute($id, $user, $hard = false) public function deleteAttribute($id, array $user, $hard = false)
{ {
$this->id = $id;
if (!$this->exists()) {
return false;
}
$result = $this->fetchAttributes($user, array( $result = $this->fetchAttributes($user, array(
'conditions' => array('Attribute.id' => $id), 'conditions' => array('Attribute.id' => $id),
'flatten' => 1, 'flatten' => 1,
@ -3887,7 +3894,6 @@ class Attribute extends AppModel
} }
} }
} }
$date = new DateTime();
if ($hard) { if ($hard) {
$save = $this->delete($id); $save = $this->delete($id);
} else { } else {
@ -3899,7 +3905,7 @@ class Attribute extends AppModel
$result['Attribute']['to_ids'] = 0; $result['Attribute']['to_ids'] = 0;
} }
$result['Attribute']['deleted'] = 1; $result['Attribute']['deleted'] = 1;
$result['Attribute']['timestamp'] = $date->getTimestamp(); $result['Attribute']['timestamp'] = time();
$save = $this->save($result); $save = $this->save($result);
$object_refs = $this->Object->ObjectReference->find('all', array( $object_refs = $this->Object->ObjectReference->find('all', array(
'conditions' => array( 'conditions' => array(

View File

@ -376,6 +376,8 @@ class Event extends AppModel
); );
} }
private $assetCache = [];
public function beforeDelete($cascade = true) public function beforeDelete($cascade = true)
{ {
// blocklist the event UUID if the feature is enabled // blocklist the event UUID if the feature is enabled

View File

@ -884,7 +884,7 @@ class MispObject extends AppModel
$newAttribute['event_id'] = $object['Object']['event_id']; $newAttribute['event_id'] = $object['Object']['event_id'];
$newAttribute['object_id'] = $object['Object']['id']; $newAttribute['object_id'] = $object['Object']['id'];
$newAttribute['timestamp'] = $date->getTimestamp(); $newAttribute['timestamp'] = $date->getTimestamp();
$result = $this->Event->Attribute->save(array('Attribute' => $newAttribute), array('fieldList' => $this->Attribute->editableFields)); $result = $this->Event->Attribute->save(array('Attribute' => $newAttribute), array('fieldList' => Attribute::EDITABLE_FIELDS));
if ($result) { if ($result) {
$this->Event->Attribute->AttributeTag->handleAttributeTags($user, $newAttribute, $newAttribute['event_id'], $capture=true); $this->Event->Attribute->AttributeTag->handleAttributeTags($user, $newAttribute, $newAttribute['event_id'], $capture=true);
} }
@ -926,7 +926,7 @@ class MispObject extends AppModel
} }
foreach ($object['Attribute'] as $origKey => $originalAttribute) { foreach ($object['Attribute'] as $origKey => $originalAttribute) {
$originalAttribute['deleted'] = 1; $originalAttribute['deleted'] = 1;
$this->Event->Attribute->save($originalAttribute, array('fieldList' => $this->Attribute->editableFields)); $this->Event->Attribute->save($originalAttribute, array('fieldList' => Attribute::EDITABLE_FIELDS));
} }
} }
} else { // we only add the new attribute } else { // we only add the new attribute