diff --git a/src/Command/Logs/ShowEventLogCommand.php b/src/Command/Logs/ShowEventLogCommand.php index d3ac5c7f..517c582f 100644 --- a/src/Command/Logs/ShowEventLogCommand.php +++ b/src/Command/Logs/ShowEventLogCommand.php @@ -147,11 +147,21 @@ class ShowEventLogCommand extends Command $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 = [ $entry->getID(), $entry->getTimestamp()->format('Y-m-d H:i:s'), $entry->getType(), - $entry->getUser()->getFullName(true), + $user, $target_class, $target_name, ]; diff --git a/src/DataTables/LogDataTable.php b/src/DataTables/LogDataTable.php index 5d23446a..0833021c 100644 --- a/src/DataTables/LogDataTable.php +++ b/src/DataTables/LogDataTable.php @@ -226,6 +226,14 @@ class LogDataTable implements DataTableTypeInterface //If user was deleted, show the info from the username field 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( '@%s [%s]', htmlentities($context->getUsername()), diff --git a/src/Entity/LogSystem/AbstractLogEntry.php b/src/Entity/LogSystem/AbstractLogEntry.php index e2dca513..460e561a 100644 --- a/src/Entity/LogSystem/AbstractLogEntry.php +++ b/src/Entity/LogSystem/AbstractLogEntry.php @@ -216,6 +216,26 @@ abstract class AbstractLogEntry extends AbstractDBElement 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). * diff --git a/src/Services/LogSystem/EventLogger.php b/src/Services/LogSystem/EventLogger.php index 80ee067e..9b4349b5 100644 --- a/src/Services/LogSystem/EventLogger.php +++ b/src/Services/LogSystem/EventLogger.php @@ -24,6 +24,7 @@ namespace App\Services\LogSystem; use App\Entity\LogSystem\AbstractLogEntry; use App\Entity\UserSystem\User; +use App\Services\Misc\ConsoleInfoHelper; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\Security\Core\Security; @@ -34,14 +35,17 @@ class EventLogger protected array $whitelist; protected EntityManagerInterface $em; 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->blacklist = $blacklist; $this->whitelist = $whitelist; $this->em = $em; $this->security = $security; + $this->console_info_helper = $console_info_helper; } /** @@ -67,6 +71,11 @@ class EventLogger $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)) { $this->em->persist($logEntry); diff --git a/src/Services/Misc/ConsoleInfoHelper.php b/src/Services/Misc/ConsoleInfoHelper.php new file mode 100644 index 00000000..8aea004e --- /dev/null +++ b/src/Services/Misc/ConsoleInfoHelper.php @@ -0,0 +1,63 @@ +. + */ + +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; + } +} \ No newline at end of file diff --git a/translations/messages.en.xlf b/translations/messages.en.xlf index e2cb67fb..eeee86b6 100644 --- a/translations/messages.en.xlf +++ b/translations/messages.en.xlf @@ -11259,5 +11259,17 @@ Element 3 Less than desired + + + log.cli_user + CLI user + + + + + log.element_edited.changed_fields.part_owner_must_match + Part owner must match storage location owner + +