mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 01:25:55 +02:00
Log the name of the CLI user, when actions were done from the CLI.
This commit is contained in:
parent
c91a6640ff
commit
6443d8e2bf
6 changed files with 124 additions and 2 deletions
|
@ -147,11 +147,21 @@ class ShowEventLogCommand extends Command
|
||||||
$target_class = $this->elementTypeNameGenerator->getLocalizedTypeLabel($entry->getTargetClass());
|
$target_class = $this->elementTypeNameGenerator->getLocalizedTypeLabel($entry->getTargetClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($entry->getUser()) {
|
||||||
|
$user = $entry->getUser()->getFullName(true);
|
||||||
|
} else {
|
||||||
|
if ($entry->isCLIUser()) {
|
||||||
|
$user = $entry->getCLIUsername() . ' [CLI]';
|
||||||
|
} else {
|
||||||
|
$user = $entry->getUsername() . ' [deleted]';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$row = [
|
$row = [
|
||||||
$entry->getID(),
|
$entry->getID(),
|
||||||
$entry->getTimestamp()->format('Y-m-d H:i:s'),
|
$entry->getTimestamp()->format('Y-m-d H:i:s'),
|
||||||
$entry->getType(),
|
$entry->getType(),
|
||||||
$entry->getUser()->getFullName(true),
|
$user,
|
||||||
$target_class,
|
$target_class,
|
||||||
$target_name,
|
$target_name,
|
||||||
];
|
];
|
||||||
|
|
|
@ -226,6 +226,14 @@ class LogDataTable implements DataTableTypeInterface
|
||||||
|
|
||||||
//If user was deleted, show the info from the username field
|
//If user was deleted, show the info from the username field
|
||||||
if ($user === null) {
|
if ($user === null) {
|
||||||
|
if ($context->isCLIUser()) {
|
||||||
|
return sprintf('%s [%s]',
|
||||||
|
htmlentities($context->getCLIUsername()),
|
||||||
|
$this->translator->trans('log.cli_user')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Else we just deal with a deleted user
|
||||||
return sprintf(
|
return sprintf(
|
||||||
'@%s [%s]',
|
'@%s [%s]',
|
||||||
htmlentities($context->getUsername()),
|
htmlentities($context->getUsername()),
|
||||||
|
|
|
@ -216,6 +216,26 @@ abstract class AbstractLogEntry extends AbstractDBElement
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setCLIUser(?string $cli_username): self
|
||||||
|
{
|
||||||
|
$this->user = null;
|
||||||
|
$this->username = '!!!CLI ' . $cli_username;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isCLIUser(): bool
|
||||||
|
{
|
||||||
|
return strpos($this->username, '!!!CLI ') === 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCLIUsername(): ?string
|
||||||
|
{
|
||||||
|
if ($this->isCLIUser()) {
|
||||||
|
return substr($this->username, 7);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retuns the username of the user that caused the event (useful if the user was deleted).
|
* Retuns the username of the user that caused the event (useful if the user was deleted).
|
||||||
*
|
*
|
||||||
|
|
|
@ -24,6 +24,7 @@ namespace App\Services\LogSystem;
|
||||||
|
|
||||||
use App\Entity\LogSystem\AbstractLogEntry;
|
use App\Entity\LogSystem\AbstractLogEntry;
|
||||||
use App\Entity\UserSystem\User;
|
use App\Entity\UserSystem\User;
|
||||||
|
use App\Services\Misc\ConsoleInfoHelper;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Component\Security\Core\Security;
|
use Symfony\Component\Security\Core\Security;
|
||||||
|
|
||||||
|
@ -34,14 +35,17 @@ class EventLogger
|
||||||
protected array $whitelist;
|
protected array $whitelist;
|
||||||
protected EntityManagerInterface $em;
|
protected EntityManagerInterface $em;
|
||||||
protected Security $security;
|
protected Security $security;
|
||||||
|
protected ConsoleInfoHelper $console_info_helper;
|
||||||
|
|
||||||
public function __construct(int $minimum_log_level, array $blacklist, array $whitelist, EntityManagerInterface $em, Security $security)
|
public function __construct(int $minimum_log_level, array $blacklist, array $whitelist, EntityManagerInterface $em,
|
||||||
|
Security $security, ConsoleInfoHelper $console_info_helper)
|
||||||
{
|
{
|
||||||
$this->minimum_log_level = $minimum_log_level;
|
$this->minimum_log_level = $minimum_log_level;
|
||||||
$this->blacklist = $blacklist;
|
$this->blacklist = $blacklist;
|
||||||
$this->whitelist = $whitelist;
|
$this->whitelist = $whitelist;
|
||||||
$this->em = $em;
|
$this->em = $em;
|
||||||
$this->security = $security;
|
$this->security = $security;
|
||||||
|
$this->console_info_helper = $console_info_helper;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -67,6 +71,11 @@ class EventLogger
|
||||||
$logEntry->setUser($user);
|
$logEntry->setUser($user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Set the console user info, if the log entry was created in a console command
|
||||||
|
if ($this->console_info_helper->isCLI()) {
|
||||||
|
$logEntry->setCLIUser($this->console_info_helper->getCLIUser() ?? 'Unknown');
|
||||||
|
}
|
||||||
|
|
||||||
if ($this->shouldBeAdded($logEntry)) {
|
if ($this->shouldBeAdded($logEntry)) {
|
||||||
$this->em->persist($logEntry);
|
$this->em->persist($logEntry);
|
||||||
|
|
||||||
|
|
63
src/Services/Misc/ConsoleInfoHelper.php
Normal file
63
src/Services/Misc/ConsoleInfoHelper.php
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published
|
||||||
|
* by the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Services\Misc;
|
||||||
|
|
||||||
|
class ConsoleInfoHelper
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Returns true if the current script is executed in a CLI environment.
|
||||||
|
* @return bool true if the current script is executed in a CLI environment, false otherwise
|
||||||
|
*/
|
||||||
|
public function isCLI(): bool
|
||||||
|
{
|
||||||
|
return \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the username of the user who started the current script if possible.
|
||||||
|
* @return string|null the username of the user who started the current script if possible, null otherwise
|
||||||
|
*/
|
||||||
|
public function getCLIUser(): ?string
|
||||||
|
{
|
||||||
|
if (!$this->isCLI()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Try to use the posix extension if available (Linux)
|
||||||
|
if (function_exists('posix_getpwuid') && function_exists('posix_geteuid')) {
|
||||||
|
$user = posix_getpwuid(posix_geteuid());
|
||||||
|
return $user['name'];
|
||||||
|
}
|
||||||
|
|
||||||
|
//Try to retrieve the name via the environment variable Username (Windows)
|
||||||
|
if (isset($_SERVER['USERNAME'])) {
|
||||||
|
return $_SERVER['USERNAME'];
|
||||||
|
}
|
||||||
|
|
||||||
|
//Try to retrieve the name via the environment variable USER (Linux)
|
||||||
|
if (isset($_SERVER['USER'])) {
|
||||||
|
return $_SERVER['USER'];
|
||||||
|
}
|
||||||
|
|
||||||
|
//Otherwise we can't determine the username
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -11259,5 +11259,17 @@ Element 3</target>
|
||||||
<target>Less than desired</target>
|
<target>Less than desired</target>
|
||||||
</segment>
|
</segment>
|
||||||
</unit>
|
</unit>
|
||||||
|
<unit id="cdnsW4q" name="log.cli_user">
|
||||||
|
<segment>
|
||||||
|
<source>log.cli_user</source>
|
||||||
|
<target>CLI user</target>
|
||||||
|
</segment>
|
||||||
|
</unit>
|
||||||
|
<unit id="4GTAJ9E" name="log.element_edited.changed_fields.part_owner_must_match">
|
||||||
|
<segment>
|
||||||
|
<source>log.element_edited.changed_fields.part_owner_must_match</source>
|
||||||
|
<target>Part owner must match storage location owner</target>
|
||||||
|
</segment>
|
||||||
|
</unit>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue