mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-24 02:38:50 +02:00
Allow to show collection elements that were deleted before by timetravel
This commit is contained in:
parent
b5bc096972
commit
f604022e49
9 changed files with 386 additions and 78 deletions
|
@ -20,24 +20,45 @@
|
|||
|
||||
namespace App\EventSubscriber;
|
||||
|
||||
use App\Entity\Attachments\Attachment;
|
||||
use App\Entity\Attachments\AttachmentType;
|
||||
use App\Entity\Base\AbstractDBElement;
|
||||
use App\Entity\Base\AbstractPartsContainingDBElement;
|
||||
use App\Entity\Base\AbstractStructuralDBElement;
|
||||
use App\Entity\LogSystem\AbstractLogEntry;
|
||||
use App\Entity\LogSystem\CollectionElementDeleted;
|
||||
use App\Entity\LogSystem\ElementCreatedLogEntry;
|
||||
use App\Entity\LogSystem\ElementDeletedLogEntry;
|
||||
use App\Entity\LogSystem\ElementEditedLogEntry;
|
||||
use App\Entity\Parts\PartLot;
|
||||
use App\Entity\PriceInformations\Orderdetail;
|
||||
use App\Entity\PriceInformations\Pricedetail;
|
||||
use App\Entity\UserSystem\User;
|
||||
use App\Services\LogSystem\EventCommentHelper;
|
||||
use App\Services\LogSystem\EventLogger;
|
||||
use Doctrine\Common\EventSubscriber;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\Event\OnFlushEventArgs;
|
||||
use Doctrine\ORM\Event\PostFlushEventArgs;
|
||||
use Doctrine\ORM\Events;
|
||||
use Doctrine\ORM\UnitOfWork;
|
||||
use Doctrine\Persistence\Event\LifecycleEventArgs;
|
||||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
|
||||
class EventLoggerSubscriber implements EventSubscriber
|
||||
{
|
||||
/** @var array The given fields will not be saved, because they contain sensitive informations */
|
||||
protected const FIELD_BLACKLIST = [
|
||||
User::class => ['password', 'need_pw_change', 'googleAuthenticatorSecret', 'backupCodes', 'trustedDeviceCookieVersion', 'pw_reset_token', 'backupCodesGenerationDate'],
|
||||
];
|
||||
|
||||
/** @var array If elements of the given class are deleted, a log for the given fields will be triggered */
|
||||
protected const TRIGGER_ASSOCIATION_LOG_WHITELIST = [
|
||||
PartLot::class => ['part'],
|
||||
Orderdetail::class => ['part'],
|
||||
Pricedetail::class => ['orderdetail'],
|
||||
Attachment::class => ['element'],
|
||||
];
|
||||
|
||||
protected const MAX_STRING_LENGTH = 2000;
|
||||
|
||||
|
@ -47,13 +68,15 @@ class EventLoggerSubscriber implements EventSubscriber
|
|||
protected $save_changed_fields;
|
||||
protected $save_changed_data;
|
||||
protected $save_removed_data;
|
||||
protected $propertyAccessor;
|
||||
|
||||
public function __construct(EventLogger $logger, SerializerInterface $serializer, EventCommentHelper $commentHelper,
|
||||
bool $save_changed_fields, bool $save_changed_data, bool $save_removed_data)
|
||||
bool $save_changed_fields, bool $save_changed_data, bool $save_removed_data, PropertyAccessorInterface $propertyAccessor)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
$this->serializer = $serializer;
|
||||
$this->eventCommentHelper = $commentHelper;
|
||||
$this->propertyAccessor = $propertyAccessor;
|
||||
|
||||
$this->save_changed_fields = $save_changed_fields;
|
||||
$this->save_changed_data = $save_changed_data;
|
||||
|
@ -72,68 +95,22 @@ class EventLoggerSubscriber implements EventSubscriber
|
|||
|
||||
foreach ($uow->getScheduledEntityUpdates() as $entity) {
|
||||
if ($this->validEntity($entity)) {
|
||||
$log = new ElementEditedLogEntry($entity);
|
||||
if ($this->save_changed_data) {
|
||||
$this->saveChangeSet($entity, $log, $uow);
|
||||
} elseif ($this->save_changed_fields) {
|
||||
$changed_fields = array_keys($uow->getEntityChangeSet($entity));
|
||||
$log->setChangedFields($changed_fields);
|
||||
}
|
||||
//Add user comment to log entry
|
||||
if ($this->eventCommentHelper->isMessageSet()) {
|
||||
$log->setComment($this->eventCommentHelper->getMessage());
|
||||
}
|
||||
$this->logger->log($log);
|
||||
$this->logElementEdited($entity, $em);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($uow->getScheduledEntityDeletions() as $entity) {
|
||||
if ($this->validEntity($entity)) {
|
||||
$log = new ElementDeletedLogEntry($entity);
|
||||
//Add user comment to log entry
|
||||
if ($this->eventCommentHelper->isMessageSet()) {
|
||||
$log->setComment($this->eventCommentHelper->getMessage());
|
||||
}
|
||||
if ($this->save_removed_data) {
|
||||
$this->saveChangeSet($entity, $log, $uow);
|
||||
}
|
||||
$this->logger->log($log);
|
||||
$this->logElementDeleted($entity, $em);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$uow->computeChangeSets();
|
||||
}
|
||||
|
||||
protected function saveChangeSet(AbstractDBElement $entity, AbstractLogEntry $logEntry, UnitOfWork $uow): void
|
||||
{
|
||||
if (!$logEntry instanceof ElementEditedLogEntry && !$logEntry instanceof ElementDeletedLogEntry) {
|
||||
throw new \InvalidArgumentException('$logEntry must be ElementEditedLogEntry or ElementDeletedLogEntry!');
|
||||
}
|
||||
|
||||
$changeSet = $uow->getEntityChangeSet($entity);
|
||||
$old_data = array_diff(array_combine(array_keys($changeSet), array_column($changeSet, 0)), [null]);
|
||||
//Restrict length of string fields, to save memory...
|
||||
$old_data = array_map(function ($value) {
|
||||
if (is_string($value)) {
|
||||
return mb_strimwidth($value, 0, self::MAX_STRING_LENGTH, '...');
|
||||
}
|
||||
|
||||
return $value;
|
||||
}, $old_data);
|
||||
|
||||
//Dont save sensitive fields to log
|
||||
if ($entity instanceof User) {
|
||||
unset($old_data['password'], $old_data['pw_reset_token'], $old_data['backupCodes']);
|
||||
}
|
||||
|
||||
|
||||
$logEntry->setOldData($old_data);
|
||||
}
|
||||
|
||||
public function postPersist(LifecycleEventArgs $args)
|
||||
{
|
||||
//Create an log entry
|
||||
//Create an log entry, we have to do this post persist, cause we have to know the ID
|
||||
|
||||
/** @var AbstractDBElement $entity */
|
||||
$entity = $args->getObject();
|
||||
|
@ -160,6 +137,140 @@ class EventLoggerSubscriber implements EventSubscriber
|
|||
$this->eventCommentHelper->clearMessage();
|
||||
}
|
||||
|
||||
protected function logElementDeleted(AbstractDBElement $entity, EntityManagerInterface $em): void
|
||||
{
|
||||
$log = new ElementDeletedLogEntry($entity);
|
||||
//Add user comment to log entry
|
||||
if ($this->eventCommentHelper->isMessageSet()) {
|
||||
$log->setComment($this->eventCommentHelper->getMessage());
|
||||
}
|
||||
if ($this->save_removed_data) {
|
||||
//The 4th param is important here, as we delete the element...
|
||||
$this->saveChangeSet($entity, $log, $em, true);
|
||||
}
|
||||
$this->logger->log($log);
|
||||
|
||||
//Check if we have to log CollectionElementDeleted entries
|
||||
if ($this->save_changed_data) {
|
||||
$metadata = $em->getClassMetadata(get_class($entity));
|
||||
$mappings = $metadata->getAssociationMappings();
|
||||
//Check if class is whitelisted for CollectionElementDeleted entry
|
||||
foreach (static::TRIGGER_ASSOCIATION_LOG_WHITELIST as $class => $whitelist) {
|
||||
if (is_a($entity, $class)) {
|
||||
//Check names
|
||||
foreach ($mappings as $field => $mapping) {
|
||||
if (in_array($field, $whitelist)) {
|
||||
$changed = $this->propertyAccessor->getValue($entity, $field);
|
||||
$log = new CollectionElementDeleted($changed, $mapping['inversedBy'], $entity);
|
||||
$this->logger->log($log);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function logElementEdited(AbstractDBElement $entity, EntityManagerInterface $em): void
|
||||
{
|
||||
$uow = $em->getUnitOfWork();
|
||||
|
||||
$log = new ElementEditedLogEntry($entity);
|
||||
if ($this->save_changed_data) {
|
||||
$this->saveChangeSet($entity, $log, $em);
|
||||
} elseif ($this->save_changed_fields) {
|
||||
$changed_fields = array_keys($uow->getEntityChangeSet($entity));
|
||||
$log->setChangedFields($changed_fields);
|
||||
}
|
||||
//Add user comment to log entry
|
||||
if ($this->eventCommentHelper->isMessageSet()) {
|
||||
$log->setComment($this->eventCommentHelper->getMessage());
|
||||
}
|
||||
$this->logger->log($log);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given element class has restrictions to its fields
|
||||
* @param AbstractDBElement $element
|
||||
* @return bool True if there are restrictions, and further checking is needed
|
||||
*/
|
||||
public function hasFieldRestrictions(AbstractDBElement $element): bool
|
||||
{
|
||||
foreach (static::FIELD_BLACKLIST as $class => $blacklist) {
|
||||
if (is_a($element, $class)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter out every forbidden field and return the cleaned array.
|
||||
* @param AbstractDBElement $element
|
||||
* @param array $fields
|
||||
* @return array
|
||||
*/
|
||||
protected function filterFieldRestrictions(AbstractDBElement $element, array $fields): array
|
||||
{
|
||||
if (!$this->hasFieldRestrictions($element)) {
|
||||
return $fields;
|
||||
}
|
||||
|
||||
return array_filter($fields, function ($value, $key) use ($element) {
|
||||
//Associative array (save changed data) case
|
||||
if (is_string($key)) {
|
||||
return $this->shouldFieldBeSaved($element, $key);
|
||||
}
|
||||
|
||||
return $this->shouldFieldBeSaved($element, $value);
|
||||
}, ARRAY_FILTER_USE_BOTH);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the field of the given element should be saved (if it is not blacklisted).
|
||||
* @param AbstractDBElement $element
|
||||
* @param string $field_name
|
||||
* @return bool
|
||||
*/
|
||||
public function shouldFieldBeSaved(AbstractDBElement $element, string $field_name): bool
|
||||
{
|
||||
foreach (static::FIELD_BLACKLIST as $class => $blacklist) {
|
||||
if (is_a($element, $class) && in_array($field_name, $blacklist)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//By default allow every field.
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function saveChangeSet(AbstractDBElement $entity, AbstractLogEntry $logEntry, EntityManagerInterface $em, $element_deleted = false): void
|
||||
{
|
||||
$uow = $em->getUnitOfWork();
|
||||
|
||||
if (!$logEntry instanceof ElementEditedLogEntry && !$logEntry instanceof ElementDeletedLogEntry) {
|
||||
throw new \InvalidArgumentException('$logEntry must be ElementEditedLogEntry or ElementDeletedLogEntry!');
|
||||
}
|
||||
|
||||
if ($element_deleted) { //If the element was deleted we can use getOriginalData to save its content
|
||||
$old_data = $uow->getOriginalEntityData($entity);
|
||||
} else { //Otherwise we have to get it from entity changeset
|
||||
$changeSet = $uow->getEntityChangeSet($entity);
|
||||
$old_data = array_diff(array_combine(array_keys($changeSet), array_column($changeSet, 0)), [null]);
|
||||
}
|
||||
$this->filterFieldRestrictions($entity, $old_data);
|
||||
|
||||
//Restrict length of string fields, to save memory...
|
||||
$old_data = array_map(function ($value) {
|
||||
if (is_string($value)) {
|
||||
return mb_strimwidth($value, 0, self::MAX_STRING_LENGTH, '...');
|
||||
}
|
||||
|
||||
return $value;
|
||||
}, $old_data);
|
||||
|
||||
$logEntry->setOldData($old_data);
|
||||
}
|
||||
/**
|
||||
* Check if the given entity can be logged.
|
||||
* @param object $entity
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue