Report.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OC\Core\Command\User;
  9. use OC\Files\View;
  10. use OCP\IConfig;
  11. use OCP\IUserManager;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Helper\Table;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. class Report extends Command {
  18. public const DEFAULT_COUNT_DIRS_MAX_USERS = 500;
  19. public function __construct(
  20. protected IUserManager $userManager,
  21. private IConfig $config,
  22. ) {
  23. parent::__construct();
  24. }
  25. protected function configure(): void {
  26. $this
  27. ->setName('user:report')
  28. ->setDescription('shows how many users have access')
  29. ->addOption(
  30. 'count-dirs',
  31. null,
  32. InputOption::VALUE_NONE,
  33. 'Also count the number of user directories in the database (could time out on huge installations, therefore defaults to no with ' . self::DEFAULT_COUNT_DIRS_MAX_USERS . '+ users)'
  34. )
  35. ;
  36. }
  37. protected function execute(InputInterface $input, OutputInterface $output): int {
  38. $table = new Table($output);
  39. $table->setHeaders(['Account Report', '']);
  40. $userCountArray = $this->countUsers();
  41. $total = 0;
  42. if (!empty($userCountArray)) {
  43. $rows = [];
  44. foreach ($userCountArray as $classname => $users) {
  45. $total += $users;
  46. $rows[] = [$classname, $users];
  47. }
  48. $rows[] = [' '];
  49. $rows[] = ['total users', $total];
  50. } else {
  51. $rows[] = ['No backend enabled that supports user counting', ''];
  52. }
  53. $rows[] = [' '];
  54. if ($total <= self::DEFAULT_COUNT_DIRS_MAX_USERS || $input->getOption('count-dirs')) {
  55. $userDirectoryCount = $this->countUserDirectories();
  56. $rows[] = ['user directories', $userDirectoryCount];
  57. }
  58. $activeUsers = $this->userManager->countSeenUsers();
  59. $rows[] = ['active users', $activeUsers];
  60. $disabledUsers = $this->config->getUsersForUserValue('core', 'enabled', 'false');
  61. $disabledUsersCount = count($disabledUsers);
  62. $rows[] = ['disabled users', $disabledUsersCount];
  63. $table->setRows($rows);
  64. $table->render();
  65. return 0;
  66. }
  67. private function countUsers(): array {
  68. return $this->userManager->countUsers();
  69. }
  70. private function countUserDirectories(): int {
  71. $dataview = new View('/');
  72. $userDirectories = $dataview->getDirectoryContent('/', 'httpd/unix-directory');
  73. return count($userDirectories);
  74. }
  75. }