diff --git a/src/Command/Logs/ShowEventLogCommand.php b/src/Command/Logs/ShowEventLogCommand.php index 517c582f..7eef7a2d 100644 --- a/src/Command/Logs/ShowEventLogCommand.php +++ b/src/Command/Logs/ShowEventLogCommand.php @@ -150,7 +150,7 @@ class ShowEventLogCommand extends Command if ($entry->getUser()) { $user = $entry->getUser()->getFullName(true); } else { - if ($entry->isCLIUser()) { + if ($entry->isCLIEntry()) { $user = $entry->getCLIUsername() . ' [CLI]'; } else { $user = $entry->getUsername() . ' [deleted]'; diff --git a/src/DataTables/LogDataTable.php b/src/DataTables/LogDataTable.php index 0833021c..85ab3113 100644 --- a/src/DataTables/LogDataTable.php +++ b/src/DataTables/LogDataTable.php @@ -226,7 +226,7 @@ class LogDataTable implements DataTableTypeInterface //If user was deleted, show the info from the username field if ($user === null) { - if ($context->isCLIUser()) { + if ($context->isCLIEntry()) { return sprintf('%s [%s]', htmlentities($context->getCLIUsername()), $this->translator->trans('log.cli_user') diff --git a/src/Entity/LogSystem/AbstractLogEntry.php b/src/Entity/LogSystem/AbstractLogEntry.php index 460e561a..5b6a728b 100644 --- a/src/Entity/LogSystem/AbstractLogEntry.php +++ b/src/Entity/LogSystem/AbstractLogEntry.php @@ -216,21 +216,36 @@ abstract class AbstractLogEntry extends AbstractDBElement return $this; } - public function setCLIUser(?string $cli_username): self + /** + * Returns true if this log entry was created by a CLI command, false otherwise. + * @return bool + */ + public function isCLIEntry(): bool + { + return strpos($this->username, '!!!CLI ') === 0; + } + + /** + * 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. + * @param string $cli_username + * @return $this + */ + public function setCLIUsername(string $cli_username): self { $this->user = null; $this->username = '!!!CLI ' . $cli_username; return $this; } - public function isCLIUser(): bool - { - return strpos($this->username, '!!!CLI ') === 0; - } - + /** + * 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. + */ public function getCLIUsername(): ?string { - if ($this->isCLIUser()) { + if ($this->isCLIEntry()) { return substr($this->username, 7); } return null; diff --git a/src/Services/LogSystem/EventLogger.php b/src/Services/LogSystem/EventLogger.php index 9b4349b5..8155819b 100644 --- a/src/Services/LogSystem/EventLogger.php +++ b/src/Services/LogSystem/EventLogger.php @@ -73,7 +73,7 @@ class EventLogger //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'); + $logEntry->setCLIUsername($this->console_info_helper->getCLIUser() ?? 'Unknown'); } if ($this->shouldBeAdded($logEntry)) { diff --git a/tests/Entity/LogSystem/AbstractLogEntryTest.php b/tests/Entity/LogSystem/AbstractLogEntryTest.php index 0fde2daf..243895aa 100644 --- a/tests/Entity/LogSystem/AbstractLogEntryTest.php +++ b/tests/Entity/LogSystem/AbstractLogEntryTest.php @@ -168,16 +168,16 @@ class AbstractLogEntryTest extends TestCase //By default no no CLI username is set $this->assertNull($log->getCLIUsername()); - $this->assertFalse($log->isCLIUser()); + $this->assertFalse($log->isCLIEntry()); $user = new User(); $user->setName('test'); $log->setUser($user); //Set a CLI username - $log->setCLIUser('root'); + $log->setCLIUsername('root'); $this->assertSame('root', $log->getCLIUsername()); - $this->assertTrue($log->isCLIUser()); + $this->assertTrue($log->isCLIEntry()); //Normal user must be null now $this->assertNull($log->getUser());