aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/Wallabag/CoreBundle/Command/ListUserCommand.php61
-rw-r--r--src/Wallabag/UserBundle/Repository/UserRepository.php13
-rw-r--r--tests/Wallabag/CoreBundle/Command/ListUserCommandTest.php75
3 files changed, 149 insertions, 0 deletions
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 @@
1<?php
2
3namespace Wallabag\CoreBundle\Command;
4
5use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6use Symfony\Component\Console\Input\InputArgument;
7use Symfony\Component\Console\Input\InputInterface;
8use Symfony\Component\Console\Input\InputOption;
9use Symfony\Component\Console\Output\OutputInterface;
10use Symfony\Component\Console\Style\SymfonyStyle;
11
12class ListUserCommand extends ContainerAwareCommand
13{
14 protected function configure()
15 {
16 $this
17 ->setName('wallabag:user:list')
18 ->setDescription('List all users')
19 ->setHelp('This command list all existing users')
20 ->addArgument('search', InputArgument::OPTIONAL, 'Filter list by given search term')
21 ->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Max number of displayed users', 100)
22 ;
23 }
24
25 protected function execute(InputInterface $input, OutputInterface $output)
26 {
27 $io = new SymfonyStyle($input, $output);
28
29 $users = $this->getContainer()->get('wallabag_user.user_repository')
30 ->getQueryBuilderForSearch($input->getArgument('search'))
31 ->setMaxResults($input->getOption('limit'))
32 ->getQuery()
33 ->getResult();
34
35 $nbUsers = $this->getContainer()->get('wallabag_user.user_repository')
36 ->getSumUsers();
37
38 $rows = [];
39 foreach ($users as $user) {
40 $rows[] = [
41 $user->getUsername(),
42 $user->getEmail(),
43 $user->isEnabled() ? 'yes' : 'no',
44 $user->hasRole('ROLE_SUPER_ADMIN') || $user->hasRole('ROLE_ADMIN') ? 'yes' : 'no',
45 ];
46 }
47
48 $io->table(['username', 'email', 'is enabled?', 'is admin?'], $rows);
49
50 $io->success(
51 sprintf(
52 '%s/%s%s user(s) displayed.',
53 count($users),
54 $nbUsers,
55 $input->getArgument('search') === null ? '' : ' (filtered)'
56 )
57 );
58
59 return 0;
60 }
61}
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
@@ -56,6 +56,19 @@ class UserRepository extends EntityRepository
56 } 56 }
57 57
58 /** 58 /**
59 * Count how many users are existing.
60 *
61 * @return int
62 */
63 public function getSumUsers()
64 {
65 return $this->createQueryBuilder('u')
66 ->select('count(u)')
67 ->getQuery()
68 ->getSingleScalarResult();
69 }
70
71 /**
59 * Retrieves users filtered with a search term. 72 * Retrieves users filtered with a search term.
60 * 73 *
61 * @param string $term 74 * @param string $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 @@
1<?php
2
3namespace Tests\Wallabag\CoreBundle\Command;
4
5use Symfony\Bundle\FrameworkBundle\Console\Application;
6use Symfony\Component\Console\Tester\CommandTester;
7use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
8use Wallabag\CoreBundle\Command\ListUserCommand;
9
10class ListUserCommandTest extends WallabagCoreTestCase
11{
12 public function testRunListUserCommand()
13 {
14 $application = new Application($this->getClient()->getKernel());
15 $application->add(new ListUserCommand());
16
17 $command = $application->find('wallabag:user:list');
18
19 $tester = new CommandTester($command);
20 $tester->execute([
21 'command' => $command->getName(),
22 ]);
23
24 $this->assertContains('3/3 user(s) displayed.', $tester->getDisplay());
25 }
26
27 public function testRunListUserCommandWithLimit()
28 {
29 $application = new Application($this->getClient()->getKernel());
30 $application->add(new ListUserCommand());
31
32 $command = $application->find('wallabag:user:list');
33
34 $tester = new CommandTester($command);
35 $tester->execute([
36 'command' => $command->getName(),
37 '--limit' => 2,
38 ]);
39
40 $this->assertContains('2/3 user(s) displayed.', $tester->getDisplay());
41 }
42
43 public function testRunListUserCommandWithSearch()
44 {
45 $application = new Application($this->getClient()->getKernel());
46 $application->add(new ListUserCommand());
47
48 $command = $application->find('wallabag:user:list');
49
50 $tester = new CommandTester($command);
51 $tester->execute([
52 'command' => $command->getName(),
53 'search' => 'boss',
54 ]);
55
56 $this->assertContains('1/3 (filtered) user(s) displayed.', $tester->getDisplay());
57 }
58
59 public function testRunListUserCommandWithSearchAndLimit()
60 {
61 $application = new Application($this->getClient()->getKernel());
62 $application->add(new ListUserCommand());
63
64 $command = $application->find('wallabag:user:list');
65
66 $tester = new CommandTester($command);
67 $tester->execute([
68 'command' => $command->getName(),
69 'search' => 'bo',
70 '--limit' => 1,
71 ]);
72
73 $this->assertContains('1/3 (filtered) user(s) displayed.', $tester->getDisplay());
74 }
75}