Improved naming and documentation of CLIUser functions on AbstractLogEntry

This commit is contained in:
Jan Böhmer 2023-04-08 01:13:13 +02:00
parent c060d6ebb1
commit d258235430
5 changed files with 28 additions and 13 deletions

View file

@ -150,7 +150,7 @@ class ShowEventLogCommand extends Command
if ($entry->getUser()) { if ($entry->getUser()) {
$user = $entry->getUser()->getFullName(true); $user = $entry->getUser()->getFullName(true);
} else { } else {
if ($entry->isCLIUser()) { if ($entry->isCLIEntry()) {
$user = $entry->getCLIUsername() . ' [CLI]'; $user = $entry->getCLIUsername() . ' [CLI]';
} else { } else {
$user = $entry->getUsername() . ' [deleted]'; $user = $entry->getUsername() . ' [deleted]';

View file

@ -226,7 +226,7 @@ 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()) { if ($context->isCLIEntry()) {
return sprintf('%s [%s]', return sprintf('%s [%s]',
htmlentities($context->getCLIUsername()), htmlentities($context->getCLIUsername()),
$this->translator->trans('log.cli_user') $this->translator->trans('log.cli_user')

View file

@ -216,21 +216,36 @@ abstract class AbstractLogEntry extends AbstractDBElement
return $this; 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->user = null;
$this->username = '!!!CLI ' . $cli_username; $this->username = '!!!CLI ' . $cli_username;
return $this; return $this;
} }
public function isCLIUser(): bool /**
{ * Retrieves the username of the CLI user that caused the event.
return strpos($this->username, '!!!CLI ') === 0; * @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 public function getCLIUsername(): ?string
{ {
if ($this->isCLIUser()) { if ($this->isCLIEntry()) {
return substr($this->username, 7); return substr($this->username, 7);
} }
return null; return null;

View file

@ -73,7 +73,7 @@ class EventLogger
//Set the console user info, if the log entry was created in a console command //Set the console user info, if the log entry was created in a console command
if ($this->console_info_helper->isCLI()) { 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)) { if ($this->shouldBeAdded($logEntry)) {

View file

@ -168,16 +168,16 @@ class AbstractLogEntryTest extends TestCase
//By default no no CLI username is set //By default no no CLI username is set
$this->assertNull($log->getCLIUsername()); $this->assertNull($log->getCLIUsername());
$this->assertFalse($log->isCLIUser()); $this->assertFalse($log->isCLIEntry());
$user = new User(); $user = new User();
$user->setName('test'); $user->setName('test');
$log->setUser($user); $log->setUser($user);
//Set a CLI username //Set a CLI username
$log->setCLIUser('root'); $log->setCLIUsername('root');
$this->assertSame('root', $log->getCLIUsername()); $this->assertSame('root', $log->getCLIUsername());
$this->assertTrue($log->isCLIUser()); $this->assertTrue($log->isCLIEntry());
//Normal user must be null now //Normal user must be null now
$this->assertNull($log->getUser()); $this->assertNull($log->getUser());