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).
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2020-02-01 16:17:20 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-01-24 22:57:04 +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)
|
|
|
|
*
|
|
|
|
* 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\DataTables;
|
|
|
|
|
2020-02-22 20:04:43 +01:00
|
|
|
use App\DataTables\Column\IconLinkColumn;
|
2020-01-24 22:57:04 +01:00
|
|
|
use App\DataTables\Column\LocaleDateTimeColumn;
|
2020-01-25 22:52:34 +01:00
|
|
|
use App\DataTables\Column\LogEntryExtraColumn;
|
2020-01-24 22:57:04 +01:00
|
|
|
use App\DataTables\Column\LogEntryTargetColumn;
|
2020-03-01 19:46:48 +01:00
|
|
|
use App\DataTables\Column\RevertLogColumn;
|
2020-02-22 20:04:43 +01:00
|
|
|
use App\Entity\Base\AbstractDBElement;
|
|
|
|
use App\Entity\Contracts\TimeTravelInterface;
|
2020-01-24 22:57:04 +01:00
|
|
|
use App\Entity\LogSystem\AbstractLogEntry;
|
2020-02-29 22:53:53 +01:00
|
|
|
use App\Entity\LogSystem\CollectionElementDeleted;
|
2020-03-07 17:15:16 +01:00
|
|
|
use App\Entity\LogSystem\ElementCreatedLogEntry;
|
|
|
|
use App\Entity\LogSystem\ElementDeletedLogEntry;
|
|
|
|
use App\Entity\LogSystem\ElementEditedLogEntry;
|
2020-03-07 22:30:28 +01:00
|
|
|
use App\Entity\UserSystem\Group;
|
|
|
|
use App\Entity\UserSystem\User;
|
2020-02-22 20:04:43 +01:00
|
|
|
use App\Exceptions\EntityNotSupportedException;
|
2020-01-24 22:57:04 +01:00
|
|
|
use App\Services\ElementTypeNameGenerator;
|
2020-02-22 20:04:43 +01:00
|
|
|
use App\Services\EntityURLGenerator;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2020-01-24 22:57:04 +01:00
|
|
|
use Doctrine\ORM\QueryBuilder;
|
|
|
|
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapter;
|
|
|
|
use Omines\DataTablesBundle\Column\TextColumn;
|
|
|
|
use Omines\DataTablesBundle\DataTable;
|
|
|
|
use Omines\DataTablesBundle\DataTableTypeInterface;
|
2020-01-25 20:55:30 +01:00
|
|
|
use Psr\Log\LogLevel;
|
2020-04-04 15:45:14 +02:00
|
|
|
use Symfony\Component\OptionsResolver\Options;
|
2020-02-22 20:04:43 +01:00
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
2020-01-25 20:28:00 +01:00
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
2020-03-07 20:49:52 +01:00
|
|
|
use Symfony\Component\Security\Core\Security;
|
2020-01-24 22:57:04 +01:00
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
|
|
|
|
|
|
|
class LogDataTable implements DataTableTypeInterface
|
|
|
|
{
|
|
|
|
protected $elementTypeNameGenerator;
|
|
|
|
protected $translator;
|
2020-01-25 20:28:00 +01:00
|
|
|
protected $urlGenerator;
|
2020-02-22 20:04:43 +01:00
|
|
|
protected $entityURLGenerator;
|
|
|
|
protected $logRepo;
|
2020-03-07 20:49:52 +01:00
|
|
|
protected $security;
|
2020-01-24 22:57:04 +01:00
|
|
|
|
2020-01-25 20:28:00 +01:00
|
|
|
public function __construct(ElementTypeNameGenerator $elementTypeNameGenerator, TranslatorInterface $translator,
|
2020-03-07 20:49:52 +01:00
|
|
|
UrlGeneratorInterface $urlGenerator, EntityURLGenerator $entityURLGenerator, EntityManagerInterface $entityManager, Security $security)
|
2020-01-24 22:57:04 +01:00
|
|
|
{
|
|
|
|
$this->elementTypeNameGenerator = $elementTypeNameGenerator;
|
|
|
|
$this->translator = $translator;
|
2020-01-25 20:28:00 +01:00
|
|
|
$this->urlGenerator = $urlGenerator;
|
2020-02-22 20:04:43 +01:00
|
|
|
$this->entityURLGenerator = $entityURLGenerator;
|
|
|
|
$this->logRepo = $entityManager->getRepository(AbstractLogEntry::class);
|
2020-03-07 20:49:52 +01:00
|
|
|
$this->security = $security;
|
2020-02-22 20:04:43 +01:00
|
|
|
}
|
|
|
|
|
2020-03-15 13:56:31 +01:00
|
|
|
public function configureOptions(OptionsResolver $optionsResolver): void
|
2020-02-22 20:04:43 +01:00
|
|
|
{
|
|
|
|
$optionsResolver->setDefaults([
|
2020-03-15 13:56:31 +01:00
|
|
|
'mode' => 'system_log',
|
|
|
|
'filter_elements' => [],
|
|
|
|
]);
|
2020-02-22 20:04:43 +01:00
|
|
|
|
2020-04-04 15:45:14 +02:00
|
|
|
$optionsResolver->setAllowedTypes('filter_elements', ['array', 'object']);
|
|
|
|
$optionsResolver->setAllowedTypes('mode', 'string');
|
|
|
|
|
|
|
|
$optionsResolver->setNormalizer('filter_elements', function (Options $options, $value) {
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!is_array($value)) {
|
2020-04-04 15:45:14 +02:00
|
|
|
return [$value];
|
|
|
|
}
|
2020-04-10 13:05:08 +02:00
|
|
|
|
2020-04-04 15:45:14 +02:00
|
|
|
return $value;
|
|
|
|
});
|
|
|
|
|
2020-03-07 17:15:16 +01:00
|
|
|
$optionsResolver->setAllowedValues('mode', ['system_log', 'element_history', 'last_activity']);
|
2020-01-24 22:57:04 +01:00
|
|
|
}
|
|
|
|
|
2020-02-01 16:17:20 +01:00
|
|
|
public function configure(DataTable $dataTable, array $options): void
|
2020-01-24 22:57:04 +01:00
|
|
|
{
|
2020-02-22 20:04:43 +01:00
|
|
|
$resolver = new OptionsResolver();
|
|
|
|
$this->configureOptions($resolver);
|
|
|
|
$options = $resolver->resolve($options);
|
|
|
|
|
2020-01-25 20:55:30 +01:00
|
|
|
$dataTable->add('symbol', TextColumn::class, [
|
|
|
|
'label' => '',
|
|
|
|
'render' => function ($value, AbstractLogEntry $context) {
|
|
|
|
switch ($context->getLevelString()) {
|
|
|
|
case LogLevel::DEBUG:
|
|
|
|
$symbol = 'fa-bug';
|
2020-02-01 16:17:20 +01:00
|
|
|
|
2020-01-25 20:55:30 +01:00
|
|
|
break;
|
|
|
|
case LogLevel::INFO:
|
|
|
|
$symbol = 'fa-info';
|
2020-02-01 16:17:20 +01:00
|
|
|
|
2020-01-25 20:55:30 +01:00
|
|
|
break;
|
|
|
|
case LogLevel::NOTICE:
|
|
|
|
$symbol = 'fa-flag';
|
2020-02-01 16:17:20 +01:00
|
|
|
|
2020-01-25 20:55:30 +01:00
|
|
|
break;
|
|
|
|
case LogLevel::WARNING:
|
|
|
|
$symbol = 'fa-exclamation-circle';
|
2020-02-01 16:17:20 +01:00
|
|
|
|
2020-01-25 20:55:30 +01:00
|
|
|
break;
|
|
|
|
case LogLevel::ERROR:
|
|
|
|
$symbol = 'fa-exclamation-triangle';
|
2020-02-01 16:17:20 +01:00
|
|
|
|
2020-01-25 20:55:30 +01:00
|
|
|
break;
|
|
|
|
case LogLevel::CRITICAL:
|
|
|
|
$symbol = 'fa-bolt';
|
2020-02-01 16:17:20 +01:00
|
|
|
|
2020-01-25 20:55:30 +01:00
|
|
|
break;
|
|
|
|
case LogLevel::ALERT:
|
|
|
|
$symbol = 'fa-radiation';
|
2020-02-01 16:17:20 +01:00
|
|
|
|
2020-01-25 20:55:30 +01:00
|
|
|
break;
|
|
|
|
case LogLevel::EMERGENCY:
|
|
|
|
$symbol = 'fa-skull-crossbones';
|
2020-02-01 16:17:20 +01:00
|
|
|
|
2020-01-25 20:55:30 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$symbol = 'fa-question-circle';
|
2020-02-01 16:17:20 +01:00
|
|
|
|
2020-01-25 20:55:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-03-07 17:20:01 +01:00
|
|
|
return sprintf(
|
|
|
|
'<i class="fas fa-fw %s" title="%s"></i>',
|
|
|
|
$symbol,
|
|
|
|
$context->getLevelString()
|
|
|
|
);
|
2020-02-01 16:17:20 +01:00
|
|
|
},
|
2020-01-25 20:55:30 +01:00
|
|
|
]);
|
|
|
|
|
2020-01-24 22:57:04 +01:00
|
|
|
$dataTable->add('id', TextColumn::class, [
|
|
|
|
'label' => $this->translator->trans('log.id'),
|
|
|
|
'visible' => false,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$dataTable->add('timestamp', LocaleDateTimeColumn::class, [
|
|
|
|
'label' => $this->translator->trans('log.timestamp'),
|
2020-02-01 16:17:20 +01:00
|
|
|
'timeFormat' => 'medium',
|
2020-01-24 22:57:04 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$dataTable->add('type', TextColumn::class, [
|
|
|
|
'label' => $this->translator->trans('log.type'),
|
|
|
|
'propertyPath' => 'type',
|
|
|
|
'render' => function (string $value, AbstractLogEntry $context) {
|
2020-02-01 16:17:20 +01:00
|
|
|
return $this->translator->trans('log.type.'.$value);
|
|
|
|
},
|
2020-01-24 22:57:04 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$dataTable->add('level', TextColumn::class, [
|
|
|
|
'label' => $this->translator->trans('log.level'),
|
2020-03-15 13:56:31 +01:00
|
|
|
'visible' => 'system_log' === $options['mode'],
|
2020-01-24 22:57:04 +01:00
|
|
|
'propertyPath' => 'levelString',
|
|
|
|
'render' => function (string $value, AbstractLogEntry $context) {
|
2020-01-25 20:20:46 +01:00
|
|
|
return $value;
|
2020-02-01 16:17:20 +01:00
|
|
|
},
|
2020-01-24 22:57:04 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$dataTable->add('user', TextColumn::class, [
|
|
|
|
'label' => $this->translator->trans('log.user'),
|
2020-01-25 20:28:00 +01:00
|
|
|
'render' => function ($value, AbstractLogEntry $context) {
|
|
|
|
$user = $context->getUser();
|
2020-02-01 16:17:20 +01:00
|
|
|
|
2020-01-25 20:28:00 +01:00
|
|
|
return sprintf(
|
|
|
|
'<a href="%s">%s</a>',
|
|
|
|
$this->urlGenerator->generate('user_info', ['id' => $user->getID()]),
|
|
|
|
$user->getFullName(true)
|
|
|
|
);
|
2020-02-01 16:17:20 +01:00
|
|
|
},
|
2020-01-24 22:57:04 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$dataTable->add('target_type', TextColumn::class, [
|
|
|
|
'label' => $this->translator->trans('log.target_type'),
|
|
|
|
'visible' => false,
|
|
|
|
'render' => function ($value, AbstractLogEntry $context) {
|
|
|
|
$class = $context->getTargetClass();
|
2020-02-01 16:17:20 +01:00
|
|
|
if (null !== $class) {
|
2020-01-24 22:57:04 +01:00
|
|
|
return $this->elementTypeNameGenerator->getLocalizedTypeLabel($class);
|
|
|
|
}
|
2020-02-01 16:17:20 +01:00
|
|
|
|
2020-01-24 22:57:04 +01:00
|
|
|
return '';
|
2020-02-01 16:17:20 +01:00
|
|
|
},
|
2020-01-24 22:57:04 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$dataTable->add('target', LogEntryTargetColumn::class, [
|
2020-02-01 16:17:20 +01:00
|
|
|
'label' => $this->translator->trans('log.target'),
|
2020-03-29 22:16:06 +02:00
|
|
|
'show_associated' => 'element_history' !== $options['mode'],
|
2020-01-24 22:57:04 +01:00
|
|
|
]);
|
|
|
|
|
2020-01-25 22:52:34 +01:00
|
|
|
$dataTable->add('extra', LogEntryExtraColumn::class, [
|
2020-02-01 16:17:20 +01:00
|
|
|
'label' => $this->translator->trans('log.extra'),
|
2020-01-25 22:52:34 +01:00
|
|
|
]);
|
|
|
|
|
2020-03-15 13:56:31 +01:00
|
|
|
$dataTable->add('timeTravel', IconLinkColumn::class, [
|
2020-02-22 20:04:43 +01:00
|
|
|
'label' => '',
|
|
|
|
'icon' => 'fas fa-fw fa-eye',
|
|
|
|
'href' => function ($value, AbstractLogEntry $context) {
|
|
|
|
if (
|
2020-03-07 22:30:28 +01:00
|
|
|
($context instanceof TimeTravelInterface
|
|
|
|
&& $context->hasOldDataInformations())
|
|
|
|
|| $context instanceof CollectionElementDeleted
|
2020-02-22 20:04:43 +01:00
|
|
|
) {
|
|
|
|
try {
|
|
|
|
$target = $this->logRepo->getTargetElement($context);
|
2020-03-15 13:56:31 +01:00
|
|
|
if (null !== $target) {
|
|
|
|
return $this->entityURLGenerator->timeTravelURL($target, $context->getTimestamp());
|
2020-03-01 19:46:48 +01:00
|
|
|
}
|
2020-02-22 20:04:43 +01:00
|
|
|
} catch (EntityNotSupportedException $exception) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2020-03-15 13:56:31 +01:00
|
|
|
|
2020-02-22 20:04:43 +01:00
|
|
|
return null;
|
2020-03-07 20:49:52 +01:00
|
|
|
},
|
|
|
|
'disabled' => function ($value, AbstractLogEntry $context) {
|
|
|
|
return
|
2020-08-21 21:36:22 +02:00
|
|
|
!$this->security->isGranted('@tools.timetravel')
|
|
|
|
|| !$this->security->isGranted('show_history', $context->getTargetClass());
|
2020-03-15 13:56:31 +01:00
|
|
|
},
|
2020-02-22 20:04:43 +01:00
|
|
|
]);
|
|
|
|
|
2020-03-01 19:46:48 +01:00
|
|
|
$dataTable->add('actionRevert', RevertLogColumn::class, [
|
2020-03-15 13:56:31 +01:00
|
|
|
'label' => '',
|
2020-03-01 19:46:48 +01:00
|
|
|
]);
|
|
|
|
|
2020-01-24 22:57:04 +01:00
|
|
|
$dataTable->addOrderBy('timestamp', DataTable::SORT_DESCENDING);
|
|
|
|
|
|
|
|
$dataTable->createAdapter(ORMAdapter::class, [
|
|
|
|
'entity' => AbstractLogEntry::class,
|
2020-02-22 20:04:43 +01:00
|
|
|
'query' => function (QueryBuilder $builder) use ($options): void {
|
|
|
|
$this->getQuery($builder, $options);
|
2020-01-24 22:57:04 +01:00
|
|
|
},
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-02-22 20:04:43 +01:00
|
|
|
protected function getQuery(QueryBuilder $builder, array $options): void
|
2020-01-24 22:57:04 +01:00
|
|
|
{
|
|
|
|
$builder->distinct()->select('log')
|
|
|
|
->addSelect('user')
|
|
|
|
->from(AbstractLogEntry::class, 'log')
|
|
|
|
->leftJoin('log.user', 'user');
|
2020-02-22 20:04:43 +01:00
|
|
|
|
2020-03-15 13:56:31 +01:00
|
|
|
if ('last_activity' === $options['mode']) {
|
|
|
|
$builder->where('log INSTANCE OF '.ElementCreatedLogEntry::class)
|
|
|
|
->orWhere('log INSTANCE OF '.ElementDeletedLogEntry::class)
|
|
|
|
->orWhere('log INSTANCE OF '.ElementEditedLogEntry::class)
|
|
|
|
->orWhere('log INSTANCE OF '.CollectionElementDeleted::class)
|
|
|
|
->andWhere('log.target_type NOT IN (:disallowed)');
|
2020-03-07 22:30:28 +01:00
|
|
|
|
|
|
|
$builder->setParameter('disallowed', [
|
|
|
|
AbstractLogEntry::targetTypeClassToID(User::class),
|
|
|
|
AbstractLogEntry::targetTypeClassToID(Group::class),
|
|
|
|
]);
|
2020-03-07 17:15:16 +01:00
|
|
|
}
|
|
|
|
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!empty($options['filter_elements'])) {
|
2020-02-22 20:04:43 +01:00
|
|
|
foreach ($options['filter_elements'] as $element) {
|
|
|
|
/** @var AbstractDBElement $element */
|
|
|
|
|
|
|
|
$target_type = AbstractLogEntry::targetTypeClassToID(get_class($element));
|
|
|
|
$target_id = $element->getID();
|
2020-03-15 13:56:31 +01:00
|
|
|
$builder->orWhere("log.target_type = ${target_type} AND log.target_id = ${target_id}");
|
2020-02-22 20:04:43 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-24 22:57:04 +01:00
|
|
|
}
|
2020-02-01 16:17:20 +01:00
|
|
|
}
|