mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 09:35:49 +02:00
Log when elements are created, edited or removed.
This commit is contained in:
parent
9f5a4ddf95
commit
eb071b1780
7 changed files with 213 additions and 0 deletions
|
@ -73,6 +73,10 @@ services:
|
||||||
tags:
|
tags:
|
||||||
- { name: "doctrine.orm.entity_listener" }
|
- { name: "doctrine.orm.entity_listener" }
|
||||||
|
|
||||||
|
App\EventSubscriber\EventLoggerSubscriber:
|
||||||
|
tags:
|
||||||
|
- { name: 'doctrine.event_subscriber' }
|
||||||
|
|
||||||
App\EventSubscriber\MigrationListener:
|
App\EventSubscriber\MigrationListener:
|
||||||
tags:
|
tags:
|
||||||
- { name: 'doctrine.event_subscriber' }
|
- { name: 'doctrine.event_subscriber' }
|
||||||
|
|
|
@ -32,9 +32,14 @@ use App\Entity\Devices\DevicePart;
|
||||||
use App\Entity\Parts\Category;
|
use App\Entity\Parts\Category;
|
||||||
use App\Entity\Parts\Footprint;
|
use App\Entity\Parts\Footprint;
|
||||||
use App\Entity\Parts\Manufacturer;
|
use App\Entity\Parts\Manufacturer;
|
||||||
|
use App\Entity\Parts\MeasurementUnit;
|
||||||
use App\Entity\Parts\Part;
|
use App\Entity\Parts\Part;
|
||||||
|
use App\Entity\Parts\PartLot;
|
||||||
use App\Entity\Parts\Storelocation;
|
use App\Entity\Parts\Storelocation;
|
||||||
use App\Entity\Parts\Supplier;
|
use App\Entity\Parts\Supplier;
|
||||||
|
use App\Entity\PriceInformations\Currency;
|
||||||
|
use App\Entity\PriceInformations\Orderdetail;
|
||||||
|
use App\Entity\PriceInformations\Pricedetail;
|
||||||
use App\Entity\UserSystem\Group;
|
use App\Entity\UserSystem\Group;
|
||||||
use App\Entity\UserSystem\User;
|
use App\Entity\UserSystem\User;
|
||||||
use DateTime;
|
use DateTime;
|
||||||
|
@ -85,6 +90,11 @@ abstract class AbstractLogEntry extends AbstractDBElement
|
||||||
protected const TARGET_TYPE_PART = 10;
|
protected const TARGET_TYPE_PART = 10;
|
||||||
protected const TARGET_TYPE_STORELOCATION = 11;
|
protected const TARGET_TYPE_STORELOCATION = 11;
|
||||||
protected const TARGET_TYPE_SUPPLIER = 12;
|
protected const TARGET_TYPE_SUPPLIER = 12;
|
||||||
|
protected const TARGET_TYPE_PARTLOT = 13;
|
||||||
|
protected const TARGET_TYPE_CURRENCY = 14;
|
||||||
|
protected const TARGET_TYPE_ORDERDETAIL = 15;
|
||||||
|
protected const TARGET_TYPE_PRICEDETAIL = 16;
|
||||||
|
protected const TARGET_TYPE_MEASUREMENTUNIT = 17;
|
||||||
|
|
||||||
/** @var array This const is used to convert the numeric level to a PSR-3 compatible log level */
|
/** @var array This const is used to convert the numeric level to a PSR-3 compatible log level */
|
||||||
protected const LEVEL_ID_TO_STRING = [
|
protected const LEVEL_ID_TO_STRING = [
|
||||||
|
@ -111,6 +121,11 @@ abstract class AbstractLogEntry extends AbstractDBElement
|
||||||
self::TARGET_TYPE_PART => Part::class,
|
self::TARGET_TYPE_PART => Part::class,
|
||||||
self::TARGET_TYPE_STORELOCATION => Storelocation::class,
|
self::TARGET_TYPE_STORELOCATION => Storelocation::class,
|
||||||
self::TARGET_TYPE_SUPPLIER => Supplier::class,
|
self::TARGET_TYPE_SUPPLIER => Supplier::class,
|
||||||
|
self::TARGET_TYPE_PARTLOT => PartLot::class,
|
||||||
|
self::TARGET_TYPE_CURRENCY => Currency::class,
|
||||||
|
self::TARGET_TYPE_ORDERDETAIL => Orderdetail::class,
|
||||||
|
self::TARGET_TYPE_PRICEDETAIL => Pricedetail::class,
|
||||||
|
self::TARGET_TYPE_MEASUREMENTUNIT => MeasurementUnit::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
/** @var User The user which has caused this log entry
|
/** @var User The user which has caused this log entry
|
||||||
|
|
28
src/Entity/LogSystem/DBElement.php
Normal file
28
src/Entity/LogSystem/DBElement.php
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* 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)
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* 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 General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Entity\LogSystem;
|
||||||
|
|
||||||
|
|
||||||
|
class DBElement
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -24,6 +24,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace App\Entity\LogSystem;
|
namespace App\Entity\LogSystem;
|
||||||
|
|
||||||
|
use App\Entity\Base\AbstractDBElement;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -33,6 +34,13 @@ class ElementCreatedLogEntry extends AbstractLogEntry
|
||||||
{
|
{
|
||||||
protected $typeString = 'element_created';
|
protected $typeString = 'element_created';
|
||||||
|
|
||||||
|
public function __construct(AbstractDBElement $new_element)
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
$this->level = self::LEVEL_INFO;
|
||||||
|
$this->setTargetElement($new_element);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the instock when the part was created.
|
* Gets the instock when the part was created.
|
||||||
*
|
*
|
||||||
|
|
|
@ -24,6 +24,8 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace App\Entity\LogSystem;
|
namespace App\Entity\LogSystem;
|
||||||
|
|
||||||
|
use App\Entity\Base\AbstractDBElement;
|
||||||
|
use App\Entity\Contracts\NamedElementInterface;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -33,6 +35,32 @@ class ElementDeletedLogEntry extends AbstractLogEntry
|
||||||
{
|
{
|
||||||
protected $typeString = 'element_deleted';
|
protected $typeString = 'element_deleted';
|
||||||
|
|
||||||
|
public function __construct(AbstractDBElement $deleted_element)
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
$this->level = self::LEVEL_INFO;
|
||||||
|
$this->setTargetElement($deleted_element);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setTargetElement(?AbstractDBElement $element): AbstractLogEntry
|
||||||
|
{
|
||||||
|
parent::setTargetElement($element);
|
||||||
|
if ($element instanceof NamedElementInterface) {
|
||||||
|
$this->setOldName($element->getName());
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setOldName(string $old_name): self
|
||||||
|
{
|
||||||
|
$this->extra['n'] = $old_name;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function getOldName(): string
|
public function getOldName(): string
|
||||||
{
|
{
|
||||||
return $this->extra['n'];
|
return $this->extra['n'];
|
||||||
|
|
|
@ -24,6 +24,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace App\Entity\LogSystem;
|
namespace App\Entity\LogSystem;
|
||||||
|
|
||||||
|
use App\Entity\Base\AbstractDBElement;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -33,6 +34,14 @@ class ElementEditedLogEntry extends AbstractLogEntry
|
||||||
{
|
{
|
||||||
protected $typeString = 'element_edited';
|
protected $typeString = 'element_edited';
|
||||||
|
|
||||||
|
public function __construct(AbstractDBElement $changed_element)
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
$this->level = self::LEVEL_INFO;
|
||||||
|
|
||||||
|
$this->setTargetElement($changed_element);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the message associated with this edit change.
|
* Returns the message associated with this edit change.
|
||||||
*
|
*
|
||||||
|
|
121
src/EventSubscriber/EventLoggerSubscriber.php
Normal file
121
src/EventSubscriber/EventLoggerSubscriber.php
Normal file
|
@ -0,0 +1,121 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* 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)
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* 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 General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\EventSubscriber;
|
||||||
|
|
||||||
|
use App\Entity\Base\AbstractDBElement;
|
||||||
|
use App\Entity\LogSystem\AbstractLogEntry;
|
||||||
|
use App\Entity\LogSystem\ElementCreatedLogEntry;
|
||||||
|
use App\Entity\LogSystem\ElementDeletedLogEntry;
|
||||||
|
use App\Entity\LogSystem\ElementEditedLogEntry;
|
||||||
|
use App\Services\LogSystem\EventLogger;
|
||||||
|
use Doctrine\Common\EventSubscriber;
|
||||||
|
use Doctrine\ORM\Event\OnFlushEventArgs;
|
||||||
|
use Doctrine\ORM\Event\PostFlushEventArgs;
|
||||||
|
use Doctrine\ORM\Events;
|
||||||
|
use Doctrine\Persistence\Event\LifecycleEventArgs;
|
||||||
|
|
||||||
|
class EventLoggerSubscriber implements EventSubscriber
|
||||||
|
{
|
||||||
|
protected $logger;
|
||||||
|
|
||||||
|
public function __construct(EventLogger $logger)
|
||||||
|
{
|
||||||
|
$this->logger = $logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onFlush(OnFlushEventArgs $eventArgs)
|
||||||
|
{
|
||||||
|
$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)) {
|
||||||
|
$log = new ElementEditedLogEntry($entity);
|
||||||
|
$this->logger->log($log);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($uow->getScheduledEntityDeletions() as $entity) {
|
||||||
|
if ($this->validEntity($entity)) {
|
||||||
|
$log = new ElementDeletedLogEntry($entity);
|
||||||
|
$this->logger->log($log);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$uow->computeChangeSets();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postPersist(LifecycleEventArgs $args)
|
||||||
|
{
|
||||||
|
//Create an log entry
|
||||||
|
|
||||||
|
/** @var AbstractDBElement $entity */
|
||||||
|
$entity = $args->getObject();
|
||||||
|
if ($this->validEntity($entity)) {
|
||||||
|
$log = new ElementCreatedLogEntry($entity);
|
||||||
|
$this->logger->log($log);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postFlush(PostFlushEventArgs $args)
|
||||||
|
{
|
||||||
|
$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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the given entity can be logged.
|
||||||
|
* @param object $entity
|
||||||
|
* @return bool True, if the given entity can be logged.
|
||||||
|
*/
|
||||||
|
protected function validEntity(object $entity): bool
|
||||||
|
{
|
||||||
|
//Dont log logentries itself!
|
||||||
|
if ($entity instanceof AbstractDBElement && !$entity instanceof AbstractLogEntry) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function getSubscribedEvents()
|
||||||
|
{
|
||||||
|
return[
|
||||||
|
Events::onFlush,
|
||||||
|
Events::postPersist,
|
||||||
|
Events::postFlush
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue