fix: [UI] Error message when trying to add invalid attribute to object

pull/8613/head
Jakub Onderka 2022-09-28 10:11:57 +02:00
parent f720706a4f
commit ed98b42efc
4 changed files with 55 additions and 16 deletions

View File

@ -431,7 +431,7 @@ class ObjectsController extends AppController
$savedObject = array();
if (!is_numeric($objectToSave)) {
$object_validation_errors = array();
foreach($objectToSave as $field => $field_errors) {
foreach ($objectToSave as $field => $field_errors) {
$object_validation_errors[] = sprintf('%s: %s', $field, implode(', ', $field_errors));
}
$error_message = __('Object could not be saved.') . PHP_EOL . implode(PHP_EOL, $object_validation_errors);
@ -458,12 +458,10 @@ class ObjectsController extends AppController
return $this->RestResponse->saveFailResponse('Objects', 'edit', false, $id, $this->response->type());
}
} else {
$message = __('Object attributes saved.');
if ($this->request->is('ajax')) {
$this->autoRender = false;
if (is_numeric($objectToSave)) {
$this->MispObject->Event->unpublishEvent($event);
return new CakeResponse(array('body'=> json_encode(array('saved' => true, 'success' => $message)), 'status'=>200, 'type' => 'json'));
return new CakeResponse(array('body'=> json_encode(array('saved' => true, 'success' => __('Object attributes saved.'))), 'status'=>200, 'type' => 'json'));
} else {
return new CakeResponse(array('body'=> json_encode(array('saved' => true, 'errors' => $error_message)), 'status'=>200, 'type' => 'json'));
}

View File

@ -6336,11 +6336,33 @@ class Event extends AppModel
return $message;
}
public function processModuleResultsData($user, $resolved_data, $id, $default_comment = '', $jobId = false, $adhereToWarninglists = false, $event_level = false)
/**
* @param array $user
* @param array $resolved_data
* @param int $id
* @param string $default_comment
* @param int|false $jobId
* @param bool $adhereToWarninglists
* @param bool $event_level
* @return int|string
* @throws JsonException
*/
public function processModuleResultsData(array $user, $resolved_data, $id, $default_comment = '', $jobId = false, $adhereToWarninglists = false, $event_level = false)
{
$event = $this->find('first', [
'recursive' => -1,
'conditions' => ['id' => $id],
]);
if (empty($event)) {
throw new Exception("Event with ID `$id` not found.");
}
if ($jobId) {
$this->Job = ClassRegistry::init('Job');
$this->Job->id = $jobId;
/** @var EventLock $eventLock */
$eventLock = ClassRegistry::init('EventLock');
$eventLock->insertLockBackgroundJob($event['Event']['id'], $jobId);
}
$failed_attributes = $failed_objects = $failed_object_attributes = $failed_reports = 0;
$saved_attributes = $saved_objects = $saved_object_attributes = $saved_reports = 0;
@ -6386,6 +6408,7 @@ class Event extends AppModel
}
}
} else {
$this->Attribute->logDropped($user, $attribute);
$failed_attributes++;
$lastAttributeError = $this->Attribute->validationErrors;
$original_uuid = $this->__findOriginalUUID(
@ -6447,7 +6470,7 @@ class Event extends AppModel
if (isset($initial_attributes[$object_relation]) && in_array($object_attribute['value'], $initial_attributes[$object_relation])) {
continue;
}
if ($this->__saveObjectAttribute($object_attribute, $default_comment, $id, $initial_object_id, $user)) {
if ($this->__saveObjectAttribute($object_attribute, $default_comment, $event, $initial_object_id, $user)) {
$saved_object_attributes++;
} else {
$failed_object_attributes++;
@ -6480,7 +6503,7 @@ class Event extends AppModel
if ($this->Object->save($object)) {
$object_id = $this->Object->id;
foreach ($object['Attribute'] as $object_attribute) {
if ($this->__saveObjectAttribute($object_attribute, $default_comment, $id, $object_id, $user)) {
if ($this->__saveObjectAttribute($object_attribute, $default_comment, $event, $object_id, $user)) {
$saved_object_attributes++;
} else {
$failed_object_attributes++;
@ -6590,7 +6613,7 @@ class Event extends AppModel
}
if ($saved_attributes > 0 || $saved_objects > 0 || $saved_reports > 0) {
$this->unpublishEvent($id);
$this->unpublishEvent($event);
}
if ($event_level) {
return $saved_attributes + $saved_object_attributes + $saved_reports;
@ -6653,6 +6676,7 @@ class Event extends AppModel
if ($jobId) {
$this->Job->saveField('message', 'Processing complete. ' . $message);
$this->Job->saveField('progress', 100);
$eventLock->deleteBackgroundJobLock($event['Event']['id'], $jobId);
}
return $message;
}
@ -6739,10 +6763,19 @@ class Event extends AppModel
return (!empty($original_uuid)) ? $original_uuid['Object']['uuid'] : $original_uuid;
}
private function __saveObjectAttribute($attribute, $default_comment, $event_id, $object_id, $user)
/**
* @param array $attribute
* @param string $default_comment
* @param array $event
* @param int $object_id
* @param array $user
* @return array|bool|mixed
* @throws Exception
*/
private function __saveObjectAttribute(array $attribute, $default_comment, array $event, $object_id, array $user)
{
$attribute['object_id'] = $object_id;
$attribute['event_id'] = $event_id;
$attribute['event_id'] = $event['Event']['id'];
if (empty($attribute['comment'])) {
$attribute['comment'] = $default_comment;
}
@ -6750,17 +6783,19 @@ class Event extends AppModel
$attribute = $this->Attribute->onDemandEncrypt($attribute);
}
$this->Attribute->create();
$attribute_save = $this->Attribute->save($attribute);
$attribute_save = $this->Attribute->save($attribute, ['parentEvent' => $event]);
if ($attribute_save) {
if (!empty($attribute['Tag'])) {
foreach ($attribute['Tag'] as $tag) {
$tag_id = $this->Attribute->AttributeTag->Tag->captureTag($tag, $user);
$relationship_type = empty($tag['relationship_type']) ? false : $tag['relationship_type'];
if ($tag_id) {
$this->Attribute->AttributeTag->attachTagToAttribute($this->Attribute->id, $event_id, $tag_id, !empty($tag['local']), $relationship_type);
$this->Attribute->AttributeTag->attachTagToAttribute($this->Attribute->id, $event['Event']['id'], $tag_id, !empty($tag['local']), $relationship_type);
}
}
}
} else {
$this->Attribute->logDropped($user, $attribute);
}
return $attribute_save;
}

View File

@ -91,7 +91,7 @@ class EventTag extends AppModel
/**
* @param int $event_id
* @param int $tagId
* @param array $tag
* @param bool $nothingToChange
* @return bool
* @throws Exception

View File

@ -851,6 +851,14 @@ class MispObject extends AppModel
return $object;
}
/**
* @param array $object
* @param array $objectToSave
* @param bool $onlyAddNewAttribute
* @param array $user
* @return array|int
* @throws JsonException
*/
public function deltaMerge(array $object, array $objectToSave, $onlyAddNewAttribute=false, array $user)
{
if (!isset($objectToSave['Object'])) {
@ -965,7 +973,6 @@ class MispObject extends AppModel
}
} else { // we only add the new attribute
$newAttribute = $objectToSave['Attribute'][0];
$this->Event->Attribute->create();
$newAttribute['event_id'] = $object['Object']['event_id'];
$newAttribute['object_id'] = $object['Object']['id'];
// Set seen of object at attribute level
@ -980,10 +987,9 @@ class MispObject extends AppModel
(!array_key_exists('last_seen', $object['Object']) && !is_null($object['Object']['last_seen']))
) {
$newAttribute['last_seen'] = $object['Object']['last_seen'];
$different = true;
}
$saveAttributeResult = $this->Attribute->saveAttributes(array($newAttribute), $user);
return $saveAttributeResult ? $this->id : $this->validationErrors;
return $saveAttributeResult ? $this->id : $this->Attribute->validationErrors;
}
return $this->id;
}