Allow to undo a change from event log.

This commit is contained in:
Jan Böhmer 2020-03-01 19:46:48 +01:00
parent 15d25cf2b2
commit 5a5d7b24be
24 changed files with 659 additions and 30 deletions

View file

@ -34,7 +34,7 @@ use Symfony\Component\Serializer\Annotation\Groups;
* Every database table which are managed with this class (or a subclass of it)
* must have the table row "id"!! The ID is the unique key to identify the elements.
*
* @ORM\MappedSuperclass()
* @ORM\MappedSuperclass(repositoryClass="App\Repository\DBElementRepository")
*
* @ORM\EntityListeners({"App\Security\EntityListeners\ElementPermissionListener"})
*

View file

@ -31,7 +31,7 @@ use Symfony\Component\Validator\Constraints as Assert;
/**
* All subclasses of this class have an attribute "name".
*
* @ORM\MappedSuperclass(repositoryClass="App\Repository\UserRepository")
* @ORM\MappedSuperclass(repositoryClass="App\Repository\NamedDBElement")
* @ORM\HasLifecycleCallbacks()
*/
abstract class AbstractNamedDBElement extends AbstractDBElement implements NamedElementInterface, TimeStampableInterface

View file

@ -0,0 +1,55 @@
<?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 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;
use App\Entity\LogSystem\AbstractLogEntry;
interface LogWithEventUndoInterface
{
/**
* Checks if this element undoes another event.
* @return bool
*/
public function isUndoEvent(): bool;
/**
* Returns the ID of the undone event or null if no event is undone.
* @return int|null
*/
public function getUndoEventID(): ?int;
/**
* Sets the event that is undone, and the undo mode.
* @param AbstractLogEntry $event
* @param string $mode
* @return $this
*/
public function setUndoneEvent(AbstractLogEntry $event, string $mode = 'undo'): self;
/**
* Returns the mode how the event was undone:
* "undo" = Only a single event was applied to element
* "revert" = Element was reverted to the state it was to the timestamp of the log.
* @return string
*/
public function getUndoMode(): string;
}

View file

@ -368,6 +368,17 @@ abstract class AbstractLogEntry extends AbstractDBElement
return $this;
}
/**
* Sets the target ID of the element associated with this element.
* @param int $target_id
* @return $this
*/
public function setTargetElementID(int $target_id): self
{
$this->target_id = $target_id;
return $this;
}
public function getExtraData(): array
{
return $this->extra;

View file

@ -22,6 +22,7 @@ namespace App\Entity\LogSystem;
use App\Entity\Base\AbstractDBElement;
use App\Entity\Contracts\LogWithEventUndoInterface;
use App\Entity\Contracts\NamedElementInterface;
use Doctrine\ORM\Mapping as ORM;
@ -30,7 +31,7 @@ use Doctrine\ORM\Mapping as ORM;
* This log entry is created when an element is deleted, that is used in a collection of an other entity.
* This is needed to signal time travel, that it has to undelete the deleted entity.
*/
class CollectionElementDeleted extends AbstractLogEntry
class CollectionElementDeleted extends AbstractLogEntry implements LogWithEventUndoInterface
{
protected $typeString = 'collection_element_deleted';
protected $level = self::LEVEL_INFO;
@ -85,4 +86,51 @@ class CollectionElementDeleted extends AbstractLogEntry
{
return $this->extra['i'];
}
/**
* @inheritDoc
*/
public function isUndoEvent(): bool
{
return isset($this->extra['u']);
}
/**
* @inheritDoc
*/
public function getUndoEventID(): ?int
{
return $this->extra['u'] ?? null;
}
/**
* @inheritDoc
*/
public function setUndoneEvent(AbstractLogEntry $event, string $mode = 'undo'): LogWithEventUndoInterface
{
$this->extra['u'] = $event->getID();
if ($mode === 'undo') {
$this->extra['um'] = 1;
} elseif ($mode === 'revert') {
$this->extra['um'] = 2;
} else {
throw new \InvalidArgumentException('Passed invalid $mode!');
}
return $this;
}
/**
* @inheritDoc
*/
public function getUndoMode(): string
{
$mode_int = $this->extra['um'] ?? 1;
if ($mode_int === 1) {
return 'undo';
} else {
return 'revert';
}
}
}

View file

@ -44,12 +44,13 @@ namespace App\Entity\LogSystem;
use App\Entity\Base\AbstractDBElement;
use App\Entity\Contracts\LogWithCommentInterface;
use App\Entity\Contracts\LogWithEventUndoInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class ElementCreatedLogEntry extends AbstractLogEntry implements LogWithCommentInterface
class ElementCreatedLogEntry extends AbstractLogEntry implements LogWithCommentInterface, LogWithEventUndoInterface
{
protected $typeString = 'element_created';
@ -104,4 +105,51 @@ class ElementCreatedLogEntry extends AbstractLogEntry implements LogWithCommentI
$this->extra['m'] = $new_comment;
return $this;
}
/**
* @inheritDoc
*/
public function isUndoEvent(): bool
{
return isset($this->extra['u']);
}
/**
* @inheritDoc
*/
public function getUndoEventID(): ?int
{
return $this->extra['u'] ?? null;
}
/**
* @inheritDoc
*/
public function setUndoneEvent(AbstractLogEntry $event, string $mode = 'undo'): LogWithEventUndoInterface
{
$this->extra['u'] = $event->getID();
if ($mode === 'undo') {
$this->extra['um'] = 1;
} elseif ($mode === 'revert') {
$this->extra['um'] = 2;
} else {
throw new \InvalidArgumentException('Passed invalid $mode!');
}
return $this;
}
/**
* @inheritDoc
*/
public function getUndoMode(): string
{
$mode_int = $this->extra['um'] ?? 1;
if ($mode_int === 1) {
return 'undo';
} else {
return 'revert';
}
}
}

View file

@ -44,6 +44,7 @@ namespace App\Entity\LogSystem;
use App\Entity\Base\AbstractDBElement;
use App\Entity\Contracts\LogWithCommentInterface;
use App\Entity\Contracts\LogWithEventUndoInterface;
use App\Entity\Contracts\NamedElementInterface;
use App\Entity\Contracts\TimeTravelInterface;
use Doctrine\ORM\Mapping as ORM;
@ -51,7 +52,7 @@ use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class ElementDeletedLogEntry extends AbstractLogEntry implements TimeTravelInterface, LogWithCommentInterface
class ElementDeletedLogEntry extends AbstractLogEntry implements TimeTravelInterface, LogWithCommentInterface, LogWithEventUndoInterface
{
protected $typeString = 'element_deleted';
@ -137,4 +138,50 @@ class ElementDeletedLogEntry extends AbstractLogEntry implements TimeTravelInter
$this->extra['m'] = $new_comment;
return $this;
}
/**
* @inheritDoc
*/
public function isUndoEvent(): bool
{
return isset($this->extra['u']);
}
/**
* @inheritDoc
*/
public function getUndoEventID(): ?int
{
return $this->extra['u'] ?? null;
}
/**
* @inheritDoc
*/
public function setUndoneEvent(AbstractLogEntry $event, string $mode = 'undo'): LogWithEventUndoInterface
{
$this->extra['u'] = $event->getID();
if ($mode === 'undo') {
$this->extra['um'] = 1;
} elseif ($mode === 'revert') {
$this->extra['um'] = 2;
} else {
throw new \InvalidArgumentException('Passed invalid $mode!');
}
return $this;
}
/**
* @inheritDoc
*/
public function getUndoMode(): string
{
$mode_int = $this->extra['um'] ?? 1;
if ($mode_int === 1) {
return 'undo';
}
return 'revert';
}
}

View file

@ -44,13 +44,14 @@ namespace App\Entity\LogSystem;
use App\Entity\Base\AbstractDBElement;
use App\Entity\Contracts\LogWithCommentInterface;
use App\Entity\Contracts\LogWithEventUndoInterface;
use App\Entity\Contracts\TimeTravelInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class ElementEditedLogEntry extends AbstractLogEntry implements TimeTravelInterface, LogWithCommentInterface
class ElementEditedLogEntry extends AbstractLogEntry implements TimeTravelInterface, LogWithCommentInterface, LogWithEventUndoInterface
{
protected $typeString = 'element_edited';
@ -150,4 +151,51 @@ class ElementEditedLogEntry extends AbstractLogEntry implements TimeTravelInterf
$this->extra['m'] = $new_comment;
return $this;
}
/**
* @inheritDoc
*/
public function isUndoEvent(): bool
{
return isset($this->extra['u']);
}
/**
* @inheritDoc
*/
public function getUndoEventID(): ?int
{
return $this->extra['u'] ?? null;
}
/**
* @inheritDoc
*/
public function setUndoneEvent(AbstractLogEntry $event, string $mode = 'undo'): LogWithEventUndoInterface
{
$this->extra['u'] = $event->getID();
if ($mode === 'undo') {
$this->extra['um'] = 1;
} elseif ($mode === 'revert') {
$this->extra['um'] = 2;
} else {
throw new \InvalidArgumentException('Passed invalid $mode!');
}
return $this;
}
/**
* @inheritDoc
*/
public function getUndoMode(): string
{
$mode_int = $this->extra['um'] ?? 1;
if ($mode_int === 1) {
return 'undo';
} else {
return 'revert';
}
}
}