2020-01-24 22:57:04 +01:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 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.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2020-02-01 16:17:20 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-01-24 22:57:04 +01:00
|
|
|
namespace App\Controller;
|
|
|
|
|
2023-04-10 00:30:23 +02:00
|
|
|
use App\DataTables\Column\LogEntryTargetColumn;
|
2022-09-11 18:45:31 +02:00
|
|
|
use App\DataTables\Filters\LogFilter;
|
2020-01-24 22:57:04 +01:00
|
|
|
use App\DataTables\LogDataTable;
|
2020-03-01 19:46:48 +01:00
|
|
|
use App\Entity\Base\AbstractDBElement;
|
|
|
|
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;
|
2022-09-11 18:45:31 +02:00
|
|
|
use App\Form\Filters\LogFilterType;
|
2022-09-18 23:44:44 +02:00
|
|
|
use App\Repository\DBElementRepository;
|
2020-03-01 19:46:48 +01:00
|
|
|
use App\Services\LogSystem\EventUndoHelper;
|
2023-04-10 00:30:23 +02:00
|
|
|
use App\Services\LogSystem\LogEntryExtraFormatter;
|
|
|
|
use App\Services\LogSystem\LogLevelHelper;
|
|
|
|
use App\Services\LogSystem\LogTargetHelper;
|
2020-03-01 19:46:48 +01:00
|
|
|
use App\Services\LogSystem\TimeTravel;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2022-09-18 22:59:31 +02:00
|
|
|
use Doctrine\ORM\EntityRepository;
|
2022-08-14 19:32:53 +02:00
|
|
|
use InvalidArgumentException;
|
2020-01-24 22:57:04 +01:00
|
|
|
use Omines\DataTablesBundle\DataTableFactory;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
2022-08-14 19:32:53 +02:00
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
2020-01-24 22:57:04 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
|
|
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Route(path: '/log')]
|
2020-01-24 22:57:04 +01:00
|
|
|
class LogController extends AbstractController
|
|
|
|
{
|
2022-09-18 23:44:44 +02:00
|
|
|
protected DBElementRepository $dbRepository;
|
2020-03-01 19:46:48 +01:00
|
|
|
|
2023-06-11 14:15:46 +02:00
|
|
|
public function __construct(protected EntityManagerInterface $entityManager, protected TimeTravel $timeTravel)
|
2020-03-01 19:46:48 +01:00
|
|
|
{
|
|
|
|
$this->dbRepository = $entityManager->getRepository(AbstractDBElement::class);
|
|
|
|
}
|
|
|
|
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Route(path: '/', name: 'log_view')]
|
2023-04-15 21:49:19 +02:00
|
|
|
public function showLogs(Request $request, DataTableFactory $dataTable): Response
|
2020-01-24 22:57:04 +01:00
|
|
|
{
|
2020-01-25 23:17:06 +01:00
|
|
|
$this->denyAccessUnlessGranted('@system.show_logs');
|
|
|
|
|
2022-09-11 18:45:31 +02:00
|
|
|
$formRequest = clone $request;
|
|
|
|
$formRequest->setMethod('GET');
|
|
|
|
$filter = new LogFilter();
|
|
|
|
|
|
|
|
$filterForm = $this->createForm(LogFilterType::class, $filter, ['method' => 'GET']);
|
|
|
|
|
|
|
|
$filterForm->handleRequest($formRequest);
|
|
|
|
|
|
|
|
$table = $dataTable->createFromType(LogDataTable::class, [
|
|
|
|
'filter' => $filter,
|
|
|
|
])
|
2020-01-24 22:57:04 +01:00
|
|
|
->handleRequest($request);
|
|
|
|
|
|
|
|
if ($table->isCallback()) {
|
|
|
|
return $table->getResponse();
|
|
|
|
}
|
|
|
|
|
2023-02-04 23:09:36 +01:00
|
|
|
return $this->render('log_system/log_list.html.twig', [
|
2020-02-01 16:17:20 +01:00
|
|
|
'datatable' => $table,
|
2023-05-28 01:21:05 +02:00
|
|
|
'filterForm' => $filterForm,
|
2020-01-24 22:57:04 +01:00
|
|
|
]);
|
|
|
|
}
|
2020-03-01 19:46:48 +01:00
|
|
|
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Route(path: '/{id}/details', name: 'log_details')]
|
|
|
|
public function logDetails(AbstractLogEntry $logEntry, LogEntryExtraFormatter $logEntryExtraFormatter,
|
2023-05-15 00:16:49 +02:00
|
|
|
LogLevelHelper $logLevelHelper, LogTargetHelper $logTargetHelper, EntityManagerInterface $entityManager): Response
|
2023-04-10 00:30:23 +02:00
|
|
|
{
|
2023-05-16 00:05:54 +02:00
|
|
|
$this->denyAccessUnlessGranted('show_details', $logEntry);
|
2023-04-10 00:30:23 +02:00
|
|
|
|
|
|
|
$extra_html = $logEntryExtraFormatter->format($logEntry);
|
|
|
|
$target_html = $logTargetHelper->formatTarget($logEntry);
|
|
|
|
|
2023-05-15 00:16:49 +02:00
|
|
|
$repo = $entityManager->getRepository(AbstractLogEntry::class);
|
|
|
|
$target_element = $repo->getTargetElement($logEntry);
|
|
|
|
|
2023-04-10 00:30:23 +02:00
|
|
|
return $this->render('log_system/details/log_details.html.twig', [
|
|
|
|
'log_entry' => $logEntry,
|
2023-05-15 00:16:49 +02:00
|
|
|
'target_element' => $target_element,
|
2023-04-10 00:30:23 +02:00
|
|
|
'extra_html' => $extra_html,
|
|
|
|
'target_html' => $target_html,
|
|
|
|
'log_level_helper' => $logLevelHelper,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Route(path: '/{id}/delete', name: 'log_delete', methods: ['DELETE'])]
|
2023-05-15 23:02:30 +02:00
|
|
|
public function deleteLogEntry(Request $request, AbstractLogEntry $logEntry, EntityManagerInterface $entityManager): RedirectResponse
|
|
|
|
{
|
|
|
|
$this->denyAccessUnlessGranted('delete', $logEntry);
|
|
|
|
|
2023-06-18 00:00:58 +02:00
|
|
|
if ($this->isCsrfTokenValid('delete'.$logEntry->getID(), $request->request->get('_token'))) {
|
2023-05-15 23:02:30 +02:00
|
|
|
//Remove part
|
|
|
|
$entityManager->remove($logEntry);
|
|
|
|
//Flush changes
|
|
|
|
$entityManager->flush();
|
|
|
|
$this->addFlash('success', 'log.delete.success');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->redirectToRoute('homepage');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Route(path: '/undo', name: 'log_undo', methods: ['POST'])]
|
2022-08-14 19:32:53 +02:00
|
|
|
public function undoRevertLog(Request $request, EventUndoHelper $eventUndoHelper): RedirectResponse
|
2020-03-01 19:46:48 +01:00
|
|
|
{
|
2020-03-01 20:30:23 +01:00
|
|
|
$mode = EventUndoHelper::MODE_UNDO;
|
2020-03-01 19:46:48 +01:00
|
|
|
$id = $request->request->get('undo');
|
2020-03-01 20:30:23 +01:00
|
|
|
|
|
|
|
//If no undo value was set check if a revert was set
|
2020-03-15 13:56:31 +01:00
|
|
|
if (null === $id) {
|
2020-03-01 20:30:23 +01:00
|
|
|
$id = $request->get('revert');
|
|
|
|
$mode = EventUndoHelper::MODE_REVERT;
|
|
|
|
}
|
|
|
|
|
2020-03-01 19:46:48 +01:00
|
|
|
$log_element = $this->entityManager->find(AbstractLogEntry::class, $id);
|
2023-06-11 14:55:06 +02:00
|
|
|
if (!$log_element instanceof AbstractLogEntry) {
|
2022-08-14 19:32:53 +02:00
|
|
|
throw new InvalidArgumentException('No log entry with the given ID is existing!');
|
2020-03-01 20:30:23 +01:00
|
|
|
}
|
2020-03-01 19:46:48 +01:00
|
|
|
|
2020-03-07 20:49:52 +01:00
|
|
|
$this->denyAccessUnlessGranted('revert_element', $log_element->getTargetClass());
|
|
|
|
|
2020-03-01 20:30:23 +01:00
|
|
|
$eventUndoHelper->setMode($mode);
|
2020-03-01 19:46:48 +01:00
|
|
|
$eventUndoHelper->setUndoneEvent($log_element);
|
|
|
|
|
2020-03-15 13:56:31 +01:00
|
|
|
if (EventUndoHelper::MODE_UNDO === $mode) {
|
2020-03-01 20:30:23 +01:00
|
|
|
$this->undoLog($log_element);
|
2020-03-15 13:56:31 +01:00
|
|
|
} elseif (EventUndoHelper::MODE_REVERT === $mode) {
|
2020-03-01 20:30:23 +01:00
|
|
|
$this->revertLog($log_element);
|
|
|
|
}
|
|
|
|
|
|
|
|
$eventUndoHelper->clearUndoneEvent();
|
|
|
|
|
|
|
|
$redirect = $request->request->get('redirect_back');
|
2020-03-15 13:56:31 +01:00
|
|
|
|
2020-03-01 20:30:23 +01:00
|
|
|
return $this->redirect($redirect);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function revertLog(AbstractLogEntry $logEntry): void
|
|
|
|
{
|
|
|
|
$timestamp = $logEntry->getTimestamp();
|
|
|
|
$element = $this->entityManager->find($logEntry->getTargetClass(), $logEntry->getTargetID());
|
|
|
|
//If the element is not available in DB try to undelete it
|
2020-03-15 13:56:31 +01:00
|
|
|
if (null === $element) {
|
2020-03-01 20:30:23 +01:00
|
|
|
$element = $this->timeTravel->undeleteEntity($logEntry->getTargetClass(), $logEntry->getTargetID());
|
|
|
|
$this->entityManager->persist($element);
|
|
|
|
$this->entityManager->flush();
|
|
|
|
$this->dbRepository->changeID($element, $logEntry->getTargetID());
|
|
|
|
}
|
|
|
|
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!$element instanceof AbstractDBElement) {
|
2020-03-01 20:30:23 +01:00
|
|
|
$this->addFlash('error', 'log.undo.target_not_found');
|
2020-03-15 13:56:31 +01:00
|
|
|
|
2020-03-01 20:30:23 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->timeTravel->revertEntityToTimestamp($element, $timestamp);
|
|
|
|
$this->entityManager->flush();
|
|
|
|
$this->addFlash('success', 'log.undo.revert_success');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function undoLog(AbstractLogEntry $log_element): void
|
|
|
|
{
|
2020-03-01 19:46:48 +01:00
|
|
|
if ($log_element instanceof ElementDeletedLogEntry || $log_element instanceof CollectionElementDeleted) {
|
|
|
|
if ($log_element instanceof ElementDeletedLogEntry) {
|
|
|
|
$element_class = $log_element->getTargetClass();
|
|
|
|
$element_id = $log_element->getTargetID();
|
|
|
|
} else {
|
|
|
|
$element_class = $log_element->getDeletedElementClass();
|
|
|
|
$element_id = $log_element->getDeletedElementID();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Check if the element we want to undelete already exits
|
2020-03-15 13:56:31 +01:00
|
|
|
if (null === $this->entityManager->find($element_class, $element_id)) {
|
2020-03-01 19:46:48 +01:00
|
|
|
$undeleted_element = $this->timeTravel->undeleteEntity($element_class, $element_id);
|
|
|
|
$this->entityManager->persist($undeleted_element);
|
|
|
|
$this->entityManager->flush();
|
|
|
|
$this->dbRepository->changeID($undeleted_element, $element_id);
|
|
|
|
$this->addFlash('success', 'log.undo.element_undelete_success');
|
|
|
|
} else {
|
|
|
|
$this->addFlash('warning', 'log.undo.element_element_already_undeleted');
|
|
|
|
}
|
|
|
|
} elseif ($log_element instanceof ElementCreatedLogEntry) {
|
|
|
|
$element = $this->entityManager->find($log_element->getTargetClass(), $log_element->getTargetID());
|
2020-03-15 13:56:31 +01:00
|
|
|
if (null !== $element) {
|
2020-03-01 19:46:48 +01:00
|
|
|
$this->entityManager->remove($element);
|
|
|
|
$this->entityManager->flush();
|
|
|
|
$this->addFlash('success', 'log.undo.element_delete_success');
|
|
|
|
} else {
|
|
|
|
$this->addFlash('warning', 'log.undo.element.element_already_delted');
|
|
|
|
}
|
|
|
|
} elseif ($log_element instanceof ElementEditedLogEntry) {
|
|
|
|
$element = $this->entityManager->find($log_element->getTargetClass(), $log_element->getTargetID());
|
|
|
|
if ($element instanceof AbstractDBElement) {
|
|
|
|
$this->timeTravel->applyEntry($element, $log_element);
|
|
|
|
$this->entityManager->flush();
|
|
|
|
$this->addFlash('success', 'log.undo.element_change_undone');
|
|
|
|
} else {
|
|
|
|
$this->addFlash('error', 'log.undo.do_undelete_before');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->addFlash('error', 'log.undo.log_type_invalid');
|
|
|
|
}
|
|
|
|
}
|
2020-02-01 16:17:20 +01:00
|
|
|
}
|