Merge branch 'log_system'

This commit is contained in:
Jan Böhmer 2020-01-26 21:31:54 +01:00
commit c594c98292
40 changed files with 2726 additions and 20 deletions

View file

@ -386,8 +386,8 @@ class AjaxUI {
switch(request.status) {
case 500:
title = 'Internal Server Error!';
break;
title = 'Internal Server Error!';
break;
case 404:
title = "Site not found!";
break;
@ -396,7 +396,7 @@ class AjaxUI {
break;
}
var alert = bootbox.alert(
var alert = bootbox.alert(
{
size: 'large',
message: function() {
@ -418,10 +418,10 @@ class AjaxUI {
//@ts-ignore
alert.init(function (){
var dstFrame = document.getElementById('iframe');
//@ts-ignore
var dstDoc = dstFrame.contentDocument || dstFrame.contentWindow.document;
dstDoc.write(request.responseText);
dstDoc.close();
//@ts-ignore
var dstDoc = dstFrame.contentDocument || dstFrame.contentWindow.document;
dstDoc.write(request.responseText);
dstDoc.close();
});
@ -530,7 +530,31 @@ class AjaxUI {
"extend": 'colvis',
'className': 'mr-2 btn-light',
"text": "<i class='fa fa-cog'></i>"
}]
}],
"rowCallback": function( row, data, index ) {
//Check if we have a level, then change color of this row
if (data.level) {
let style = "";
switch(data.level) {
case "emergency":
case "alert":
case "critical":
case "error":
style = "table-danger";
break;
case "warning":
style = "table-warning";
break;
case "notice":
style = "table-info";
break;
}
if (style){
$(row).addClass(style);
}
}
}
});
//Register links.

View file

@ -12,7 +12,7 @@ doctrine:
date:
class: App\Helpers\UTCDateTimeType
schema_filter: ~^(?!internal|log)~
schema_filter: ~^(?!internal)~
profiling_collect_backtrace: true
orm:

View file

@ -31,7 +31,6 @@ security:
# https://symfony.com/doc/current/security/form_login_setup.html
form_login:
login_path: login
check_path: login
csrf_token_generator: security.csrf.token_manager
@ -41,6 +40,7 @@ security:
logout:
path: logout
target: homepage
handlers: [App\EventSubscriber\LogoutListener]
remember_me:
secret: '%kernel.secret%'

View file

@ -21,6 +21,8 @@ parameters:
sender_name: 'Part-DB Mailer' # The name that will be used for all mails sent by Part-DB
allow_email_pw_reset: '%env(validMailDSN:MAILER_DSN)%' # Config if users are able, to reset their password by email. By default this enabled, when a mail server is configured.
locale_menu: ['en', 'de', 'ru'] # The languages that are shown in user drop down menu
# If this option is activated, IP addresses are anonymized to be GPDR compliant
gpdr_compliance: true
services:
@ -30,6 +32,7 @@ services:
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
bind:
bool $demo_mode: '%demo_mode%'
bool $gpdr_compliance : '%gpdr_compliance%'
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
@ -49,6 +52,17 @@ services:
$email: '%sender_email%'
$name: '%sender_name%'
App\Services\LogSystem\EventLogger:
arguments:
# By default only log events which has minimum info level (debug levels are not logged)
# 7 is lowest level (debug), 0 highest (emergency
$minimum_log_level: 6
# Event classes specified here are not saved to DB
$blacklist: []
# Only the event classes specified here are saved to DB (set to []) to log all events
$whitelist: []
Liip\ImagineBundle\Service\FilterService:
alias: 'liip_imagine.service.filter'
@ -59,6 +73,11 @@ services:
tags:
- { name: "doctrine.orm.entity_listener" }
App\EventSubscriber\MigrationListener:
tags:
- { name: 'doctrine.event_subscriber' }
tree_invalidation_listener:
class: App\EntityListeners\TreeCacheInvalidationListener
tags:

View file

@ -0,0 +1,160 @@
<?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\Command;
use App\Entity\Base\NamedDBElement;
use App\Entity\LogSystem\AbstractLogEntry;
use App\Services\ElementTypeNameGenerator;
use App\Services\LogSystem\LogEntryExtraFormatter;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Contracts\Translation\TranslatorInterface;
class ShowEventLogCommand extends Command
{
protected static $defaultName = 'app:show-logs';
protected $entityManager;
protected $translator;
protected $elementTypeNameGenerator;
protected $repo;
protected $formatter;
public function __construct(EntityManagerInterface $entityManager,
TranslatorInterface $translator, ElementTypeNameGenerator $elementTypeNameGenerator, LogEntryExtraFormatter $formatter)
{
$this->entityManager = $entityManager;
$this->translator = $translator;
$this->elementTypeNameGenerator = $elementTypeNameGenerator;
$this->formatter = $formatter;
$this->repo = $this->entityManager->getRepository(AbstractLogEntry::class);
parent::__construct();
}
protected function configure()
{
$this
->setDescription('List the last event log entries.')
->addOption('count', 'c', InputOption::VALUE_REQUIRED, 'How many log entries should be shown per page.', 50 )
->addOption('oldest_first', null, InputOption::VALUE_NONE,'Show older entries first.')
->addOption('page', 'p', InputOption::VALUE_REQUIRED, 'Which page should be shown?', 1)
->addOption('onePage', null, InputOption::VALUE_NONE, 'Show only one page (dont ask to go to next).')
->addOption('showExtra', 'x', InputOption::VALUE_NONE, 'Show a column with the extra data.');
;
}
public function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$onePage = $input->getOption('onePage');
$desc = $input->getOption('oldest_first');
$limit = $input->getOption('count');
$page = $input->getOption('page');
$showExtra = $input->getOption('showExtra');
$total_count = $this->repo->count([]);
$max_page = ceil($total_count / $limit);
if ($page > $max_page) {
$io->error("There is no page $page! The maximum page is $max_page.");
return 1;
}
$io->note("There are a total of $total_count log entries in the DB.");
$continue = true;
while ($continue && $page <= $max_page) {
$this->showPage($output, $desc, $limit, $page, $max_page, $showExtra);
if ($onePage) {
return 0;
}
$continue = $io->confirm('Do you want to show the next page?');
$page++;
}
return 0;
}
protected function showPage(OutputInterface $output, bool $desc, int $limit, int $page, int $max_page, bool $showExtra): void
{
$sorting = $desc ? 'ASC' : 'DESC';
$offset = ($page - 1) * $limit;
/** @var AbstractLogEntry[] $entries */
$entries = $this->repo->getLogsOrderedByTimestamp($sorting, $limit, $offset);
$table = new Table($output);
$table->setHeaderTitle("Page $page / $max_page");
$headers = ['ID', 'Timestamp', 'Type', 'User', 'Target Type', 'Target'];
if ($showExtra) {
$headers[] = 'Extra data';
}
$table->setHeaders($headers);
foreach ($entries as $entry) {
$this->addTableRow($table, $entry, $showExtra);
}
$table->render();
}
protected function addTableRow(Table $table, AbstractLogEntry $entry, bool $showExtra): void
{
$target = $this->repo->getTargetElement($entry);
$target_name = "";
if ($target instanceof NamedDBElement) {
$target_name = $target->getName() . ' <info>(' . $target->getID() . ')</info>';
} elseif ($entry->getTargetID()) {
$target_name = '<info>(' . $entry->getTargetID() . ')</info>';
}
$target_class = "";
if ($entry->getTargetClass() !== null) {
$target_class = $this->elementTypeNameGenerator->getLocalizedTypeLabel($entry->getTargetClass());
}
$row = [
$entry->getID(),
$entry->getTimestamp()->format('Y-m-d H:i:s'),
$entry->getType(),
$entry->getUser()->getFullName(true),
$target_class,
$target_name
];
if ($showExtra) {
$row[] = $this->formatter->formatConsole($entry);
}
$table->addRow($row);
}
}

View file

@ -0,0 +1,60 @@
<?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 showLogs(Request $request, DataTableFactory $dataTable)
{
$this->denyAccessUnlessGranted('@system.show_logs');
$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

@ -130,7 +130,7 @@ class UserController extends AdminPages\BaseAdminController
/**
* @Route("/info", name="user_info_self")
* @Route("/{id}/info")
* @Route("/{id}/info", name="user_info")
*/
public function userInfo(?User $user, Packages $packages)
{

View file

@ -0,0 +1,61 @@
<?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\LogSystem\DatabaseUpdatedLogEntry;
use App\Entity\LogSystem\ElementCreatedLogEntry;
use App\Entity\LogSystem\ElementDeletedLogEntry;
use App\Entity\LogSystem\ElementEditedLogEntry;
use App\Entity\LogSystem\ExceptionLogEntry;
use App\Entity\LogSystem\InstockChangedLogEntry;
use App\Entity\LogSystem\UserLoginLogEntry;
use App\Entity\LogSystem\UserLogoutLogEntry;
use App\Entity\LogSystem\UserNotAllowedLogEntry;
use App\Services\LogSystem\LogEntryExtraFormatter;
use Omines\DataTablesBundle\Column\AbstractColumn;
use Symfony\Contracts\Translation\TranslatorInterface;
class LogEntryExtraColumn extends AbstractColumn
{
protected $translator;
protected $formatter;
public function __construct(TranslatorInterface $translator, LogEntryExtraFormatter $formatter)
{
$this->translator = $translator;
$this->formatter = $formatter;
}
/**
* @inheritDoc
*/
public function normalize($value)
{
return $value;
}
public function render($value, $context)
{
return $this->formatter->format($context);
}
}

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,175 @@
<?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\LogEntryExtraColumn;
use App\DataTables\Column\LogEntryTargetColumn;
use App\Entity\Attachments\Attachment;
use App\Entity\LogSystem\AbstractLogEntry;
use App\Entity\UserSystem\User;
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 Psr\Log\LogLevel;
use SebastianBergmann\CodeCoverage\Report\Text;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class LogDataTable implements DataTableTypeInterface
{
protected $elementTypeNameGenerator;
protected $translator;
protected $urlGenerator;
public function __construct(ElementTypeNameGenerator $elementTypeNameGenerator, TranslatorInterface $translator,
UrlGeneratorInterface $urlGenerator)
{
$this->elementTypeNameGenerator = $elementTypeNameGenerator;
$this->translator = $translator;
$this->urlGenerator = $urlGenerator;
}
public function configure(DataTable $dataTable, array $options)
{
$dataTable->add('symbol', TextColumn::class, [
'label' => '',
'render' => function ($value, AbstractLogEntry $context) {
switch ($context->getLevelString()) {
case LogLevel::DEBUG:
$symbol = 'fa-bug';
break;
case LogLevel::INFO:
$symbol = 'fa-info';
break;
case LogLevel::NOTICE:
$symbol = 'fa-flag';
break;
case LogLevel::WARNING:
$symbol = 'fa-exclamation-circle';
break;
case LogLevel::ERROR:
$symbol = 'fa-exclamation-triangle';
break;
case LogLevel::CRITICAL:
$symbol = 'fa-bolt';
break;
case LogLevel::ALERT:
$symbol = 'fa-radiation';
break;
case LogLevel::EMERGENCY:
$symbol = 'fa-skull-crossbones';
break;
default:
$symbol = 'fa-question-circle';
break;
}
return sprintf('<i class="fas fa-fw %s"></i>', $symbol);
}
]);
$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 $value;
}
]);
$dataTable->add('user', TextColumn::class, [
'label' => $this->translator->trans('log.user'),
'render' => function ($value, AbstractLogEntry $context) {
$user = $context->getUser();
return sprintf(
'<a href="%s">%s</a>',
$this->urlGenerator->generate('user_info', ['id' => $user->getID()]),
$user->getFullName(true)
);
}
]);
$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->add('extra', LogEntryExtraColumn::class, [
'label' => $this->translator->trans('log.extra')
]);
$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
*/
final public function getID(): ?int
public function getID(): ?int
{
return $this->id;
}

View file

@ -0,0 +1,392 @@
<?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 = "InstockChangedLogEntry",
* 10 = "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", nullable=false)
*/
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";
/** @var array The extra data in raw (short form) saved in the DB
* @ORM\Column(name="extra", type="json")
*/
protected $extra = [];
public function __construct()
{
$this->timestamp = new DateTime();
$this->level = self::LEVEL_WARNING;
}
/**
* 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;
}
public function getExtraData(): array
{
return $this->extra;
}
/**
* 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,69 @@
<?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(string $oldVersion, string $newVersion)
{
parent::__construct();
$this->extra['o'] = $oldVersion;
$this->extra['n'] = $newVersion;
}
/**
* Checks if the database update was successful.
* @return bool
*/
public function isSuccessful(): bool
{
//We dont save unsuccessful updates now, so just assume it to save space.
return $this->extra['s'] ?? true;
}
/**
* Gets the database version before update.
* @return int
*/
public function getOldVersion(): string
{
return (string) $this->extra['o'];
}
/**
* Gets the (target) database version after update.
* @return int
*/
public function getNewVersion(): string
{
return (string) $this->extra['n'];
}
}

View file

@ -0,0 +1,52 @@
<?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";
/**
* Gets the instock when the part was created
* @return int|null
*/
public function getCreationInstockValue(): ?int
{
return $this->extra['i'] ?? null;
}
/**
* Checks if a creation instock value was saved with this entry.
* @return bool
*/
public function hasCreationInstockValue(): bool
{
return $this->getCreationInstockValue() !== null;
}
}

View file

@ -0,0 +1,38 @@
<?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";
public function getOldName(): string
{
return $this->extra['n'];
}
}

View file

@ -0,0 +1,42 @@
<?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";
/**
* Returns the message associated with this edit change
* @return string
*/
public function getMessage() : string
{
return $this->extra['m'] ?? '';
}
}

View file

@ -0,0 +1,77 @@
<?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();
}
/**
* The class name of the exception that caused this log entry.
* @return string
*/
public function getExceptionClass(): string
{
return $this->extra['t'] ?? "Unknown Class";
}
/**
* Returns the file where the exception happened.
* @return string
*/
public function getFile(): string
{
return $this->extra['f'];
}
/**
* Returns the line where the exception happened
* @return int
*/
public function getLine(): int
{
return $this->extra['l'];
}
/**
* Return the message of the exception.
* @return string
*/
public function getMessage(): string
{
return $this->extra['m'];
}
}

View file

@ -0,0 +1,105 @@
<?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\Entity\Parts\Part;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
* @package App\Entity\LogSystem
*/
class InstockChangedLogEntry extends AbstractLogEntry
{
protected $typeString = "instock_changed";
/**
* Get the old instock
* @return int
*/
public function getOldInstock(): int
{
return $this->extra['o'];
}
/**
* Get the new instock
* @return int
*/
public function getNewInstock(): int
{
return $this->extra['n'];
}
/**
* Gets the comment associated with the instock change
* @return string
*/
public function getComment(): string
{
return $this->extra['c'];
}
/**
* Returns the price that has to be payed for the change (in the base currency).
* @param $absolute bool Set this to true, if you want only get the absolute value of the price (without minus)
* @return float
*/
public function getPrice(bool $absolute = false): float
{
if ($absolute) {
return abs($this->extra['p']);
}
return $this->extra['p'];
}
/**
* Returns the difference value of the change ($new_instock - $old_instock).
* @param $absolute bool Set this to true if you want only the absolute value of the difference.
* @return int Difference is positive if instock has increased, negative if decreased.
*/
public function getDifference(bool $absolute = false): int
{
// Check if one of the instock values is unknown
if ($this->getNewInstock() == -2 || $this->getOldInstock() == -2) {
return 0;
}
$difference = $this->getNewInstock() - $this->getOldInstock();
if ($absolute) {
return abs($difference);
}
return $difference;
}
/**
* Checks if the Change was an withdrawal of parts.
* @return bool True if the change was an withdrawal, false if not.
*/
public function isWithdrawal(): bool
{
return $this->getNewInstock() < $this->getOldInstock();
}
}

View file

@ -0,0 +1,67 @@
<?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;
use Symfony\Component\HttpFoundation\IpUtils;
/**
* This log entry is created when a user logs in.
* @package App\Entity\LogSystem
* @ORM\Entity()
*/
class UserLoginLogEntry extends AbstractLogEntry
{
protected $typeString = "user_login";
public function __construct(string $ip_address, bool $anonymize = true)
{
parent::__construct();
$this->level = self::LEVEL_INFO;
$this->setIPAddress($ip_address, $anonymize);
}
/**
* Return the (anonymized) IP address used to login the user.
* @return string
*/
public function getIPAddress(): string
{
return $this->extra['i'];
}
/**
* Sets the IP address used to login the user
* @param string $ip The IP address used to login the user.
* @param bool $anonymize Anonymize the IP address (remove last block) to be GPDR compliant
* @return $this
*/
public function setIPAddress(string $ip, bool $anonymize = true): self
{
if ($anonymize) {
$ip = IpUtils::anonymize($ip);
}
$this->extra['i'] = $ip;
return $this;
}
}

View file

@ -0,0 +1,69 @@
<?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;
use Symfony\Component\HttpFoundation\IpUtils;
/**
* @ORM\Entity()
* @package App\Entity\LogSystem
*/
class UserLogoutLogEntry extends AbstractLogEntry
{
protected $typeString = "user_logout";
public function __construct(string $ip_address, bool $anonymize = true)
{
parent::__construct();
$this->level = self::LEVEL_INFO;
$this->setIPAddress($ip_address, $anonymize);
}
/**
* Return the (anonymized) IP address used to login the user.
* @return string
*/
public function getIPAddress(): string
{
return $this->extra['i'];
}
/**
* Sets the IP address used to login the user
* @param string $ip The IP address used to login the user.
* @param bool $anonymize Anonymize the IP address (remove last block) to be GPDR compliant
* @return $this
*/
public function setIPAddress(string $ip, bool $anonymize = true): self
{
if ($anonymize) {
$ip = IpUtils::anonymize($ip);
}
$this->extra['i'] = $ip;
return $this;
}
}

View file

@ -0,0 +1,46 @@
<?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 $typeString = 'user_not_allowed';
public function __construct()
{
//Obsolete, use server log now.
throw new LogEntryObsoleteException();
}
public function getMessage(): string
{
return $this->extra['p'] ?? '';
}
}

View file

@ -24,6 +24,9 @@ declare(strict_types=1);
namespace App\EventSubscriber;
use App\Entity\LogSystem\UserLoginLogEntry;
use App\Entity\UserSystem\User;
use App\Services\LogSystem\EventLogger;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
@ -37,15 +40,27 @@ final class LoginSuccessListener implements EventSubscriberInterface
{
protected $translator;
protected $flashBag;
protected $eventLogger;
protected $gpdr_compliance;
public function __construct(TranslatorInterface $translator, FlashBagInterface $flashBag)
public function __construct(TranslatorInterface $translator, FlashBagInterface $flashBag, EventLogger $eventLogger, bool $gpdr_compliance)
{
$this->translator = $translator;
$this->flashBag = $flashBag;
$this->eventLogger = $eventLogger;
$this->gpdr_compliance = $gpdr_compliance;
}
public function onLogin(InteractiveLoginEvent $event): void
{
$ip = $event->getRequest()->getClientIp();
$log = new UserLoginLogEntry($ip, $this->gpdr_compliance);
$user = $event->getAuthenticationToken()->getUser();
if ($user instanceof User) {
$log->setTargetElement($user);
}
$this->eventLogger->logAndFlush($log);
$this->flashBag->add('notice', $this->translator->trans('flash.login_successful'));
}

View file

@ -0,0 +1,57 @@
<?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\LogSystem\UserLogoutLogEntry;
use App\Entity\UserSystem\User;
use App\Services\LogSystem\EventLogger;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Logout\LogoutHandlerInterface;
class LogoutListener implements LogoutHandlerInterface
{
protected $logger;
protected $gpdr_compliance;
public function __construct(EventLogger $logger, bool $gpdr_compliance)
{
$this->logger = $logger;
$this->gpdr_compliance = $gpdr_compliance;
}
/**
* @inheritDoc
*/
public function logout(Request $request, Response $response, TokenInterface $token)
{
$log = new UserLogoutLogEntry($request->getClientIp(), $this->gpdr_compliance);
$user = $token->getUser();
if ($user instanceof User) {
$log->setTargetElement($user);
}
$this->logger->logAndFlush($log);
}
}

View file

@ -0,0 +1,87 @@
<?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\LogSystem\DatabaseUpdatedLogEntry;
use App\Services\LogSystem\EventLogger;
use Doctrine\Common\EventSubscriber;
use Doctrine\Migrations\Event\MigrationsEventArgs;
use Doctrine\Migrations\Event\MigrationsVersionEventArgs;
use Doctrine\Migrations\Events;
class MigrationListener implements EventSubscriber
{
protected $old_version = null;
protected $new_version = null;
protected $eventLogger;
public function __construct(EventLogger $eventLogger)
{
$this->eventLogger = $eventLogger;
}
public function onMigrationsMigrated(MigrationsEventArgs $args): void
{
//Dont do anything if this was a dry run
if ($args->isDryRun()) {
return;
}
//Save the version after the migration
$this->new_version = $args->getConfiguration()->getCurrentVersion();
//After everything is done, write the results to DB log
$this->old_version = empty($this->old_version) ? 'legacy/empty' : $this->old_version;
$this->new_version = empty($this->new_version) ? 'unknown' : $this->new_version;
try {
$log = new DatabaseUpdatedLogEntry($this->old_version, $this->new_version);
$this->eventLogger->logAndFlush($log);
} catch (\Exception $exception) {
//Ignore any exception occuring here...
}
}
public function onMigrationsMigrating(MigrationsEventArgs $args): void
{
// Save the version before any migration
if ($this->old_version == null) {
$this->old_version = $args->getConfiguration()->getCurrentVersion();
}
}
/**
* @inheritDoc
*/
public function getSubscribedEvents()
{
return [
Events::onMigrationsMigrated,
Events::onMigrationsMigrating,
];
}
}

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,43 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20200126191823 extends AbstractMigration
{
public function getDescription() : string
{
return 'Improve the schema of the log table';
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE log CHANGE datetime datetime DATETIME NOT NULL, CHANGE level level TINYINT, CHANGE extra extra LONGTEXT NOT NULL COMMENT \'(DC2Type:json)\'');
$this->addSql('DROP INDEX id_user ON log');
$this->addSql('ALTER TABLE log ADD CONSTRAINT FK_8F3F68C56B3CA4B FOREIGN KEY (id_user) REFERENCES `users` (id)');
$this->addSql('CREATE INDEX IDX_8F3F68C56B3CA4B ON log (id_user)');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE log DROP FOREIGN KEY FK_8F3F68C56B3CA4B');
$this->addSql('ALTER TABLE log DROP FOREIGN KEY FK_8F3F68C56B3CA4B');
$this->addSql('ALTER TABLE log CHANGE datetime datetime DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, CHANGE level level TINYINT(1) NOT NULL, CHANGE extra extra MEDIUMTEXT CHARACTER SET utf8 NOT NULL COLLATE `utf8_general_ci`');
$this->addSql('DROP INDEX idx_8f3f68c56b3ca4b ON log');
$this->addSql('CREATE INDEX id_user ON log (id_user)');
$this->addSql('ALTER TABLE log ADD CONSTRAINT FK_8F3F68C56B3CA4B FOREIGN KEY (id_user) REFERENCES `users` (id)');
}
}

View file

@ -0,0 +1,87 @@
<?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 last log entries ordered by timestamp
* @param string $order
* @param null $limit
* @param null $offset
* @return array
*/
public function getLogsOrderedByTimestamp($order = 'DESC', $limit = null, $offset = null)
{
return $this->findBy([], ['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

@ -0,0 +1,70 @@
<?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\Security\Voter;
use App\Entity\LogSystem\AbstractLogEntry;
use App\Entity\UserSystem\User;
class LogEntryVoter extends ExtendedVoter
{
public const ALLOWED_OPS = ['read', 'delete'];
/**
* @inheritDoc
*/
protected function voteOnUser($attribute, $subject, User $user): bool
{
if ($subject instanceof AbstractLogEntry) {
if ($attribute === 'delete') {
return $this->resolver->inherit($user, 'system', 'delete_logs') ?? false;
}
if ($attribute === 'read') {
//Allow read of the users own log entries
if (
$subject->getUser() === $user
&& $this->resolver->inherit($user, 'self', 'show_logs')
) {
return true;
}
return $this->resolver->inherit($user, 'system','show_logs') ?? false;
}
}
return false;
}
/**
* @inheritDoc
*/
protected function supports($attribute, $subject)
{
if ($subject instanceof AbstractLogEntry) {
return in_array($subject, static::ALLOWED_OPS);
}
return false;
}
}

View file

@ -28,6 +28,7 @@ use App\Entity\UserSystem\User;
/**
* This voter allows you to directly check permissions from the permission structure, without passing an object.
* This use the syntax like "@permission.op"
* However you should use the "normal" object based voters if possible, because they are needed for a future ACL system.
*/
class PermissionVoter extends ExtendedVoter
@ -44,7 +45,7 @@ class PermissionVoter extends ExtendedVoter
$attribute = ltrim($attribute, '@');
[$perm, $op] = explode('.', $attribute);
return $this->resolver->inherit($user, $perm, $op);
return $this->resolver->inherit($user, $perm, $op) ?? false;
}
/**

View file

@ -82,22 +82,24 @@ class ElementTypeNameGenerator
* Useful when the type should be shown to user.
* 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
*/
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
if (isset($this->mapping[get_class($entity)])) {
return $this->mapping[get_class($entity)];
if (isset($this->mapping[$class])) {
return $this->mapping[$class];
}
//Otherwise iterate over array and check for inheritance (needed when the proxy element from doctrine are passed)
foreach ($this->mapping as $class => $translation) {
if ($entity instanceof $class) {
if (is_a($entity, $class, true)) {
return $translation;
}
}

View file

@ -0,0 +1,137 @@
<?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\Services\LogSystem;
use App\Entity\LogSystem\AbstractLogEntry;
use App\Entity\UserSystem\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Security;
class EventLogger
{
protected $minimum_log_level;
protected $blacklist;
protected $whitelist;
protected $em;
protected $security;
public function __construct(int $minimum_log_level, array $blacklist, array $whitelist, EntityManagerInterface $em, Security $security)
{
$this->minimum_log_level = $minimum_log_level;
$this->blacklist = $blacklist;
$this->whitelist = $whitelist;
$this->em = $em;
$this->security = $security;
}
/**
* Adds the given log entry to the Log, if the entry fullfills the global configured criterias.
* The change will not be flushed yet.
* @param AbstractLogEntry $logEntry
* @return bool Returns true, if the event was added to log.
*/
public function log(AbstractLogEntry $logEntry): bool
{
$user = $this->security->getUser();
//If the user is not specified explicitly, set it to the current user
if (($user === null || $user instanceof User) && $logEntry->getUser() === null) {
if ($user === null) {
$repo = $this->em->getRepository(User::class);
$user = $repo->getAnonymousUser();
}
$logEntry->setUser($user);
}
if ($this->shouldBeAdded($logEntry)) {
$this->em->persist($logEntry);
return true;
}
return false;
}
/**
* Adds the given log entry to the Log, if the entry fullfills the global configured criterias and flush afterwards.
* @param AbstractLogEntry $logEntry
* @return bool Returns true, if the event was added to log.
*/
public function logAndFlush(AbstractLogEntry $logEntry): bool
{
$tmp = $this->log($logEntry);
$this->em->flush();
return $tmp;
}
public function shouldBeAdded(
AbstractLogEntry $logEntry,
?int $minimum_log_level = null,
?array $blacklist = null,
?array $whitelist = null
): bool {
//Apply the global settings, if nothing was specified
$minimum_log_level = $minimum_log_level ?? $this->minimum_log_level;
$blacklist = $blacklist ?? $this->blacklist;
$whitelist = $whitelist ?? $this->whitelist;
//Dont add the entry if it does not reach the minimum level
if ($logEntry->getLevel() > $minimum_log_level) {
return false;
}
//Check if the event type is black listed
if (!empty($blacklist) && $this->isObjectClassInArray($logEntry, $blacklist)) {
return false;
}
//Check for whitelisting
if (!empty($whitelist) && !$this->isObjectClassInArray($logEntry, $whitelist)) {
return false;
}
// By default all things should be added
return true;
}
/**
* Check if the object type is given in the classes array. This also works for inherited types
* @param object $object The object which should be checked
* @param string[] $classes The list of class names that should be used for checking.
* @return bool
*/
protected function isObjectClassInArray(object $object, array $classes): bool
{
//Check if the class is directly in the classes array
if (in_array(get_class($object), $classes)) {
return true;
}
//Iterate over all classes and check for inheritance
foreach ($classes as $class) {
if (is_a($object, $class)) {
return true;
}
}
return false;
}
}

View file

@ -0,0 +1,138 @@
<?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\Services\LogSystem;
use App\Entity\LogSystem\AbstractLogEntry;
use App\Entity\LogSystem\DatabaseUpdatedLogEntry;
use App\Entity\LogSystem\ElementCreatedLogEntry;
use App\Entity\LogSystem\ElementDeletedLogEntry;
use App\Entity\LogSystem\ElementEditedLogEntry;
use App\Entity\LogSystem\ExceptionLogEntry;
use App\Entity\LogSystem\InstockChangedLogEntry;
use App\Entity\LogSystem\UserLoginLogEntry;
use App\Entity\LogSystem\UserLogoutLogEntry;
use App\Entity\LogSystem\UserNotAllowedLogEntry;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Format the Extra field of a log entry in a user readible form.
* @package App\Services\LogSystem
*/
class LogEntryExtraFormatter
{
protected $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
/**
* Return an user viewable representation of the extra data in a log entry, styled for console output.
* @param AbstractLogEntry $logEntry
* @return string
*/
public function formatConsole(AbstractLogEntry $logEntry): string
{
$tmp = $this->format($logEntry);
//Just a simple tweak to make the console output more pretty.
$search = ['<i>', '</i>', '<b>', '</b>', ' <i class="fas fa-long-arrow-alt-right">'];
$replace = ['<info>', '</info>', '<error>', '</error>', '→'];
return str_replace($search, $replace, $tmp);
}
/**
* Return a HTML formatted string containing a user viewable form of the Extra data
* @param AbstractLogEntry $context
* @return string
*/
public function format(AbstractLogEntry $context): string
{
if ($context instanceof UserLoginLogEntry || $context instanceof UserLogoutLogEntry) {
return sprintf(
"<i>%s</i>: %s",
$this->translator->trans('log.user_login.ip'),
htmlspecialchars($context->getIPAddress())
);
}
if ($context instanceof ExceptionLogEntry) {
return sprintf(
'<i>%s</i> %s:%d : %s',
htmlspecialchars($context->getExceptionClass()),
htmlspecialchars($context->getFile()),
$context->getLine(),
htmlspecialchars($context->getMessage())
);
}
if ($context instanceof DatabaseUpdatedLogEntry) {
return sprintf(
'<i>%s</i> %s <i class="fas fa-long-arrow-alt-right"></i> %s',
$this->translator->trans($context->isSuccessful() ? 'log.database_updated.success' : 'log.database_updated.failure'),
$context->getOldVersion(),
$context->getNewVersion()
);
}
if ($context instanceof ElementCreatedLogEntry && $context->hasCreationInstockValue()) {
return sprintf(
'<i>%s</i>: %s',
$this->translator->trans('log.element_created.original_instock'),
$context->getCreationInstockValue()
);
}
if ($context instanceof ElementDeletedLogEntry) {
return sprintf(
'<i>%s</i>: %s',
$this->translator->trans('log.element_deleted.old_name'),
$context->getOldName()
);
}
if ($context instanceof ElementEditedLogEntry && !empty($context->getMessage())) {
return htmlspecialchars($context->getMessage());
}
if ($context instanceof InstockChangedLogEntry) {
return sprintf(
'<i>%s</i>; %s <i class="fas fa-long-arrow-alt-right"></i> %s (%s); %s: %s',
$this->translator->trans($context->isWithdrawal() ? 'log.instock_changed.withdrawal' : 'log.instock_changed.added'),
$context->getOldInstock(),
$context->getNewInstock(),
(!$context->isWithdrawal() ? '+' : '-') . $context->getDifference(true),
$this->translator->trans('log.instock_changed.comment'),
htmlspecialchars($context->getComment())
);
}
if ($context instanceof UserNotAllowedLogEntry) {
return htmlspecialchars($context->getMessage());
}
return "";
}
}

View file

@ -212,6 +212,13 @@ class ToolsTreeBuilder
);
}
if ($this->security->isGranted('@system.show_logs')) {
$nodes[] = new TreeViewNode(
$this->translator->trans('tree.tools.system.event_log'),
$this->urlGenerator->generate('log_view')
);
}
return $nodes;
}
}

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
$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
$this->expectException(EntityNotSupportedException::class);
$this->service->getLocalizedTypeLabel(new class() extends DBElement {

View file

@ -0,0 +1,69 @@
<?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\Services\LogSystem;
use App\Entity\LogSystem\AbstractLogEntry;
use App\Entity\LogSystem\UserLoginLogEntry;
use App\Entity\LogSystem\UserLogoutLogEntry;
use App\Services\LogSystem\EventLogger;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class EventLoggerTest extends WebTestCase
{
/**
* @var EventLogger
*/
protected $service;
protected function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
//Get an service instance.
self::bootKernel();
$this->service = self::$container->get(EventLogger::class);
}
public function testShouldBeAdded()
{
$event1 = new UserLoginLogEntry('127.0.0.1');
$event2 = new UserLogoutLogEntry('127.0.0.1');
$event2->setLevel(AbstractLogEntry::LEVEL_CRITICAL);
//Test without restrictions
$this->assertTrue($this->service->shouldBeAdded($event1, 7, [], []));
//Test minimum log level
$this->assertFalse($this->service->shouldBeAdded($event1, 2, [], []));
$this->assertTrue($this->service->shouldBeAdded($event2, 2, [], []));
//Test blacklist
$this->assertFalse($this->service->shouldBeAdded($event1, 7, [UserLoginLogEntry::class], []));
$this->assertTrue($this->service->shouldBeAdded($event2, 7, [UserLoginLogEntry::class], []));
//Test whitelist
$this->assertFalse($this->service->shouldBeAdded($event1, 7, [], [UserLogoutLogEntry::class]));
$this->assertTrue($this->service->shouldBeAdded($event2, 7, [], [UserLogoutLogEntry::class]));
}
}

View file

@ -6293,5 +6293,107 @@ Element 3</target>
<target>Dateigröße</target>
</segment>
</unit>
<unit id="6DOZlwQ" name="log.id">
<segment>
<source>log.id</source>
<target>ID</target>
</segment>
</unit>
<unit id="cMZOrO7" name="log.timestamp">
<segment>
<source>log.timestamp</source>
<target>Zeitstempel</target>
</segment>
</unit>
<unit id="Z0BzGVJ" name="log.type">
<segment>
<source>log.type</source>
<target>Ereignis</target>
</segment>
</unit>
<unit id="KNddOUS" name="log.level">
<segment>
<source>log.level</source>
<target>Level</target>
</segment>
</unit>
<unit id="3Tv5Xzj" name="log.user">
<segment>
<source>log.user</source>
<target>Benutzer</target>
</segment>
</unit>
<unit id="bgbGrN5" name="log.target_type">
<segment>
<source>log.target_type</source>
<target>Zieltyp</target>
</segment>
</unit>
<unit id="uKTMZBs" name="log.list.title">
<segment>
<source>log.list.title</source>
<target>Systemlog</target>
</segment>
</unit>
<unit id=".IgL4C2" name="log.target">
<segment>
<source>log.target</source>
<target>Ziel</target>
</segment>
</unit>
<unit id="O9Iw2cd" name="log.type.element_edited">
<segment>
<source>log.type.element_edited</source>
<target>Element bearbeitet</target>
</segment>
</unit>
<unit id="l5qNskF" name="log.type.user_login">
<segment>
<source>log.type.user_login</source>
<target>Nutzer eingeloggt</target>
</segment>
</unit>
<unit id="Jjl8edA" name="log.type.unknown">
<segment>
<source>log.type.unknown</source>
<target>Unbekannt</target>
</segment>
</unit>
<unit id="pjRvOZm" name="log.type.database_updated">
<segment>
<source>log.type.database_updated</source>
<target>Datenbank aktualisiert</target>
</segment>
</unit>
<unit id="r7Q.Vrm" name="log.type.exception">
<segment>
<source>log.type.exception</source>
<target>Unbehandelte Exception (veraltet)</target>
</segment>
</unit>
<unit id="eTGx8tR" name="log.target_deleted">
<segment>
<source>log.target_deleted</source>
<target>gelöscht</target>
</segment>
</unit>
<unit id="6suiYvP" name="log.type.user_logout">
<segment>
<source>log.type.user_logout</source>
<target>Nutzer ausgeloggt</target>
</segment>
</unit>
<unit id="n8VtTfW" name="log.type.element_created">
<segment>
<source>log.type.element_created</source>
<target>Element angelegt</target>
</segment>
</unit>
<unit id="WQkp3yE" name="log.type.element_deleted">
<segment>
<source>log.type.element_deleted</source>
<target>Element gelöscht</target>
</segment>
</unit>
</file>
</xliff>

View file

@ -6271,5 +6271,107 @@ Element 3</target>
<target>File size</target>
</segment>
</unit>
<unit id="6DOZlwQ" name="log.id">
<segment>
<source>log.id</source>
<target>ID</target>
</segment>
</unit>
<unit id="cMZOrO7" name="log.timestamp">
<segment>
<source>log.timestamp</source>
<target>Timestamp</target>
</segment>
</unit>
<unit id="Z0BzGVJ" name="log.type">
<segment>
<source>log.type</source>
<target>Event</target>
</segment>
</unit>
<unit id="KNddOUS" name="log.level">
<segment>
<source>log.level</source>
<target>Level</target>
</segment>
</unit>
<unit id="3Tv5Xzj" name="log.user">
<segment>
<source>log.user</source>
<target>User</target>
</segment>
</unit>
<unit id="bgbGrN5" name="log.target_type">
<segment>
<source>log.target_type</source>
<target>Target type</target>
</segment>
</unit>
<unit id=".IgL4C2" name="log.target">
<segment>
<source>log.target</source>
<target>Target</target>
</segment>
</unit>
<unit id="r7Q.Vrm" name="log.type.exception">
<segment>
<source>log.type.exception</source>
<target>Unhandled exception (obsolete)</target>
</segment>
</unit>
<unit id="l5qNskF" name="log.type.user_login">
<segment>
<source>log.type.user_login</source>
<target>User login</target>
</segment>
</unit>
<unit id="6suiYvP" name="log.type.user_logout">
<segment>
<source>log.type.user_logout</source>
<target>User logout</target>
</segment>
</unit>
<unit id="Jjl8edA" name="log.type.unknown">
<segment>
<source>log.type.unknown</source>
<target>Unknown</target>
</segment>
</unit>
<unit id="n8VtTfW" name="log.type.element_created">
<segment>
<source>log.type.element_created</source>
<target>Element created</target>
</segment>
</unit>
<unit id="O9Iw2cd" name="log.type.element_edited">
<segment>
<source>log.type.element_edited</source>
<target>Element edited</target>
</segment>
</unit>
<unit id="WQkp3yE" name="log.type.element_deleted">
<segment>
<source>log.type.element_deleted</source>
<target>Element deleted</target>
</segment>
</unit>
<unit id="eTGx8tR" name="log.target_deleted">
<segment>
<source>log.target_deleted</source>
<target>deleted</target>
</segment>
</unit>
<unit id="pjRvOZm" name="log.type.database_updated">
<segment>
<source>log.type.database_updated</source>
<target>Database updated</target>
</segment>
</unit>
<unit id="uKTMZBs" name="log.list.title">
<segment>
<source>log.list.title</source>
<target>System log</target>
</segment>
</unit>
</file>
</xliff>