2020-02-02 21:24:29 +01:00
|
|
|
<?php
|
2020-03-15 13:56:31 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-02-02 21:24:29 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
2020-02-22 18:14:36 +01:00
|
|
|
* 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.
|
2020-02-02 21:24:29 +01:00
|
|
|
*
|
|
|
|
* 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
|
2020-02-22 18:14:36 +01:00
|
|
|
* GNU Affero General Public License for more details.
|
2020-02-02 21:24:29 +01:00
|
|
|
*
|
2020-02-22 18:14:36 +01:00
|
|
|
* 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/>.
|
2020-02-02 21:24:29 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\EventSubscriber;
|
|
|
|
|
2020-02-29 22:53:53 +01:00
|
|
|
use App\Entity\Attachments\Attachment;
|
2020-02-02 21:24:29 +01:00
|
|
|
use App\Entity\Base\AbstractDBElement;
|
|
|
|
use App\Entity\LogSystem\AbstractLogEntry;
|
2020-02-29 22:53:53 +01:00
|
|
|
use App\Entity\LogSystem\CollectionElementDeleted;
|
2020-02-02 21:24:29 +01:00
|
|
|
use App\Entity\LogSystem\ElementCreatedLogEntry;
|
|
|
|
use App\Entity\LogSystem\ElementDeletedLogEntry;
|
|
|
|
use App\Entity\LogSystem\ElementEditedLogEntry;
|
2020-03-28 17:19:02 +01:00
|
|
|
use App\Entity\Parameters\AbstractParameter;
|
2020-02-29 22:53:53 +01:00
|
|
|
use App\Entity\Parts\PartLot;
|
|
|
|
use App\Entity\PriceInformations\Orderdetail;
|
|
|
|
use App\Entity\PriceInformations\Pricedetail;
|
2020-02-23 21:04:16 +01:00
|
|
|
use App\Entity\UserSystem\User;
|
2020-02-23 00:44:52 +01:00
|
|
|
use App\Services\LogSystem\EventCommentHelper;
|
2020-02-02 21:24:29 +01:00
|
|
|
use App\Services\LogSystem\EventLogger;
|
2020-03-01 19:46:48 +01:00
|
|
|
use App\Services\LogSystem\EventUndoHelper;
|
2020-02-02 21:24:29 +01:00
|
|
|
use Doctrine\Common\EventSubscriber;
|
2020-02-29 22:53:53 +01:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2020-02-02 21:24:29 +01:00
|
|
|
use Doctrine\ORM\Event\OnFlushEventArgs;
|
|
|
|
use Doctrine\ORM\Event\PostFlushEventArgs;
|
|
|
|
use Doctrine\ORM\Events;
|
|
|
|
use Doctrine\Persistence\Event\LifecycleEventArgs;
|
2020-02-29 22:53:53 +01:00
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
|
2020-02-16 23:48:57 +01:00
|
|
|
use Symfony\Component\Serializer\SerializerInterface;
|
2020-02-02 21:24:29 +01:00
|
|
|
|
|
|
|
class EventLoggerSubscriber implements EventSubscriber
|
|
|
|
{
|
2020-02-29 22:53:53 +01:00
|
|
|
/** @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'],
|
2020-03-29 22:16:06 +02:00
|
|
|
AbstractParameter::class => ['element'],
|
2020-02-29 22:53:53 +01:00
|
|
|
];
|
2020-02-23 21:04:16 +01:00
|
|
|
|
|
|
|
protected const MAX_STRING_LENGTH = 2000;
|
|
|
|
|
2020-02-02 21:24:29 +01:00
|
|
|
protected $logger;
|
2020-02-16 23:48:57 +01:00
|
|
|
protected $serializer;
|
2020-02-23 00:44:52 +01:00
|
|
|
protected $eventCommentHelper;
|
2020-03-01 19:46:48 +01:00
|
|
|
protected $eventUndoHelper;
|
2020-02-23 21:04:16 +01:00
|
|
|
protected $save_changed_fields;
|
|
|
|
protected $save_changed_data;
|
|
|
|
protected $save_removed_data;
|
2020-02-29 22:53:53 +01:00
|
|
|
protected $propertyAccessor;
|
2020-02-02 21:24:29 +01:00
|
|
|
|
2020-02-23 21:04:16 +01:00
|
|
|
public function __construct(EventLogger $logger, SerializerInterface $serializer, EventCommentHelper $commentHelper,
|
2020-03-01 19:46:48 +01:00
|
|
|
bool $save_changed_fields, bool $save_changed_data, bool $save_removed_data, PropertyAccessorInterface $propertyAccessor,
|
|
|
|
EventUndoHelper $eventUndoHelper)
|
2020-02-02 21:24:29 +01:00
|
|
|
{
|
|
|
|
$this->logger = $logger;
|
2020-02-16 23:48:57 +01:00
|
|
|
$this->serializer = $serializer;
|
2020-02-23 00:44:52 +01:00
|
|
|
$this->eventCommentHelper = $commentHelper;
|
2020-02-29 22:53:53 +01:00
|
|
|
$this->propertyAccessor = $propertyAccessor;
|
2020-03-01 19:46:48 +01:00
|
|
|
$this->eventUndoHelper = $eventUndoHelper;
|
2020-02-23 21:04:16 +01:00
|
|
|
|
|
|
|
$this->save_changed_fields = $save_changed_fields;
|
|
|
|
$this->save_changed_data = $save_changed_data;
|
|
|
|
$this->save_removed_data = $save_removed_data;
|
2020-02-02 21:24:29 +01:00
|
|
|
}
|
|
|
|
|
2020-03-15 13:56:31 +01:00
|
|
|
public function onFlush(OnFlushEventArgs $eventArgs): void
|
2020-02-02 21:24:29 +01:00
|
|
|
{
|
|
|
|
$em = $eventArgs->getEntityManager();
|
|
|
|
$uow = $em->getUnitOfWork();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Log changes and deletions of entites.
|
|
|
|
* We can not log persist here, because the entities do not have IDs yet...
|
|
|
|
*/
|
|
|
|
|
|
|
|
foreach ($uow->getScheduledEntityUpdates() as $entity) {
|
|
|
|
if ($this->validEntity($entity)) {
|
2020-02-29 22:53:53 +01:00
|
|
|
$this->logElementEdited($entity, $em);
|
2020-02-02 21:24:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($uow->getScheduledEntityDeletions() as $entity) {
|
|
|
|
if ($this->validEntity($entity)) {
|
2020-02-29 22:53:53 +01:00
|
|
|
$this->logElementDeleted($entity, $em);
|
2020-02-02 21:24:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$uow->computeChangeSets();
|
|
|
|
}
|
|
|
|
|
2020-03-15 13:56:31 +01:00
|
|
|
public function postPersist(LifecycleEventArgs $args): void
|
2020-02-02 21:24:29 +01:00
|
|
|
{
|
2020-02-29 22:53:53 +01:00
|
|
|
//Create an log entry, we have to do this post persist, cause we have to know the ID
|
2020-02-02 21:24:29 +01:00
|
|
|
|
|
|
|
/** @var AbstractDBElement $entity */
|
|
|
|
$entity = $args->getObject();
|
|
|
|
if ($this->validEntity($entity)) {
|
|
|
|
$log = new ElementCreatedLogEntry($entity);
|
2020-02-23 00:44:52 +01:00
|
|
|
//Add user comment to log entry
|
|
|
|
if ($this->eventCommentHelper->isMessageSet()) {
|
|
|
|
$log->setComment($this->eventCommentHelper->getMessage());
|
|
|
|
}
|
2020-03-01 19:46:48 +01:00
|
|
|
if ($this->eventUndoHelper->isUndo()) {
|
|
|
|
$undoEvent = $this->eventUndoHelper->getUndoneEvent();
|
|
|
|
|
|
|
|
$log->setUndoneEvent($undoEvent, $this->eventUndoHelper->getMode());
|
|
|
|
|
2020-03-15 13:56:31 +01:00
|
|
|
if ($undoEvent instanceof ElementDeletedLogEntry && $undoEvent->getTargetClass() === $log->getTargetClass()) {
|
2020-03-01 19:46:48 +01:00
|
|
|
$log->setTargetElementID($undoEvent->getTargetID());
|
|
|
|
}
|
2020-03-15 13:56:31 +01:00
|
|
|
if ($undoEvent instanceof CollectionElementDeleted && $undoEvent->getDeletedElementClass() === $log->getTargetClass()) {
|
2020-03-01 19:46:48 +01:00
|
|
|
$log->setTargetElementID($undoEvent->getDeletedElementID());
|
|
|
|
}
|
|
|
|
}
|
2020-02-02 21:24:29 +01:00
|
|
|
$this->logger->log($log);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 13:56:31 +01:00
|
|
|
public function postFlush(PostFlushEventArgs $args): void
|
2020-02-02 21:24:29 +01:00
|
|
|
{
|
|
|
|
$em = $args->getEntityManager();
|
|
|
|
$uow = $em->getUnitOfWork();
|
|
|
|
// If the we have added any ElementCreatedLogEntries added in postPersist, we flush them here.
|
|
|
|
if ($uow->hasPendingInsertions()) {
|
|
|
|
$em->flush();
|
|
|
|
}
|
2020-02-23 00:44:52 +01:00
|
|
|
|
|
|
|
//Clear the message provided by user.
|
|
|
|
$this->eventCommentHelper->clearMessage();
|
2020-03-01 19:46:48 +01:00
|
|
|
$this->eventUndoHelper->clearUndoneEvent();
|
2020-02-02 21:24:29 +01:00
|
|
|
}
|
|
|
|
|
2020-03-15 13:56:31 +01:00
|
|
|
/**
|
|
|
|
* Check if the given element class has restrictions to its fields.
|
|
|
|
*
|
|
|
|
* @return bool True if there are restrictions, and further checking is needed
|
|
|
|
*/
|
|
|
|
public function hasFieldRestrictions(AbstractDBElement $element): bool
|
|
|
|
{
|
2020-03-29 22:37:27 +02:00
|
|
|
foreach (array_keys(static::FIELD_BLACKLIST) as $class) {
|
2020-03-15 13:56:31 +01:00
|
|
|
if (is_a($element, $class)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the field of the given element should be saved (if it is not blacklisted).
|
|
|
|
*
|
|
|
|
* @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, true)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//By default allow every field.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSubscribedEvents()
|
|
|
|
{
|
|
|
|
return[
|
|
|
|
Events::onFlush,
|
|
|
|
Events::postPersist,
|
|
|
|
Events::postFlush,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-02-29 22:53:53 +01:00
|
|
|
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());
|
|
|
|
}
|
2020-03-01 19:46:48 +01:00
|
|
|
if ($this->eventUndoHelper->isUndo()) {
|
|
|
|
$log->setUndoneEvent($this->eventUndoHelper->getUndoneEvent(), $this->eventUndoHelper->getMode());
|
|
|
|
}
|
2020-02-29 22:53:53 +01:00
|
|
|
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) {
|
2020-03-15 13:56:31 +01:00
|
|
|
if (in_array($field, $whitelist, true)) {
|
2020-02-29 22:53:53 +01:00
|
|
|
$changed = $this->propertyAccessor->getValue($entity, $field);
|
|
|
|
$log = new CollectionElementDeleted($changed, $mapping['inversedBy'], $entity);
|
2020-03-01 19:46:48 +01:00
|
|
|
if ($this->eventUndoHelper->isUndo()) {
|
|
|
|
$log->setUndoneEvent($this->eventUndoHelper->getUndoneEvent(), $this->eventUndoHelper->getMode());
|
|
|
|
}
|
2020-02-29 22:53:53 +01:00
|
|
|
$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));
|
2020-03-07 21:36:33 +01:00
|
|
|
//Remove lastModified field, as this is always changed (gives us no additional info)
|
|
|
|
$changed_fields = array_diff($changed_fields, ['lastModified']);
|
2020-02-29 22:53:53 +01:00
|
|
|
$log->setChangedFields($changed_fields);
|
|
|
|
}
|
|
|
|
//Add user comment to log entry
|
|
|
|
if ($this->eventCommentHelper->isMessageSet()) {
|
|
|
|
$log->setComment($this->eventCommentHelper->getMessage());
|
|
|
|
}
|
2020-03-01 19:46:48 +01:00
|
|
|
if ($this->eventUndoHelper->isUndo()) {
|
|
|
|
$log->setUndoneEvent($this->eventUndoHelper->getUndoneEvent(), $this->eventUndoHelper->getMode());
|
|
|
|
}
|
2020-02-29 22:53:53 +01:00
|
|
|
$this->logger->log($log);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter out every forbidden field and return the cleaned array.
|
2020-03-15 13:56:31 +01:00
|
|
|
*
|
2020-02-29 22:53:53 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function filterFieldRestrictions(AbstractDBElement $element, array $fields): array
|
|
|
|
{
|
2020-03-01 19:46:48 +01:00
|
|
|
unset($fields['lastModified']);
|
|
|
|
|
2020-03-15 13:56:31 +01:00
|
|
|
if (! $this->hasFieldRestrictions($element)) {
|
2020-02-29 22:53:53 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-03-29 23:13:25 +02:00
|
|
|
protected function saveChangeSet(AbstractDBElement $entity, AbstractLogEntry $logEntry, EntityManagerInterface $em, bool $element_deleted = false): void
|
2020-02-29 22:53:53 +01:00
|
|
|
{
|
|
|
|
$uow = $em->getUnitOfWork();
|
|
|
|
|
2020-03-15 13:56:31 +01:00
|
|
|
if (! $logEntry instanceof ElementEditedLogEntry && ! $logEntry instanceof ElementDeletedLogEntry) {
|
2020-02-29 22:53:53 +01:00
|
|
|
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);
|
2020-03-01 19:46:48 +01:00
|
|
|
$old_data = array_combine(array_keys($changeSet), array_column($changeSet, 0));
|
2020-02-29 22:53:53 +01:00
|
|
|
}
|
2020-03-01 19:46:48 +01:00
|
|
|
$old_data = $this->filterFieldRestrictions($entity, $old_data);
|
2020-02-29 22:53:53 +01:00
|
|
|
|
|
|
|
//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);
|
|
|
|
}
|
2020-03-01 19:46:48 +01:00
|
|
|
|
2020-02-02 21:24:29 +01:00
|
|
|
/**
|
|
|
|
* Check if the given entity can be logged.
|
2020-03-15 13:56:31 +01:00
|
|
|
*
|
2020-02-02 21:24:29 +01:00
|
|
|
* @return bool True, if the given entity can be logged.
|
|
|
|
*/
|
|
|
|
protected function validEntity(object $entity): bool
|
|
|
|
{
|
|
|
|
//Dont log logentries itself!
|
2020-03-15 13:56:31 +01:00
|
|
|
if ($entity instanceof AbstractDBElement && ! $entity instanceof AbstractLogEntry) {
|
2020-02-02 21:24:29 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-15 13:56:31 +01:00
|
|
|
}
|