Add the data after the change to a element edited log entry, so you can easily view the changes in log detail pages

This commit is contained in:
Jan Böhmer 2023-05-14 21:41:00 +02:00
parent 3c724a227a
commit 923e40ed8f
14 changed files with 136 additions and 22 deletions

View file

@ -73,13 +73,13 @@ class RevertLogColumn extends AbstractColumn
{
if (
$context instanceof CollectionElementDeleted
|| ($context instanceof ElementDeletedLogEntry && $context->hasOldDataInformations())
|| ($context instanceof ElementDeletedLogEntry && $context->hasOldDataInformation())
) {
$icon = 'fa-trash-restore';
$title = $this->translator->trans('log.undo.undelete');
} elseif (
$context instanceof ElementCreatedLogEntry
|| ($context instanceof ElementEditedLogEntry && $context->hasOldDataInformations())
|| ($context instanceof ElementEditedLogEntry && $context->hasOldDataInformation())
) {
$icon = 'fa-undo';
$title = $this->translator->trans('log.undo.undo');

View file

@ -236,7 +236,7 @@ class LogDataTable implements DataTableTypeInterface
'href' => function ($value, AbstractLogEntry $context) {
if (
($context instanceof TimeTravelInterface
&& $context->hasOldDataInformations())
&& $context->hasOldDataInformation())
|| $context instanceof CollectionElementDeleted
) {
try {

View file

@ -0,0 +1,41 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Entity\Contracts;
interface LogWithNewDataInterface
{
/**
* Checks if this entry has information about the new data.
* @return bool
*/
public function hasNewDataInformation(): bool;
/**
* Returns the new data for this entry.
*/
public function getNewData(): array;
/**
* Sets the new data for this entry.
* @return $this
*/
public function setNewData(array $new_data): self;
}

View file

@ -31,7 +31,7 @@ interface TimeTravelInterface
*
* @return bool true if this entry has information about the changed data
*/
public function hasOldDataInformations(): bool;
public function hasOldDataInformation(): bool;
/**
* Returns the data the entity had before this log entry.

View file

@ -88,7 +88,7 @@ class ElementDeletedLogEntry extends AbstractLogEntry implements TimeTravelInter
return $this;
}
public function hasOldDataInformations(): bool
public function hasOldDataInformation(): bool
{
return !empty($this->extra['o']);
}

View file

@ -25,6 +25,7 @@ namespace App\Entity\LogSystem;
use App\Entity\Base\AbstractDBElement;
use App\Entity\Contracts\LogWithCommentInterface;
use App\Entity\Contracts\LogWithEventUndoInterface;
use App\Entity\Contracts\LogWithNewDataInterface;
use App\Entity\Contracts\TimeTravelInterface;
use Doctrine\ORM\Mapping as ORM;
use InvalidArgumentException;
@ -32,7 +33,7 @@ use InvalidArgumentException;
/**
* @ORM\Entity()
*/
class ElementEditedLogEntry extends AbstractLogEntry implements TimeTravelInterface, LogWithCommentInterface, LogWithEventUndoInterface
class ElementEditedLogEntry extends AbstractLogEntry implements TimeTravelInterface, LogWithCommentInterface, LogWithEventUndoInterface, LogWithNewDataInterface
{
protected string $typeString = 'element_edited';
@ -49,7 +50,7 @@ class ElementEditedLogEntry extends AbstractLogEntry implements TimeTravelInterf
*/
public function hasChangedFieldsInfo(): bool
{
return isset($this->extra['f']) || $this->hasOldDataInformations();
return isset($this->extra['f']) || $this->hasOldDataInformation();
}
/**
@ -59,7 +60,7 @@ class ElementEditedLogEntry extends AbstractLogEntry implements TimeTravelInterf
*/
public function getChangedFields(): array
{
if ($this->hasOldDataInformations()) {
if ($this->hasOldDataInformation()) {
return array_keys($this->getOldData());
}
@ -92,7 +93,29 @@ class ElementEditedLogEntry extends AbstractLogEntry implements TimeTravelInterf
return $this;
}
public function hasOldDataInformations(): bool
public function hasNewDataInformation(): bool
{
return !empty($this->extra['n']);
}
public function getNewData(): array
{
return $this->extra['n'] ?? [];
}
/**
* Sets the old data for this entry.
*
* @return $this
*/
public function setNewData(array $new_data): self
{
$this->extra['n'] = $new_data;
return $this;
}
public function hasOldDataInformation(): bool
{
return !empty($this->extra['d']);
}

View file

@ -78,11 +78,12 @@ class EventLoggerSubscriber implements EventSubscriber
protected bool $save_changed_fields;
protected bool $save_changed_data;
protected bool $save_removed_data;
protected bool $save_new_data;
protected PropertyAccessorInterface $propertyAccessor;
public function __construct(EventLogger $logger, SerializerInterface $serializer, EventCommentHelper $commentHelper,
bool $save_changed_fields, bool $save_changed_data, bool $save_removed_data, PropertyAccessorInterface $propertyAccessor,
EventUndoHelper $eventUndoHelper)
bool $save_changed_fields, bool $save_changed_data, bool $save_removed_data, bool $save_new_data,
PropertyAccessorInterface $propertyAccessor, EventUndoHelper $eventUndoHelper)
{
$this->logger = $logger;
$this->serializer = $serializer;
@ -93,6 +94,8 @@ class EventLoggerSubscriber implements EventSubscriber
$this->save_changed_fields = $save_changed_fields;
$this->save_changed_data = $save_changed_data;
$this->save_removed_data = $save_removed_data;
//This option only makes sense if save_changed_data is true
$this->save_new_data = $save_new_data && $save_changed_data;
}
public function onFlush(OnFlushEventArgs $eventArgs): void
@ -150,6 +153,7 @@ class EventLoggerSubscriber implements EventSubscriber
$log->setTargetElementID($undoEvent->getDeletedElementID());
}
}
$this->logger->log($log);
}
}
@ -301,6 +305,24 @@ class EventLoggerSubscriber implements EventSubscriber
}, ARRAY_FILTER_USE_BOTH);
}
/**
* Restrict the length of every string in the given array to MAX_STRING_LENGTH, to save memory in the case of very
* long strings (e.g. images in notes)
* @param array $fields
* @return array
*/
protected function fieldLengthRestrict(array $fields): array
{
return array_map(
static function ($value) {
if (is_string($value)) {
return mb_strimwidth($value, 0, self::MAX_STRING_LENGTH, '...');
}
return $value;
}, $fields);
}
protected function saveChangeSet(AbstractDBElement $entity, AbstractLogEntry $logEntry, EntityManagerInterface $em, bool $element_deleted = false): void
{
$uow = $em->getUnitOfWork();
@ -314,20 +336,24 @@ class EventLoggerSubscriber implements EventSubscriber
} else { //Otherwise we have to get it from entity changeset
$changeSet = $uow->getEntityChangeSet($entity);
$old_data = array_combine(array_keys($changeSet), array_column($changeSet, 0));
//If save_new_data is enabled, we extract it from the change set
if ($this->save_new_data) {
$new_data = array_combine(array_keys($changeSet), array_column($changeSet, 1));
}
}
$old_data = $this->filterFieldRestrictions($entity, $old_data);
//Restrict length of string fields, to save memory...
$old_data = array_map(
static function ($value) {
if (is_string($value)) {
return mb_strimwidth($value, 0, self::MAX_STRING_LENGTH, '...');
}
return $value;
}, $old_data);
$old_data = $this->fieldLengthRestrict($old_data);
$logEntry->setOldData($old_data);
if (!empty($new_data)) {
$new_data = $this->filterFieldRestrictions($entity, $new_data);
$new_data = $this->fieldLengthRestrict($new_data);
$logEntry->setNewData($new_data);
}
}
/**

View file

@ -194,7 +194,7 @@ class TimeTravel
public function applyEntry(AbstractDBElement $element, TimeTravelInterface $logEntry): void
{
//Skip if this does not provide any info...
if (!$logEntry->hasOldDataInformations()) {
if (!$logEntry->hasOldDataInformation()) {
return;
}
if (!$element instanceof TimeStampableInterface) {