From f071cef22c97993af2aa0c8ae1bdec7e9cc146b2 Mon Sep 17 00:00:00 2001 From: Jakub Onderka Date: Sat, 23 Oct 2021 20:48:03 +0200 Subject: [PATCH] fix: [internal] Proposal validation --- app/Controller/ShadowAttributesController.php | 2 +- app/Model/ShadowAttribute.php | 63 ++++++++++--------- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/app/Controller/ShadowAttributesController.php b/app/Controller/ShadowAttributesController.php index 4142db415..0311ede11 100644 --- a/app/Controller/ShadowAttributesController.php +++ b/app/Controller/ShadowAttributesController.php @@ -708,7 +708,7 @@ class ShadowAttributesController extends AppController } throw new InternalErrorException(__('Could not save the proposal. Errors: %s', $message)); } else { - $this->Flash->error(__('The ShadowAttribute could not be saved. Please, try again.')); + $this->Flash->error(__('The proposed Attribute could not be saved. Please, try again.')); } } } else { diff --git a/app/Model/ShadowAttribute.php b/app/Model/ShadowAttribute.php index 66540162a..be134f48f 100644 --- a/app/Model/ShadowAttribute.php +++ b/app/Model/ShadowAttribute.php @@ -129,13 +129,13 @@ class ShadowAttribute extends AppModel 'first_seen' => array( 'rule' => array('datetimeOrNull'), 'required' => false, - 'message' => array('Invalid ISO 8601 format') + 'message' => array('Invalid ISO 8601 format'), ), 'last_seen' => array( 'datetimeOrNull' => array( 'rule' => array('datetimeOrNull'), 'required' => false, - 'message' => array('Invalid ISO 8601 format') + 'message' => array('Invalid ISO 8601 format'), ), 'validateLastSeenValue' => array( 'rule' => array('validateLastSeenValue'), @@ -173,7 +173,7 @@ class ShadowAttribute extends AppModel $compositeTypes = $this->getCompositeTypes(); // explode composite types in value1 and value2 $pieces = explode('|', $this->data['ShadowAttribute']['value']); - if (in_array($this->data['ShadowAttribute']['type'], $compositeTypes)) { + if (in_array($this->data['ShadowAttribute']['type'], $compositeTypes, true)) { if (2 != count($pieces)) { throw new InternalErrorException('Composite type, but value not explodable'); } @@ -300,51 +300,54 @@ class ShadowAttribute extends AppModel public function beforeValidate($options = array()) { - parent::beforeValidate(); - // remove leading and trailing blanks - //$this->trimStringFields(); // TODO - - if (!isset($this->data['ShadowAttribute']['comment'])) { - $this->data['ShadowAttribute']['comment'] = ''; - } - - if (!isset($this->data['ShadowAttribute']['type'])) { + $proposal = &$this->data['ShadowAttribute']; + if (!isset($proposal['type'])) { + $this->invalidate('type', 'No value provided.'); return false; } + if (!isset($proposal['comment'])) { + $proposal['comment'] = ''; + } + // make some changes to the inserted value - if (isset($this->data['ShadowAttribute']['value'])) { - $value = trim($this->data['ShadowAttribute']['value']); - $value = ComplexTypeTool::refangValue($value, $this->data['ShadowAttribute']['type']); - $value = $this->Attribute->modifyBeforeValidation($this->data['ShadowAttribute']['type'], $value); - $this->data['ShadowAttribute']['value'] = $value; + if (isset($proposal['value'])) { + $value = trim($proposal['value']); + $value = ComplexTypeTool::refangValue($value, $proposal['type']); + $value = $this->Attribute->modifyBeforeValidation($proposal['type'], $value); + $proposal['value'] = $value; } - if (!isset($this->data['ShadowAttribute']['org'])) { - $this->data['ShadowAttribute']['org'] = ''; + if (!isset($proposal['org'])) { + $proposal['org'] = ''; } - if (empty($this->data['ShadowAttribute']['timestamp'])) { - $date = new DateTime(); - $this->data['ShadowAttribute']['timestamp'] = $date->getTimestamp(); + if (empty($proposal['timestamp'])) { + $proposal['timestamp'] = time(); } - if (!isset($this->data['ShadowAttribute']['proposal_to_delete'])) { - $this->data['ShadowAttribute']['proposal_to_delete'] = 0; + if (!isset($proposal['proposal_to_delete'])) { + $proposal['proposal_to_delete'] = 0; } // generate UUID if it doesn't exist - if (empty($this->data['ShadowAttribute']['uuid'])) { - $this->data['ShadowAttribute']['uuid'] = CakeText::uuid(); + if (empty($proposal['uuid'])) { + $proposal['uuid'] = CakeText::uuid(); } else { - $this->data['ShadowAttribute']['uuid'] = strtolower($this->data['ShadowAttribute']['uuid']); + $proposal['uuid'] = strtolower($proposal['uuid']); } - if (!empty($this->data['ShadowAttribute']['type']) && empty($this->data['ShadowAttribute']['category'])) { - $this->data['ShadowAttribute']['category'] = $this->Attribute->typeDefinitions[$this->data['ShadowAttribute']['type']]['default_category']; + if (empty($proposal['category'])) { + $proposal['category'] = $this->Attribute->typeDefinitions[$proposal['type']]['default_category']; + } + + if (isset($proposal['first_seen'])) { + $proposal['first_seen'] = $proposal['first_seen'] === '' ? null : $proposal['first_seen']; + } + if (isset($proposal['last_seen'])) { + $proposal['last_seen'] = $proposal['last_seen'] === '' ? null : $proposal['last_seen']; } - // always return true, otherwise the object cannot be saved return true; }