Report.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\Core\Command\User;
  29. use OC\Files\View;
  30. use OCP\IConfig;
  31. use OCP\IUserManager;
  32. use Symfony\Component\Console\Command\Command;
  33. use Symfony\Component\Console\Helper\Table;
  34. use Symfony\Component\Console\Input\InputInterface;
  35. use Symfony\Component\Console\Input\InputOption;
  36. use Symfony\Component\Console\Output\OutputInterface;
  37. class Report extends Command {
  38. public const DEFAULT_COUNT_DIRS_MAX_USERS = 500;
  39. public function __construct(
  40. protected IUserManager $userManager,
  41. private IConfig $config,
  42. ) {
  43. parent::__construct();
  44. }
  45. protected function configure(): void {
  46. $this
  47. ->setName('user:report')
  48. ->setDescription('shows how many users have access')
  49. ->addOption(
  50. 'count-dirs',
  51. null,
  52. InputOption::VALUE_NONE,
  53. '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)'
  54. )
  55. ;
  56. }
  57. protected function execute(InputInterface $input, OutputInterface $output): int {
  58. $table = new Table($output);
  59. $table->setHeaders(['Account Report', '']);
  60. $userCountArray = $this->countUsers();
  61. $total = 0;
  62. if (!empty($userCountArray)) {
  63. $rows = [];
  64. foreach ($userCountArray as $classname => $users) {
  65. $total += $users;
  66. $rows[] = [$classname, $users];
  67. }
  68. $rows[] = [' '];
  69. $rows[] = ['total users', $total];
  70. } else {
  71. $rows[] = ['No backend enabled that supports user counting', ''];
  72. }
  73. $rows[] = [' '];
  74. if ($total <= self::DEFAULT_COUNT_DIRS_MAX_USERS || $input->getOption('count-dirs')) {
  75. $userDirectoryCount = $this->countUserDirectories();
  76. $rows[] = ['user directories', $userDirectoryCount];
  77. }
  78. $activeUsers = $this->userManager->countSeenUsers();
  79. $rows[] = ['active users', $activeUsers];
  80. $disabledUsers = $this->config->getUsersForUserValue('core', 'enabled', 'false');
  81. $disabledUsersCount = count($disabledUsers);
  82. $rows[] = ['disabled users', $disabledUsersCount];
  83. $table->setRows($rows);
  84. $table->render();
  85. return 0;
  86. }
  87. private function countUsers(): array {
  88. return $this->userManager->countUsers();
  89. }
  90. private function countUserDirectories(): int {
  91. $dataview = new View('/');
  92. $userDirectories = $dataview->getDirectoryContent('/', 'httpd/unix-directory');
  93. return count($userDirectories);
  94. }
  95. }