diff --git a/core/Command/User/LastSeen.php b/core/Command/User/LastSeen.php index 514396f9f5283..8c58f17940ae3 100644 --- a/core/Command/User/LastSeen.php +++ b/core/Command/User/LastSeen.php @@ -38,6 +38,12 @@ protected function configure(): void { InputOption::VALUE_NONE, 'shows a list of when all users were last logged in' ) + ->addOption( + 'exclude-disabled', + null, + InputOption::VALUE_NONE, + 'exclude disabled users from the list (only works with --all)' + ) ; } @@ -68,7 +74,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 1; } - $this->userManager->callForAllUsers(static function (IUser $user) use ($output): void { + $excludeDisabled = $input->getOption('exclude-disabled'); + $this->userManager->callForAllUsers(static function (IUser $user) use ($output, $excludeDisabled): void { + if ($excludeDisabled && !$user->isEnabled()) { + return; + } $lastLogin = $user->getLastLogin(); if ($lastLogin === 0) { $output->writeln($user->getUID() . ' has never logged in.'); diff --git a/tests/Core/Command/User/LastSeenTest.php b/tests/Core/Command/User/LastSeenTest.php index 64c710eacc59f..6afcf40c8033e 100644 --- a/tests/Core/Command/User/LastSeenTest.php +++ b/tests/Core/Command/User/LastSeenTest.php @@ -92,4 +92,98 @@ public function testInvalidUser(): void { self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); } + + public function testAllUsersWithoutExcludeDisabled(): void { + $enabledUser = $this->getMockBuilder(IUser::class)->getMock(); + $enabledUser->expects($this->once()) + ->method('getLastLogin') + ->willReturn(time()); + $enabledUser->expects($this->once()) + ->method('getUID') + ->willReturn('enabled_user'); + $enabledUser->expects($this->never()) + ->method('isEnabled'); + + $disabledUser = $this->getMockBuilder(IUser::class)->getMock(); + $disabledUser->expects($this->once()) + ->method('getLastLogin') + ->willReturn(time()); + $disabledUser->expects($this->once()) + ->method('getUID') + ->willReturn('disabled_user'); + $disabledUser->expects($this->never()) + ->method('isEnabled'); + + $this->consoleInput->expects($this->once()) + ->method('getArgument') + ->with('uid') + ->willReturn(null); + + $this->consoleInput->expects($this->exactly(2)) + ->method('getOption') + ->willReturnMap([ + ['all', true], + ['exclude-disabled', false], + ]); + + $this->userManager->expects($this->once()) + ->method('callForAllUsers') + ->willReturnCallback(function ($callback) use ($enabledUser, $disabledUser) { + $callback($enabledUser); + $callback($disabledUser); + }); + + $this->consoleOutput->expects($this->exactly(2)) + ->method('writeln') + ->with($this->stringContains("'s last login:")); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + + public function testAllUsersWithExcludeDisabled(): void { + $enabledUser = $this->getMockBuilder(IUser::class)->getMock(); + $enabledUser->expects($this->once()) + ->method('getLastLogin') + ->willReturn(time()); + $enabledUser->expects($this->once()) + ->method('getUID') + ->willReturn('enabled_user'); + $enabledUser->expects($this->once()) + ->method('isEnabled') + ->willReturn(true); + + $disabledUser = $this->getMockBuilder(IUser::class)->getMock(); + $disabledUser->expects($this->never()) + ->method('getLastLogin'); + $disabledUser->expects($this->never()) + ->method('getUID'); + $disabledUser->expects($this->once()) + ->method('isEnabled') + ->willReturn(false); + + $this->consoleInput->expects($this->once()) + ->method('getArgument') + ->with('uid') + ->willReturn(null); + + $this->consoleInput->expects($this->exactly(2)) + ->method('getOption') + ->willReturnMap([ + ['all', true], + ['exclude-disabled', true], + ]); + + $this->userManager->expects($this->once()) + ->method('callForAllUsers') + ->willReturnCallback(function ($callback) use ($enabledUser, $disabledUser) { + $callback($enabledUser); + $callback($disabledUser); + }); + + $this->consoleOutput->expects($this->once()) + ->method('writeln') + ->with($this->stringContains("enabled_user's last login:")); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } }