2020-01-24 22:57:04 +01:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2020-02-22 18:14:36 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-02-01 16:17:20 +01:00
|
|
|
|
2020-01-24 22:57:04 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Entity\LogSystem;
|
|
|
|
|
2023-06-11 14:55:06 +02:00
|
|
|
use Doctrine\DBAL\Types\Types;
|
2020-02-01 19:48:07 +01:00
|
|
|
use App\Entity\Base\AbstractDBElement;
|
2020-01-24 22:57:04 +01:00
|
|
|
use App\Entity\UserSystem\User;
|
|
|
|
use DateTime;
|
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
2023-05-28 01:55:30 +02:00
|
|
|
use App\Repository\LogEntryRepository;
|
2020-01-24 22:57:04 +01:00
|
|
|
|
|
|
|
/**
|
2023-04-15 23:14:53 +02:00
|
|
|
* This entity describes an entry in the event log.
|
2023-06-11 15:02:59 +02:00
|
|
|
* @see \App\Tests\Entity\LogSystem\AbstractLogEntryTest
|
2020-01-24 22:57:04 +01:00
|
|
|
*/
|
2023-05-28 01:55:30 +02:00
|
|
|
#[ORM\Entity(repositoryClass: LogEntryRepository::class)]
|
|
|
|
#[ORM\Table('log')]
|
2023-05-28 01:51:13 +02:00
|
|
|
#[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 => 'LegacyInstockChangedLogEntry', 10 => 'DatabaseUpdatedLogEntry', 11 => 'CollectionElementDeleted', 12 => 'SecurityEventLogEntry', 13 => 'PartStockChangedLogEntry'])]
|
2023-05-28 01:55:30 +02:00
|
|
|
#[ORM\Index(columns: ['type'], name: 'log_idx_type')]
|
|
|
|
#[ORM\Index(columns: ['type', 'target_type', 'target_id'], name: 'log_idx_type_target')]
|
|
|
|
#[ORM\Index(columns: ['datetime'], name: 'log_idx_datetime')]
|
2020-02-01 19:48:07 +01:00
|
|
|
abstract class AbstractLogEntry extends AbstractDBElement
|
2020-01-24 22:57:04 +01:00
|
|
|
{
|
2023-04-15 21:49:19 +02:00
|
|
|
/** @var User|null The user which has caused this log entry
|
2020-01-24 22:57:04 +01:00
|
|
|
*/
|
2023-06-11 14:55:06 +02:00
|
|
|
#[ORM\ManyToOne(targetEntity: User::class, fetch: 'EAGER')]
|
2023-05-28 01:51:13 +02:00
|
|
|
#[ORM\JoinColumn(name: 'id_user', onDelete: 'SET NULL')]
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?User $user = null;
|
2020-01-24 22:57:04 +01:00
|
|
|
|
2022-12-17 00:25:54 +01:00
|
|
|
/**
|
|
|
|
* @var string The username of the user which has caused this log entry (shown if the user is deleted)
|
|
|
|
*/
|
2023-06-11 14:55:06 +02:00
|
|
|
#[ORM\Column(type: Types::STRING)]
|
2022-12-17 00:25:54 +01:00
|
|
|
protected string $username = '';
|
|
|
|
|
2024-06-22 00:31:43 +02:00
|
|
|
/**
|
|
|
|
* @var \DateTimeInterface The datetime the event associated with this log entry has occured
|
2020-01-24 22:57:04 +01:00
|
|
|
*/
|
2023-06-11 14:55:06 +02:00
|
|
|
#[ORM\Column(name: 'datetime', type: Types::DATETIME_MUTABLE)]
|
2024-06-13 23:41:35 +02:00
|
|
|
protected \DateTime $timestamp;
|
2020-01-24 22:57:04 +01:00
|
|
|
|
2023-06-18 17:25:55 +02:00
|
|
|
/**
|
|
|
|
* @var LogLevel The priority level of the associated level. 0 is highest, 7 lowest
|
2020-01-24 22:57:04 +01:00
|
|
|
*/
|
2023-06-18 17:25:55 +02:00
|
|
|
#[ORM\Column(name: 'level', type: 'tinyint', enumType: LogLevel::class)]
|
|
|
|
protected LogLevel $level = LogLevel::WARNING;
|
2020-01-24 22:57:04 +01:00
|
|
|
|
2020-02-01 16:17:20 +01:00
|
|
|
/** @var int The ID of the element targeted by this event
|
2020-01-24 22:57:04 +01:00
|
|
|
*/
|
2023-06-11 14:55:06 +02:00
|
|
|
#[ORM\Column(name: 'target_id', type: Types::INTEGER)]
|
2022-09-18 22:59:31 +02:00
|
|
|
protected int $target_id = 0;
|
2020-01-24 22:57:04 +01:00
|
|
|
|
2023-06-18 18:31:39 +02:00
|
|
|
/** @var LogTargetType The Type of the targeted element
|
2020-01-24 22:57:04 +01:00
|
|
|
*/
|
2023-06-18 18:31:39 +02:00
|
|
|
#[ORM\Column(name: 'target_type', type: Types::SMALLINT, enumType: LogTargetType::class)]
|
|
|
|
protected LogTargetType $target_type = LogTargetType::NONE;
|
2020-01-24 22:57:04 +01:00
|
|
|
|
|
|
|
/** @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.
|
|
|
|
*/
|
2022-09-18 22:59:31 +02:00
|
|
|
protected string $typeString = 'unknown';
|
2020-01-24 22:57:04 +01:00
|
|
|
|
2020-01-25 22:52:34 +01:00
|
|
|
/** @var array The extra data in raw (short form) saved in the DB
|
|
|
|
*/
|
2023-06-11 14:55:06 +02:00
|
|
|
#[ORM\Column(name: 'extra', type: Types::JSON)]
|
2023-04-15 22:05:29 +02:00
|
|
|
protected array $extra = [];
|
2020-01-25 22:52:34 +01:00
|
|
|
|
2020-01-26 21:31:48 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->timestamp = new DateTime();
|
|
|
|
}
|
|
|
|
|
2020-01-24 22:57:04 +01:00
|
|
|
/**
|
|
|
|
* Get the user that caused the event associated with this log entry.
|
2020-02-01 16:17:20 +01:00
|
|
|
*
|
2020-01-24 22:57:04 +01:00
|
|
|
* @return User
|
|
|
|
*/
|
2020-01-26 13:59:30 +01:00
|
|
|
public function getUser(): ?User
|
2020-01-24 22:57:04 +01:00
|
|
|
{
|
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the user that caused the event.
|
2020-02-01 16:17:20 +01:00
|
|
|
*
|
2020-01-24 22:57:04 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setUser(User $user): self
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
2020-02-01 16:17:20 +01:00
|
|
|
|
2022-12-17 00:25:54 +01:00
|
|
|
//Save the username for later use
|
|
|
|
$this->username = $user->getUsername();
|
|
|
|
|
2020-01-24 22:57:04 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-04-08 01:13:13 +02:00
|
|
|
/**
|
|
|
|
* Returns true if this log entry was created by a CLI command, false otherwise.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isCLIEntry(): bool
|
2023-04-07 22:44:59 +02:00
|
|
|
{
|
2023-05-27 23:58:28 +02:00
|
|
|
return str_starts_with($this->username, '!!!CLI ');
|
2023-04-07 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
2023-04-08 01:13:13 +02:00
|
|
|
/**
|
|
|
|
* Marks this log entry as a CLI entry, and set the username of the CLI user.
|
|
|
|
* This removes the association to a user object in database, as CLI users are not really related to logged in
|
|
|
|
* Part-DB users.
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setCLIUsername(string $cli_username): self
|
2023-04-07 22:44:59 +02:00
|
|
|
{
|
2023-04-08 01:13:13 +02:00
|
|
|
$this->user = null;
|
|
|
|
$this->username = '!!!CLI ' . $cli_username;
|
|
|
|
return $this;
|
2023-04-07 22:44:59 +02:00
|
|
|
}
|
|
|
|
|
2023-04-08 01:13:13 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the username of the CLI user that caused the event.
|
|
|
|
* @return string|null The username of the CLI user, or null if this log entry was not created by a CLI command.
|
|
|
|
*/
|
2023-04-07 22:44:59 +02:00
|
|
|
public function getCLIUsername(): ?string
|
|
|
|
{
|
2023-04-08 01:13:13 +02:00
|
|
|
if ($this->isCLIEntry()) {
|
2023-04-07 22:44:59 +02:00
|
|
|
return substr($this->username, 7);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-12-17 00:25:54 +01:00
|
|
|
/**
|
|
|
|
* Retuns the username of the user that caused the event (useful if the user was deleted).
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getUsername(): string
|
|
|
|
{
|
|
|
|
return $this->username;
|
|
|
|
}
|
|
|
|
|
2020-01-24 22:57:04 +01:00
|
|
|
/**
|
2023-06-18 15:37:42 +02:00
|
|
|
* Returns the timestamp when the event that caused this log entry happened.
|
2020-01-24 22:57:04 +01:00
|
|
|
*/
|
2023-06-18 16:01:28 +02:00
|
|
|
public function getTimestamp(): \DateTimeInterface
|
2020-01-24 22:57:04 +01:00
|
|
|
{
|
|
|
|
return $this->timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the timestamp when the event happened.
|
2020-02-01 16:17:20 +01:00
|
|
|
*
|
2020-01-24 22:57:04 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
2024-06-13 23:41:35 +02:00
|
|
|
public function setTimestamp(\DateTime $timestamp): self
|
2020-01-24 22:57:04 +01:00
|
|
|
{
|
|
|
|
$this->timestamp = $timestamp;
|
2020-02-01 16:17:20 +01:00
|
|
|
|
2020-01-24 22:57:04 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-06-18 17:25:55 +02:00
|
|
|
* Get the priority level of this log entry.
|
2020-01-24 22:57:04 +01:00
|
|
|
*/
|
2023-06-18 17:25:55 +02:00
|
|
|
public function getLevel(): LogLevel
|
2020-01-24 22:57:04 +01:00
|
|
|
{
|
|
|
|
return $this->level;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the new level of this log entry.
|
2020-02-01 16:17:20 +01:00
|
|
|
*
|
2020-01-24 22:57:04 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
2023-06-18 17:25:55 +02:00
|
|
|
public function setLevel(LogLevel $level): self
|
2020-01-24 22:57:04 +01:00
|
|
|
{
|
|
|
|
$this->level = $level;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-01 16:17:20 +01:00
|
|
|
* Get the priority level of this log entry as PSR3 compatible string.
|
2020-01-24 22:57:04 +01:00
|
|
|
*/
|
|
|
|
public function getLevelString(): string
|
|
|
|
{
|
2023-06-18 17:25:55 +02:00
|
|
|
return $this->level->toPSR3LevelString();
|
2020-01-24 22:57:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-01 16:17:20 +01:00
|
|
|
* Sets the priority level of this log entry as PSR3 compatible string.
|
|
|
|
*
|
2020-01-24 22:57:04 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
2020-02-01 16:17:20 +01:00
|
|
|
public function setLevelString(string $level): self
|
2020-01-24 22:57:04 +01:00
|
|
|
{
|
2023-06-18 17:25:55 +02:00
|
|
|
LogLevel::fromPSR3LevelString($level);
|
2020-01-24 22:57:04 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the type of the event this log entry is associated with.
|
|
|
|
*/
|
|
|
|
public function getType(): string
|
|
|
|
{
|
|
|
|
return $this->typeString;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the class name of the target element associated with this log entry.
|
2023-04-15 23:14:53 +02:00
|
|
|
* Returns null, if this log entry is not associated with a log entry.
|
2020-02-01 16:17:20 +01:00
|
|
|
*
|
2020-08-21 21:36:22 +02:00
|
|
|
* @return string|null the class name of the target class
|
2020-01-24 22:57:04 +01:00
|
|
|
*/
|
|
|
|
public function getTargetClass(): ?string
|
|
|
|
{
|
2023-06-18 18:31:39 +02:00
|
|
|
return $this->target_type->toClass();
|
|
|
|
}
|
2020-01-24 22:57:04 +01:00
|
|
|
|
2023-06-18 18:31:39 +02:00
|
|
|
/**
|
|
|
|
* Returns the type of the target element associated with this log entry.
|
|
|
|
* @return LogTargetType
|
|
|
|
*/
|
|
|
|
public function getTargetType(): LogTargetType
|
|
|
|
{
|
|
|
|
return $this->target_type;
|
2020-01-24 22:57:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the ID of the target element associated with this log entry.
|
2023-04-15 23:14:53 +02:00
|
|
|
* Returns null, if this log entry is not associated with a log entry.
|
2020-02-01 16:17:20 +01:00
|
|
|
*
|
2020-08-21 21:36:22 +02:00
|
|
|
* @return int|null the ID of the associated element
|
2020-01-24 22:57:04 +01:00
|
|
|
*/
|
|
|
|
public function getTargetID(): ?int
|
|
|
|
{
|
2020-02-01 16:17:20 +01:00
|
|
|
if (0 === $this->target_id) {
|
2020-01-24 22:57:04 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->target_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-01 16:17:20 +01:00
|
|
|
* Checks if this log entry is associated with an element.
|
|
|
|
*
|
2020-08-21 21:36:22 +02:00
|
|
|
* @return bool true if this log entry is associated with an element, false otherwise
|
2020-01-24 22:57:04 +01:00
|
|
|
*/
|
|
|
|
public function hasTarget(): bool
|
|
|
|
{
|
2020-02-01 16:17:20 +01:00
|
|
|
return null !== $this->getTargetID() && null !== $this->getTargetClass();
|
2020-01-24 22:57:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-01 16:17:20 +01:00
|
|
|
* Sets the target element associated with this element.
|
|
|
|
*
|
2022-08-14 19:39:07 +02:00
|
|
|
* @param AbstractDBElement|null $element the element that should be associated with this element
|
2020-02-01 16:17:20 +01:00
|
|
|
*
|
2020-01-24 22:57:04 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
2020-02-01 19:48:07 +01:00
|
|
|
public function setTargetElement(?AbstractDBElement $element): self
|
2020-01-24 22:57:04 +01:00
|
|
|
{
|
2023-06-18 18:31:39 +02:00
|
|
|
if ($element === null) {
|
2020-01-24 22:57:04 +01:00
|
|
|
$this->target_id = 0;
|
2023-06-18 18:31:39 +02:00
|
|
|
$this->target_type = LogTargetType::NONE;
|
2020-02-01 16:17:20 +01:00
|
|
|
|
2020-01-24 22:57:04 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-06-18 18:31:39 +02:00
|
|
|
$this->target_type = LogTargetType::fromElementClass($element);
|
2020-01-24 22:57:04 +01:00
|
|
|
$this->target_id = $element->getID();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-03-01 19:46:48 +01:00
|
|
|
/**
|
|
|
|
* Sets the target ID of the element associated with this element.
|
2020-03-15 13:56:31 +01:00
|
|
|
*
|
2020-03-01 19:46:48 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setTargetElementID(int $target_id): self
|
|
|
|
{
|
|
|
|
$this->target_id = $target_id;
|
2020-03-15 13:56:31 +01:00
|
|
|
|
2020-03-01 19:46:48 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-01-25 22:52:34 +01:00
|
|
|
public function getExtraData(): array
|
|
|
|
{
|
|
|
|
return $this->extra;
|
|
|
|
}
|
|
|
|
|
2020-02-01 16:17:20 +01:00
|
|
|
}
|