aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/FederationBundle/Command
diff options
context:
space:
mode:
authorThomas Citharel <tcit@tcit.fr>2017-05-31 09:31:18 +0200
committerThomas Citharel <tcit@tcit.fr>2017-06-23 10:08:54 +0200
commitbf6c0346d8d35a719dd1bff1cb4d573d422f99ff (patch)
tree04fb80e20ad1cffacb72357a632a9afe2ec058cb /src/Wallabag/FederationBundle/Command
parentd6d3f4ec3698effd1d4b063e295341791bdcf7d7 (diff)
downloadwallabag-federation.tar.gz
wallabag-federation.tar.zst
wallabag-federation.zip
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
Diffstat (limited to 'src/Wallabag/FederationBundle/Command')
-rw-r--r--src/Wallabag/FederationBundle/Command/CreateAccountsCommand.php126
1 files changed, 126 insertions, 0 deletions
diff --git a/src/Wallabag/FederationBundle/Command/CreateAccountsCommand.php b/src/Wallabag/FederationBundle/Command/CreateAccountsCommand.php
new file mode 100644
index 00000000..f2ca3b06
--- /dev/null
+++ b/src/Wallabag/FederationBundle/Command/CreateAccountsCommand.php
@@ -0,0 +1,126 @@
1<?php
2
3namespace Wallabag\FederationBundle\Command;
4
5use Doctrine\ORM\NoResultException;
6use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7use Symfony\Component\Console\Input\InputArgument;
8use Symfony\Component\Console\Input\InputInterface;
9use Symfony\Component\Console\Output\OutputInterface;
10use Wallabag\FederationBundle\Entity\Account;
11use Wallabag\FederationBundle\Entity\Instance;
12use Wallabag\UserBundle\Entity\User;
13
14class CreateAccountsCommand extends ContainerAwareCommand
15{
16 /** @var OutputInterface */
17 protected $output;
18
19 protected $created = 0;
20
21 protected function configure()
22 {
23 $this
24 ->setName('wallabag:federation:create-accounts')
25 ->setDescription('Creates missing federation accounts')
26 ->setHelp('This command creates accounts for federation for missing users')
27 ->addArgument(
28 'username',
29 InputArgument::OPTIONAL,
30 'User to create an account for'
31 );
32 }
33
34 protected function execute(InputInterface $input, OutputInterface $output)
35 {
36 $this->output = $output;
37
38 $domainName = $this->getContainer()->getParameter('domain_name');
39 $instance = $this->checkInstance($domainName);
40
41 $username = $input->getArgument('username');
42
43 if ($username) {
44 try {
45 $user = $this->getUser($username);
46 $this->createAccount($user, $instance);
47 } catch (NoResultException $e) {
48 $output->writeln(sprintf('<error>User "%s" not found.</error>', $username));
49
50 return 1;
51 }
52 } else {
53 $users = $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findAll();
54
55 $output->writeln(sprintf('Creating through %d user federated accounts', count($users)));
56
57 foreach ($users as $user) {
58 $output->writeln(sprintf('Processing user %s', $user->getUsername()));
59 $this->createAccount($user, $instance);
60 }
61 $output->writeln(sprintf('Creating user federated accounts. %d accounts created in total', $this->created));
62 }
63
64 return 0;
65 }
66
67 /**
68 * @param User $user
69 * @param $instance
70 */
71 private function createAccount(User $user, Instance $instance)
72 {
73 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
74 $repo = $em->getRepository('WallabagFederationBundle:Account');
75
76 if ($repo->findBy(['user' => $user->getId()])) {
77 return;
78 }
79
80 $account = new Account();
81 $account->setUsername($user->getUsername())
82 ->setUser($user)
83 ->setServer($instance);
84
85 $em->persist($account);
86 $em->flush();
87
88 $user->setAccount($account);
89 $em->persist($account);
90 $em->flush();
91
92 ++$this->created;
93 }
94
95 private function checkInstance($domainName)
96 {
97 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
98 $repo = $em->getRepository('WallabagFederationBundle:Instance');
99
100 $instance = $repo->findOneByDomain($domainName);
101 if (!$instance) {
102 $instance = new Instance($domainName);
103
104 $em->persist($instance);
105 $em->flush();
106 }
107 return $instance;
108 }
109
110 /**
111 * Fetches a user from its username.
112 *
113 * @param string $username
114 *
115 * @return \Wallabag\UserBundle\Entity\User
116 */
117 private function getUser($username)
118 {
119 return $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findOneByUserName($username);
120 }
121
122 private function getDoctrine()
123 {
124 return $this->getContainer()->get('doctrine');
125 }
126}