From: Jérémy Benoist Date: Wed, 2 Aug 2017 05:25:02 +0000 (+0200) Subject: Merge pull request #3301 from nclsHart/list-user-command X-Git-Tag: 2.3.0~31^2~28 X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=8b5bef48d5e5609c0c89a99f34fe56919125fa68;hp=882da5c5eb283cbc7a869182b26a69b8fbebda2b;p=github%2Fwallabag%2Fwallabag.git Merge pull request #3301 from nclsHart/list-user-command Add list users command --- diff --git a/src/Wallabag/CoreBundle/Command/ListUserCommand.php b/src/Wallabag/CoreBundle/Command/ListUserCommand.php new file mode 100644 index 00000000..20660d18 --- /dev/null +++ b/src/Wallabag/CoreBundle/Command/ListUserCommand.php @@ -0,0 +1,61 @@ +setName('wallabag:user:list') + ->setDescription('List all users') + ->setHelp('This command list all existing users') + ->addArgument('search', InputArgument::OPTIONAL, 'Filter list by given search term') + ->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Max number of displayed users', 100) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $io = new SymfonyStyle($input, $output); + + $users = $this->getContainer()->get('wallabag_user.user_repository') + ->getQueryBuilderForSearch($input->getArgument('search')) + ->setMaxResults($input->getOption('limit')) + ->getQuery() + ->getResult(); + + $nbUsers = $this->getContainer()->get('wallabag_user.user_repository') + ->getSumUsers(); + + $rows = []; + foreach ($users as $user) { + $rows[] = [ + $user->getUsername(), + $user->getEmail(), + $user->isEnabled() ? 'yes' : 'no', + $user->hasRole('ROLE_SUPER_ADMIN') || $user->hasRole('ROLE_ADMIN') ? 'yes' : 'no', + ]; + } + + $io->table(['username', 'email', 'is enabled?', 'is admin?'], $rows); + + $io->success( + sprintf( + '%s/%s%s user(s) displayed.', + count($users), + $nbUsers, + $input->getArgument('search') === null ? '' : ' (filtered)' + ) + ); + + return 0; + } +} diff --git a/src/Wallabag/UserBundle/Repository/UserRepository.php b/src/Wallabag/UserBundle/Repository/UserRepository.php index 75cbeef2..be693d3b 100644 --- a/src/Wallabag/UserBundle/Repository/UserRepository.php +++ b/src/Wallabag/UserBundle/Repository/UserRepository.php @@ -55,6 +55,19 @@ class UserRepository extends EntityRepository ->getSingleScalarResult(); } + /** + * Count how many users are existing. + * + * @return int + */ + public function getSumUsers() + { + return $this->createQueryBuilder('u') + ->select('count(u)') + ->getQuery() + ->getSingleScalarResult(); + } + /** * Retrieves users filtered with a search term. * diff --git a/tests/Wallabag/CoreBundle/Command/ListUserCommandTest.php b/tests/Wallabag/CoreBundle/Command/ListUserCommandTest.php new file mode 100644 index 00000000..9068cf59 --- /dev/null +++ b/tests/Wallabag/CoreBundle/Command/ListUserCommandTest.php @@ -0,0 +1,75 @@ +getClient()->getKernel()); + $application->add(new ListUserCommand()); + + $command = $application->find('wallabag:user:list'); + + $tester = new CommandTester($command); + $tester->execute([ + 'command' => $command->getName(), + ]); + + $this->assertContains('3/3 user(s) displayed.', $tester->getDisplay()); + } + + public function testRunListUserCommandWithLimit() + { + $application = new Application($this->getClient()->getKernel()); + $application->add(new ListUserCommand()); + + $command = $application->find('wallabag:user:list'); + + $tester = new CommandTester($command); + $tester->execute([ + 'command' => $command->getName(), + '--limit' => 2, + ]); + + $this->assertContains('2/3 user(s) displayed.', $tester->getDisplay()); + } + + public function testRunListUserCommandWithSearch() + { + $application = new Application($this->getClient()->getKernel()); + $application->add(new ListUserCommand()); + + $command = $application->find('wallabag:user:list'); + + $tester = new CommandTester($command); + $tester->execute([ + 'command' => $command->getName(), + 'search' => 'boss', + ]); + + $this->assertContains('1/3 (filtered) user(s) displayed.', $tester->getDisplay()); + } + + public function testRunListUserCommandWithSearchAndLimit() + { + $application = new Application($this->getClient()->getKernel()); + $application->add(new ListUserCommand()); + + $command = $application->find('wallabag:user:list'); + + $tester = new CommandTester($command); + $tester->execute([ + 'command' => $command->getName(), + 'search' => 'bo', + '--limit' => 1, + ]); + + $this->assertContains('1/3 (filtered) user(s) displayed.', $tester->getDisplay()); + } +}