Report.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. protected IUserManager $userManager;
  40. private IConfig $config;
  41. public function __construct(IUserManager $userManager,
  42. IConfig $config) {
  43. $this->userManager = $userManager;
  44. $this->config = $config;
  45. parent::__construct();
  46. }
  47. protected function configure(): void {
  48. $this
  49. ->setName('user:report')
  50. ->setDescription('shows how many users have access')
  51. ->addOption(
  52. 'count-dirs',
  53. null,
  54. InputOption::VALUE_NONE,
  55. '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)'
  56. )
  57. ;
  58. }
  59. protected function execute(InputInterface $input, OutputInterface $output): int {
  60. $table = new Table($output);
  61. $table->setHeaders(['User Report', '']);
  62. $userCountArray = $this->countUsers();
  63. $total = 0;
  64. if (!empty($userCountArray)) {
  65. $rows = [];
  66. foreach ($userCountArray as $classname => $users) {
  67. $total += $users;
  68. $rows[] = [$classname, $users];
  69. }
  70. $rows[] = [' '];
  71. $rows[] = ['total users', $total];
  72. } else {
  73. $rows[] = ['No backend enabled that supports user counting', ''];
  74. }
  75. $rows[] = [' '];
  76. if ($total <= self::DEFAULT_COUNT_DIRS_MAX_USERS || $input->getOption('count-dirs')) {
  77. $userDirectoryCount = $this->countUserDirectories();
  78. $rows[] = ['user directories', $userDirectoryCount];
  79. }
  80. $activeUsers = $this->userManager->countSeenUsers();
  81. $rows[] = ['active users', $activeUsers];
  82. $disabledUsers = $this->config->getUsersForUserValue('core', 'enabled', 'false');
  83. $disabledUsersCount = count($disabledUsers);
  84. $rows[] = ['disabled users', $disabledUsersCount];
  85. $table->setRows($rows);
  86. $table->render();
  87. return 0;
  88. }
  89. private function countUsers(): array {
  90. return $this->userManager->countUsers();
  91. }
  92. private function countUserDirectories(): int {
  93. $dataview = new View('/');
  94. $userDirectories = $dataview->getDirectoryContent('/', 'httpd/unix-directory');
  95. return count($userDirectories);
  96. }
  97. }