2014-01-07 17:06:37 -05:00
< ? php
2021-05-25 07:03:29 -04:00
declare ( strict_types = 1 );
2021-06-04 15:52:51 -04:00
2014-01-07 17:06:37 -05:00
/**
2024-05-24 13:43:47 -04:00
* SPDX - FileCopyrightText : 2016 - 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX - FileCopyrightText : 2016 ownCloud , Inc .
* SPDX - License - Identifier : AGPL - 3.0 - only
2014-01-07 17:06:37 -05:00
*/
namespace OC\Core\Command\User ;
2021-05-25 07:03:29 -04:00
use OC\Files\View ;
2020-08-19 03:31:41 -04:00
use OCP\IConfig ;
2015-04-23 06:40:13 -04:00
use OCP\IUserManager ;
2014-01-07 17:06:37 -05:00
use Symfony\Component\Console\Command\Command ;
2016-09-06 14:35:10 -04:00
use Symfony\Component\Console\Helper\Table ;
2014-01-07 17:06:37 -05:00
use Symfony\Component\Console\Input\InputInterface ;
2021-05-25 07:03:29 -04:00
use Symfony\Component\Console\Input\InputOption ;
2014-01-07 17:06:37 -05:00
use Symfony\Component\Console\Output\OutputInterface ;
class Report extends Command {
2021-05-25 07:03:29 -04:00
public const DEFAULT_COUNT_DIRS_MAX_USERS = 500 ;
2023-06-12 10:41:17 -04:00
public function __construct (
protected IUserManager $userManager ,
private IConfig $config ,
) {
2015-04-23 06:40:13 -04:00
parent :: __construct ();
}
2021-05-25 07:03:29 -04:00
protected function configure () : void {
2014-01-07 17:06:37 -05:00
$this
-> setName ( 'user:report' )
2021-05-25 07:03:29 -04:00
-> setDescription ( 'shows how many users have access' )
-> addOption (
'count-dirs' ,
null ,
InputOption :: VALUE_NONE ,
'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)'
)
;
2014-01-07 17:06:37 -05:00
}
2020-06-26 08:54:51 -04:00
protected function execute ( InputInterface $input , OutputInterface $output ) : int {
2016-09-06 14:35:10 -04:00
$table = new Table ( $output );
2022-09-21 11:44:32 -04:00
$table -> setHeaders ([ 'Account Report' , '' ]);
2014-01-07 17:06:37 -05:00
$userCountArray = $this -> countUsers ();
2021-05-25 07:03:29 -04:00
$total = 0 ;
2020-04-10 08:19:56 -04:00
if ( ! empty ( $userCountArray )) {
2020-03-26 04:30:18 -04:00
$rows = [];
2020-04-10 08:19:56 -04:00
foreach ( $userCountArray as $classname => $users ) {
2014-01-07 17:06:37 -05:00
$total += $users ;
2020-03-26 04:30:18 -04:00
$rows [] = [ $classname , $users ];
2014-01-07 17:06:37 -05:00
}
2020-03-26 04:30:18 -04:00
$rows [] = [ ' ' ];
$rows [] = [ 'total users' , $total ];
2014-01-07 17:06:37 -05:00
} else {
2020-03-26 04:30:18 -04:00
$rows [] = [ 'No backend enabled that supports user counting' , '' ];
2014-01-07 17:06:37 -05:00
}
2020-03-26 04:30:18 -04:00
$rows [] = [ ' ' ];
2021-05-25 07:03:29 -04:00
if ( $total <= self :: DEFAULT_COUNT_DIRS_MAX_USERS || $input -> getOption ( 'count-dirs' )) {
$userDirectoryCount = $this -> countUserDirectories ();
$rows [] = [ 'user directories' , $userDirectoryCount ];
}
2014-01-07 17:06:37 -05:00
2021-08-19 10:22:20 -04:00
$activeUsers = $this -> userManager -> countSeenUsers ();
$rows [] = [ 'active users' , $activeUsers ];
2020-08-19 03:31:41 -04:00
$disabledUsers = $this -> config -> getUsersForUserValue ( 'core' , 'enabled' , 'false' );
$disabledUsersCount = count ( $disabledUsers );
$rows [] = [ 'disabled users' , $disabledUsersCount ];
2014-01-07 17:06:37 -05:00
$table -> setRows ( $rows );
2016-09-06 14:35:10 -04:00
$table -> render ();
2020-06-26 08:54:51 -04:00
return 0 ;
2014-01-07 17:06:37 -05:00
}
2021-05-25 07:03:29 -04:00
private function countUsers () : array {
2015-04-23 06:40:13 -04:00
return $this -> userManager -> countUsers ();
2014-01-07 17:06:37 -05:00
}
2021-05-25 07:03:29 -04:00
private function countUserDirectories () : int {
$dataview = new View ( '/' );
2014-01-07 17:06:37 -05:00
$userDirectories = $dataview -> getDirectoryContent ( '/' , 'httpd/unix-directory' );
return count ( $userDirectories );
}
2014-02-06 05:34:27 -05:00
}