Added an basic table to show log entries.

This commit is contained in:
Jan Böhmer 2020-01-24 22:57:04 +01:00
parent 3f8cd6473a
commit d0b3750594
21 changed files with 1292 additions and 7 deletions

View file

@ -0,0 +1,58 @@
<?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\Controller;
use App\DataTables\LogDataTable;
use App\DataTables\PartsDataTable;
use App\Entity\Parts\Category;
use Omines\DataTablesBundle\DataTableFactory;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/log")
*/
class LogController extends AbstractController
{
/**
* @Route("/", name="log_view")
*
* @return JsonResponse|Response
*/
public function showCategory(Request $request, DataTableFactory $dataTable)
{
$table = $dataTable->createFromType(LogDataTable::class)
->handleRequest($request);
if ($table->isCallback()) {
return $table->getResponse();
}
return $this->render('LogSystem/log_list.html.twig', [
'datatable' => $table
]);
}
}

View file

@ -0,0 +1,104 @@
<?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\DataTables\Column;
use App\Entity\Base\DBElement;
use App\Entity\Base\NamedDBElement;
use App\Entity\LogSystem\AbstractLogEntry;
use App\Repository\LogEntryRepository;
use App\Services\ElementTypeNameGenerator;
use App\Services\EntityURLGenerator;
use Doctrine\ORM\EntityManagerInterface;
use Omines\DataTablesBundle\Column\AbstractColumn;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatorInterface;
class LogEntryTargetColumn extends AbstractColumn
{
protected $em;
protected $entryRepository;
protected $entityURLGenerator;
protected $elementTypeNameGenerator;
protected $translator;
public function __construct(EntityManagerInterface $entityManager, EntityURLGenerator $entityURLGenerator,
ElementTypeNameGenerator $elementTypeNameGenerator, TranslatorInterface $translator)
{
$this->em = $entityManager;
$this->entryRepository = $entityManager->getRepository(AbstractLogEntry::class);
$this->entityURLGenerator = $entityURLGenerator;
$this->elementTypeNameGenerator = $elementTypeNameGenerator;
$this->translator = $translator;
}
/**
* @inheritDoc
*/
public function normalize($value)
{
return $value;
}
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
}
public function render($value, $context)
{
/** @var AbstractLogEntry $context */
$target = $this->entryRepository->getTargetElement($context);
//The element is existing
if ($target instanceof NamedDBElement) {
return sprintf(
'<a href="%s">%s</a>',
$this->entityURLGenerator->infoURL($target),
$this->elementTypeNameGenerator->getTypeNameCombination($target, true)
);
}
//Target does not have a name
if ($target instanceof DBElement) {
return sprintf(
'<i>%s</i>: %s',
$this->elementTypeNameGenerator->getLocalizedTypeLabel($target),
$target->getID()
);
}
//Element was deleted
if ($target === null && $context->hasTarget()) {
return sprintf(
'<i>%s</i>: %s [%s]',
$this->elementTypeNameGenerator->getLocalizedTypeLabel($context->getTargetClass()),
$context->getTargetID(),
$this->translator->trans('log.target_deleted')
);
}
//Log is not associated with an element
return "";
}
}

View file

@ -0,0 +1,120 @@
<?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\DataTables;
use App\DataTables\Column\EntityColumn;
use App\DataTables\Column\LocaleDateTimeColumn;
use App\DataTables\Column\LogEntryTargetColumn;
use App\Entity\Attachments\Attachment;
use App\Entity\LogSystem\AbstractLogEntry;
use App\Services\ElementTypeNameGenerator;
use Doctrine\ORM\QueryBuilder;
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapter;
use Omines\DataTablesBundle\Column\TextColumn;
use Omines\DataTablesBundle\DataTable;
use Omines\DataTablesBundle\DataTableTypeInterface;
use SebastianBergmann\CodeCoverage\Report\Text;
use Symfony\Contracts\Translation\TranslatorInterface;
class LogDataTable implements DataTableTypeInterface
{
protected $elementTypeNameGenerator;
protected $translator;
public function __construct(ElementTypeNameGenerator $elementTypeNameGenerator, TranslatorInterface $translator)
{
$this->elementTypeNameGenerator = $elementTypeNameGenerator;
$this->translator = $translator;
}
public function configure(DataTable $dataTable, array $options)
{
$dataTable->add('id', TextColumn::class, [
'label' => $this->translator->trans('log.id'),
'visible' => false,
]);
$dataTable->add('timestamp', LocaleDateTimeColumn::class, [
'label' => $this->translator->trans('log.timestamp'),
'timeFormat' => 'medium'
]);
$dataTable->add('type', TextColumn::class, [
'label' => $this->translator->trans('log.type'),
'propertyPath' => 'type',
'render' => function (string $value, AbstractLogEntry $context) {
return $this->translator->trans('log.type.' . $value);
}
]);
$dataTable->add('level', TextColumn::class, [
'label' => $this->translator->trans('log.level'),
'propertyPath' => 'levelString',
'render' => function (string $value, AbstractLogEntry $context) {
return $this->translator->trans('log.level.' . $value);
}
]);
$dataTable->add('user', TextColumn::class, [
'label' => $this->translator->trans('log.user'),
'propertyPath' => 'user.name',
]);
$dataTable->add('target_type', TextColumn::class, [
'label' => $this->translator->trans('log.target_type'),
'visible' => false,
'render' => function ($value, AbstractLogEntry $context) {
$class = $context->getTargetClass();
if ($class !== null) {
return $this->elementTypeNameGenerator->getLocalizedTypeLabel($class);
}
return '';
}
]);
$dataTable->add('target', LogEntryTargetColumn::class, [
'label' => $this->translator->trans('log.target')
]);
$dataTable->addOrderBy('timestamp', DataTable::SORT_DESCENDING);
$dataTable->createAdapter(ORMAdapter::class, [
'entity' => AbstractLogEntry::class,
'query' => function (QueryBuilder $builder): void {
$this->getQuery($builder);
},
]);
}
protected function getQuery(QueryBuilder $builder): void
{
$builder->distinct()->select('log')
->addSelect('user')
->from(AbstractLogEntry::class, 'log')
->leftJoin('log.user', 'user');
}
}

View file

@ -80,7 +80,7 @@ abstract class DBElement
* *
* @return int|null the ID of this element * @return int|null the ID of this element
*/ */
final public function getID(): ?int public function getID(): ?int
{ {
return $this->id; return $this->id;
} }

View file

@ -0,0 +1,375 @@
<?php
declare(strict_types=1);
/**
* 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;
use App\Entity\Attachments\Attachment;
use App\Entity\Base\DBElement;
use App\Entity\Devices\Device;
use App\Entity\Parts\Category;
use App\Entity\Parts\Footprint;
use App\Entity\Parts\Manufacturer;
use App\Entity\Parts\Part;
use App\Entity\Parts\Storelocation;
use App\Entity\Parts\Supplier;
use App\Entity\UserSystem\Group;
use App\Entity\UserSystem\User;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\Attachments\AttachmentType;
use App\Entity\Devices\DevicePart;
use Psr\Log\LogLevel;
/**
* This entity describes a entry in the event log.
* @package App\Entity\LogSystem
* @ORM\Entity(repositoryClass="App\Repository\LogEntryRepository")
* @ORM\Table("log")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="type", type="smallint")
* @ORM\DiscriminatorMap({
* 1 = "UserLoginLogEntry",
* 2 = "UserLogoutLogEntry",
* 3 = "UserNotAllowedLogEntry",
* 4 = "ExceptionLogEntry",
* 5 = "ElementDeletedLogEntry",
* 6 = "ElementCreatedLogEntry",
* 7 = "ElementEditedLogEntry",
* 8 = "ConfigChangedLogEntry",
* 9 = "DatabaseUpdatedLogEntry"
* })
*/
abstract class AbstractLogEntry extends DBElement
{
public const LEVEL_EMERGENCY = 0;
public const LEVEL_ALERT = 1;
public const LEVEL_CRITICAL = 2;
public const LEVEL_ERROR = 3;
public const LEVEL_WARNING = 4;
public const LEVEL_NOTICE = 5;
public const LEVEL_INFO = 6;
public const LEVEL_DEBUG = 7;
protected const TARGET_TYPE_NONE = 0;
protected const TARGET_TYPE_USER = 1;
protected const TARGET_TYPE_ATTACHEMENT = 2;
protected const TARGET_TYPE_ATTACHEMENTTYPE = 3;
protected const TARGET_TYPE_CATEGORY = 4;
protected const TARGET_TYPE_DEVICE = 5;
protected const TARGET_TYPE_DEVICEPART = 6;
protected const TARGET_TYPE_FOOTPRINT = 7;
protected const TARGET_TYPE_GROUP = 8;
protected const TARGET_TYPE_MANUFACTURER = 9;
protected const TARGET_TYPE_PART = 10;
protected const TARGET_TYPE_STORELOCATION = 11;
protected const TARGET_TYPE_SUPPLIER = 12;
/** @var array This const is used to convert the numeric level to a PSR-3 compatible log level */
protected const LEVEL_ID_TO_STRING = [
self::LEVEL_EMERGENCY => LogLevel::EMERGENCY,
self::LEVEL_ALERT => LogLevel::ALERT,
self::LEVEL_CRITICAL => LogLevel::CRITICAL,
self::LEVEL_ERROR => LogLevel::ERROR,
self::LEVEL_WARNING => LogLevel::WARNING,
self::LEVEL_NOTICE => LogLevel::NOTICE,
self::LEVEL_INFO => LogLevel::INFO,
self::LEVEL_DEBUG => LogLevel::DEBUG,
];
protected const TARGET_CLASS_MAPPING = [
self::TARGET_TYPE_USER => User::class,
self::TARGET_TYPE_ATTACHEMENT => Attachment::class,
self::TARGET_TYPE_ATTACHEMENTTYPE => AttachmentType::class,
self::TARGET_TYPE_CATEGORY => Category::class,
self::TARGET_TYPE_DEVICE => Device::class,
self::TARGET_TYPE_DEVICEPART => DevicePart::class,
self::TARGET_TYPE_FOOTPRINT => Footprint::class,
self::TARGET_TYPE_GROUP => Group::class,
self::TARGET_TYPE_MANUFACTURER => Manufacturer::class,
self::TARGET_TYPE_PART => Part::class,
self::TARGET_TYPE_STORELOCATION => Storelocation::class,
self::TARGET_TYPE_SUPPLIER => Supplier::class,
];
/** @var User $user The user which has caused this log entry
* @ORM\ManyToOne(targetEntity="App\Entity\UserSystem\User")
* @ORM\JoinColumn(name="id_user")
*/
protected $user;
/** @var DateTime The datetime the event associated with this log entry has occured.
* @ORM\Column(type="datetime", name="datetime")
*/
protected $timestamp;
/** @var integer The priority level of the associated level. 0 is highest, 7 lowest
* @ORM\Column(type="integer", name="level", columnDefinition="TINYINT")
*/
protected $level;
/** @var int $target_id The ID of the element targeted by this event
* @ORM\Column(name="target_id", type="integer", nullable=false)
*/
protected $target_id = 0;
/** @var int $target_type The Type of the targeted element
* @ORM\Column(name="target_type", type="smallint", nullable=false)
*/
protected $target_type = 0;
/** @var string The type of this log entry, aka the description what has happened.
* The mapping between the log entry class and the discriminator column is done by doctrine.
* Each subclass should override this string to specify a better string.
*/
protected $typeString = "unknown";
/**
* Get the user that caused the event associated with this log entry.
* @return User
*/
public function getUser(): User
{
return $this->user;
}
/**
* Sets the user that caused the event.
* @param User $user
* @return $this
*/
public function setUser(User $user): self
{
$this->user = $user;
return $this;
}
/**
* Returns the timestamp when the event that caused this log entry happened
* @return DateTime
*/
public function getTimestamp(): DateTime
{
return $this->timestamp;
}
/**
* Sets the timestamp when the event happened.
* @param DateTime $timestamp
* @return $this
*/
public function setTimestamp(DateTime $timestamp): AbstractLogEntry
{
$this->timestamp = $timestamp;
return $this;
}
/**
* Get the priority level of this log entry. 0 is highest and 7 lowest level.
* See LEVEL_* consts in this class for more info
* @return int
*/
public function getLevel(): int
{
//It is always alerting when a wrong int is saved in DB...
if ($this->level < 0 || $this->level > 7) {
return self::LEVEL_ALERT;
}
return $this->level;
}
/**
* Sets the new level of this log entry.
* @param int $level
* @return $this
*/
public function setLevel(int $level): AbstractLogEntry
{
if ($level < 0 || $this->level > 7) {
throw new \InvalidArgumentException(sprintf('$level must be between 0 and 7! %d given!', $level));
}
$this->level = $level;
return $this;
}
/**
* Get the priority level of this log entry as PSR3 compatible string
* @return string
*/
public function getLevelString(): string
{
return self::levelIntToString($this->getLevel());
}
/**
* Sets the priority level of this log entry as PSR3 compatible string
* @param string $level
* @return $this
*/
public function setLevelString(string $level): AbstractLogEntry
{
$this->setLevel(self::levelStringToInt($level));
return $this;
}
/**
* Returns the type of the event this log entry is associated with.
* @return string
*/
public function getType(): string
{
return $this->typeString;
}
/**
* @inheritDoc
*/
public function getIDString(): string
{
return "LOG".$this->getID();
}
/**
* Returns the class name of the target element associated with this log entry.
* Returns null, if this log entry is not associated with an log entry.
* @return string|null The class name of the target class.
*/
public function getTargetClass(): ?string
{
if ($this->target_type === self::TARGET_TYPE_NONE) {
return null;
}
return self::targetTypeIdToClass($this->target_type);
}
/**
* Returns the ID of the target element associated with this log entry.
* Returns null, if this log entry is not associated with an log entry.
* @return int|null The ID of the associated element.
*/
public function getTargetID(): ?int
{
if ($this->target_id === 0) {
return null;
}
return $this->target_id;
}
/**
* Checks if this log entry is associated with an element
* @return bool True if this log entry is associated with an element, false otherwise.
*/
public function hasTarget(): bool
{
return $this->getTargetID() !== null && $this->getTargetClass() !== null;
}
/**
* Sets the target element associated with this element
* @param DBElement $element The element that should be associated with this element.
* @return $this
*/
public function setTargetElement(?DBElement $element): self
{
if ($element === null) {
$this->target_id = 0;
$this->target_type = self::TARGET_TYPE_NONE;
return $this;
}
$this->target_type = static::targetTypeClassToID(get_class($element));
$this->target_id = $element->getID();
return $this;
}
/**
* This function converts the internal numeric log level into an PSR3 compatible level string.
* @param int $level The numerical log level
* @return string The PSR3 compatible level string
*/
final public static function levelIntToString(int $level): string
{
if (!isset(self::LEVEL_ID_TO_STRING[$level])) {
throw new \InvalidArgumentException('No level with this int is existing!');
}
return self::LEVEL_ID_TO_STRING[$level];
}
/**
* This function converts a PSR3 compatible string to the internal numeric level string.
* @param string $level the PSR3 compatible string that should be converted
* @return int The internal int representation.
*/
final public static function levelStringToInt(string $level): int
{
$tmp = array_flip(self::LEVEL_ID_TO_STRING);
if (!isset($tmp[$level])) {
throw new \InvalidArgumentException('No level with this string is existing!');
}
return $tmp[$level];
}
/**
* Converts an target type id to an full qualified class name.
* @param int $type_id The target type ID
* @return string
*/
final public static function targetTypeIdToClass(int $type_id): string
{
if (!isset(self::TARGET_CLASS_MAPPING[$type_id])) {
throw new \InvalidArgumentException('No target type with this ID is existing!');
}
return self::TARGET_CLASS_MAPPING[$type_id];
}
/**
* Convert a class name to a target type ID.
* @param string $class The name of the class (FQN) that should be converted to id
* @return int The ID of the associated target type ID.
*/
final public static function targetTypeClassToID(string $class): int
{
$tmp = array_flip(self::TARGET_CLASS_MAPPING);
//Check if we can use a key directly
if (isset($tmp[$class])) {
return $tmp[$class];
}
//Otherwise we have to iterate over everything and check for inheritance
foreach ($tmp as $compare_class => $class_id) {
if (is_a($class, $compare_class, true)) {
return $class_id;
}
}
throw new \InvalidArgumentException('No target ID for this class is existing!');
}
}

View file

@ -0,0 +1,39 @@
<?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;
use App\Exceptions\LogEntryObsoleteException;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
* @package App\Entity\LogSystem
*/
class ConfigChangedLogEntry extends AbstractLogEntry
{
protected $typeString = "config_changed";
public function __construct()
{
throw new LogEntryObsoleteException();
}
}

View file

@ -0,0 +1,39 @@
<?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;
use App\Exceptions\LogEntryObsoleteException;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class DatabaseUpdatedLogEntry extends AbstractLogEntry
{
protected $typeString = "database_updated";
public function __construct()
{
throw new LogEntryObsoleteException();
}
}

View file

@ -0,0 +1,34 @@
<?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;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
* @package App\Entity\LogSystem
*/
class ElementCreatedLogEntry extends AbstractLogEntry
{
protected $typeString = "element_created";
}

View file

@ -0,0 +1,33 @@
<?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;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class ElementDeletedLogEntry extends AbstractLogEntry
{
protected $typeString = "element_deleted";
}

View file

@ -0,0 +1,33 @@
<?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;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
* @package App\Entity\LogSystem
*/
class ElementEditedLogEntry extends AbstractLogEntry
{
protected $typeString = "element_edited";
}

View file

@ -0,0 +1,40 @@
<?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;
use App\Exceptions\LogEntryObsoleteException;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
* @package App\Entity\LogSystem
*/
class ExceptionLogEntry extends AbstractLogEntry
{
protected $typeString = 'exception';
public function __construct()
{
throw new LogEntryObsoleteException();
}
}

View file

@ -0,0 +1,34 @@
<?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;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
* @package App\Entity\LogSystem
*/
class InstockChangedLogEntry extends AbstractLogEntry
{
protected $typeString = "instock_changed";
}

View file

@ -0,0 +1,34 @@
<?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;
use Doctrine\ORM\Mapping as ORM;
/**
* This log entry is created when a user logs in.
* @package App\Entity\LogSystem
* @ORM\Entity()
*/
class UserLoginLogEntry extends AbstractLogEntry
{
protected $typeString = "user_login";
}

View file

@ -0,0 +1,34 @@
<?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;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
* @package App\Entity\LogSystem
*/
class UserLogoutLogEntry extends AbstractLogEntry
{
protected $typeString = "user_logout";
}

View file

@ -0,0 +1,41 @@
<?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;
use App\Exceptions\LogEntryObsoleteException;
use Doctrine\ORM\Mapping as ORM;
/**
* @package App\Entity\LogSystem
* @ORM\Entity()
*/
class UserNotAllowedLogEntry extends AbstractLogEntry
{
protected $type = 'user_not_allowed';
public function __construct()
{
//Obsolete, use server log now.
throw new LogEntryObsoleteException();
}
}

View 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\Exceptions;
class LogEntryObsoleteException extends \RuntimeException
{
protected $message = "This log entry is obsolete and exists only for compatibility reasons with old Part-DB versions. You should not use it!";
}

View file

@ -0,0 +1,75 @@
<?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\Repository;
use App\Entity\Base\DBElement;
use App\Entity\LogSystem\AbstractLogEntry;
use Doctrine\ORM\EntityRepository;
class LogEntryRepository extends EntityRepository
{
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
{
//Emulate a target element criteria by splitting it manually in the needed criterias
if (isset($criteria['target']) && $criteria['target'] instanceof DBElement) {
$element = $criteria['target'];
$criteria['target_id'] = $element;
$criteria['target_type'] = AbstractLogEntry::targetTypeClassToID(get_class($element));
unset($criteria['target']);
}
return parent::findBy($criteria, $orderBy, $limit, $offset); // TODO: Change the autogenerated stub
}
/**
* Find log entries associated with the given element (the history of the element)
* @param DBElement $element The element for which the history should be generated
* @param string $order By default newest entries are shown first. Change this to ASC to show oldest entries first.
* @param null $limit
* @param null $offset
* @return AbstractLogEntry[]
*/
public function getElementHistory(DBElement $element, $order = 'DESC', $limit = null, $offset = null)
{
return $this->findBy(['element' => $element], ['timestamp' => $order], $limit, $offset);
}
/**
* Gets the target element associated with the logentry.
* @param AbstractLogEntry $logEntry
* @return DBElement|null Returns the associated DBElement or null if the log either has no target or the element
* was deleted from DB.
*/
public function getTargetElement(AbstractLogEntry $logEntry): ?DBElement
{
$class = $logEntry->getTargetClass();
$id = $logEntry->getTargetID();
if ($class === null || $id === null) {
return null;
}
return $this->getEntityManager()->find($class, $id);
}
}

View file

@ -82,22 +82,24 @@ class ElementTypeNameGenerator
* Useful when the type should be shown to user. * Useful when the type should be shown to user.
* Throws an exception if the class is not supported. * Throws an exception if the class is not supported.
* *
* @param DBElement $entity The element for which the label should be generated * @param DBElement|string $entity The element or class for which the label should be generated
* *
* @return string the locatlized label for the entity type * @return string the localized label for the entity type
* *
* @throws EntityNotSupportedException when the passed entity is not supported * @throws EntityNotSupportedException when the passed entity is not supported
*/ */
public function getLocalizedTypeLabel(DBElement $entity): string public function getLocalizedTypeLabel($entity): string
{ {
$class = is_string($entity) ? $entity : get_class($entity);
//Check if we have an direct array entry for our entity class, then we can use it //Check if we have an direct array entry for our entity class, then we can use it
if (isset($this->mapping[get_class($entity)])) { if (isset($this->mapping[$class])) {
return $this->mapping[get_class($entity)]; return $this->mapping[$class];
} }
//Otherwise iterate over array and check for inheritance (needed when the proxy element from doctrine are passed) //Otherwise iterate over array and check for inheritance (needed when the proxy element from doctrine are passed)
foreach ($this->mapping as $class => $translation) { foreach ($this->mapping as $class => $translation) {
if ($entity instanceof $class) { if (is_a($entity, $class, true)) {
return $translation; return $translation;
} }
} }

View file

@ -0,0 +1,16 @@
{% extends "base.html.twig" %}
{% block title %}{% trans %}log.list.title{% endtrans %}{% endblock %}
{% block content %}
<div id="part_list" class="" data-datatable data-settings='{{ datatable_settings(datatable) }}'>
<div class="card-body">
<div class="card">
<div class="card-body">
<h4>{% trans %}part_list.loading.caption{% endtrans %}</h4>
<h6>{% trans %}part_list.loading.message{% endtrans %}</h6>
</div>
</div>
</div>
</div>
{% endblock %}

View file

@ -0,0 +1,143 @@
<?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\Tests\Entity\LogSystem;
use App\Entity\Attachments\Attachment;
use App\Entity\Attachments\AttachmentType;
use App\Entity\Attachments\PartAttachment;
use App\Entity\Devices\Device;
use App\Entity\LogSystem\AbstractLogEntry;
use App\Entity\Parts\Category;
use App\Entity\Parts\Footprint;
use App\Entity\Parts\Manufacturer;
use App\Entity\Parts\Part;
use App\Entity\Parts\Storelocation;
use App\Entity\Parts\Supplier;
use App\Entity\UserSystem\Group;
use App\Entity\UserSystem\User;
use PHPUnit\Framework\TestCase;
use App\Entity\Devices\DevicePart;
class AbstractLogEntryTest extends TestCase
{
public function levelDataProvider(): array
{
return [
[0, 'emergency'],
[1, 'alert'],
[2, 'critical'],
[3, 'error'],
[4, 'warning'],
[5, 'notice'],
[6, 'info'],
[7, 'debug'],
[8, 'blabla', true],
[-1, 'test', true]
];
}
public function targetTypeDataProvider(): array
{
return [
[1, User::class],
[2, Attachment::class],
[3, AttachmentType::class],
[4, Category::class],
[5, Device::class],
[6, DevicePart::class],
[7, Footprint::class],
[8, Group::class],
[9, Manufacturer::class],
[10, Part::class],
[11, Storelocation::class],
[12, Supplier::class],
[-1, 'blablub', true]
];
}
/**
* @dataProvider levelDataProvider
*/
public function testLevelIntToString(int $int, string $expected_string, bool $expect_exception = false)
{
if ($expect_exception) {
$this->expectException(\InvalidArgumentException::class);
}
$this->assertSame($expected_string, AbstractLogEntry::levelIntToString($int));
}
/**
* @dataProvider levelDataProvider
*/
public function testLevelStringToInt(int $expected_int, string $string, bool $expect_exception = false)
{
if ($expect_exception) {
$this->expectException(\InvalidArgumentException::class);
}
$this->assertSame($expected_int, AbstractLogEntry::levelStringToInt($string));
}
/**
* @dataProvider targetTypeDataProvider
*/
public function testTargetTypeIdToClass(int $int, string $expected_class, bool $expect_exception = false)
{
if ($expect_exception) {
$this->expectException(\InvalidArgumentException::class);
}
$this->assertSame($expected_class, AbstractLogEntry::targetTypeIdToClass($int));
}
/**
* @dataProvider targetTypeDataProvider
*/
public function testTypeClassToID(int $expected_id, string $class, bool $expect_exception = false)
{
if ($expect_exception) {
$this->expectException(\InvalidArgumentException::class);
}
$this->assertSame($expected_id, AbstractLogEntry::targetTypeClassToID($class));
}
public function testTypeClassToIDSubclasses()
{
//Test if class mapping works for subclasses
$this->assertSame(2, AbstractLogEntry::targetTypeClassToID(PartAttachment::class));
}
public function testSetGetTarget()
{
$part = $this->createMock(Part::class);
$part->method('getID')->willReturn(10);
$log = new class extends AbstractLogEntry {};
$log->setTargetElement($part);
$this->assertSame(Part::class, $log->getTargetClass());
$this->assertSame(10, $log->getTargetID());
$log->setTargetElement(null);
$this->assertSame(null, $log->getTargetClass());
$this->assertSame(null, $log->getTargetID());
}
}

View file

@ -59,6 +59,9 @@ class ElementTypeNameGeneratorTest extends WebTestCase
//Test inheritance //Test inheritance
$this->assertSame('Attachment', $this->service->getLocalizedTypeLabel(new PartAttachment())); $this->assertSame('Attachment', $this->service->getLocalizedTypeLabel(new PartAttachment()));
//Test for class name
$this->assertSame('Part', $this->service->getLocalizedTypeLabel(Part::class));
//Test exception for unknpwn type //Test exception for unknpwn type
$this->expectException(EntityNotSupportedException::class); $this->expectException(EntityNotSupportedException::class);
$this->service->getLocalizedTypeLabel(new class() extends DBElement { $this->service->getLocalizedTypeLabel(new class() extends DBElement {