From bf6c0346d8d35a719dd1bff1cb4d573d422f99ff Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Wed, 31 May 2017 09:31:18 +0200 Subject: WIP Signed-off-by: Thomas Citharel --- .../Command/CreateAccountsCommand.php | 126 +++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 src/Wallabag/FederationBundle/Command/CreateAccountsCommand.php (limited to 'src/Wallabag/FederationBundle/Command/CreateAccountsCommand.php') 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 @@ +setName('wallabag:federation:create-accounts') + ->setDescription('Creates missing federation accounts') + ->setHelp('This command creates accounts for federation for missing users') + ->addArgument( + 'username', + InputArgument::OPTIONAL, + 'User to create an account for' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->output = $output; + + $domainName = $this->getContainer()->getParameter('domain_name'); + $instance = $this->checkInstance($domainName); + + $username = $input->getArgument('username'); + + if ($username) { + try { + $user = $this->getUser($username); + $this->createAccount($user, $instance); + } catch (NoResultException $e) { + $output->writeln(sprintf('User "%s" not found.', $username)); + + return 1; + } + } else { + $users = $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findAll(); + + $output->writeln(sprintf('Creating through %d user federated accounts', count($users))); + + foreach ($users as $user) { + $output->writeln(sprintf('Processing user %s', $user->getUsername())); + $this->createAccount($user, $instance); + } + $output->writeln(sprintf('Creating user federated accounts. %d accounts created in total', $this->created)); + } + + return 0; + } + + /** + * @param User $user + * @param $instance + */ + private function createAccount(User $user, Instance $instance) + { + $em = $this->getContainer()->get('doctrine.orm.entity_manager'); + $repo = $em->getRepository('WallabagFederationBundle:Account'); + + if ($repo->findBy(['user' => $user->getId()])) { + return; + } + + $account = new Account(); + $account->setUsername($user->getUsername()) + ->setUser($user) + ->setServer($instance); + + $em->persist($account); + $em->flush(); + + $user->setAccount($account); + $em->persist($account); + $em->flush(); + + ++$this->created; + } + + private function checkInstance($domainName) + { + $em = $this->getContainer()->get('doctrine.orm.entity_manager'); + $repo = $em->getRepository('WallabagFederationBundle:Instance'); + + $instance = $repo->findOneByDomain($domainName); + if (!$instance) { + $instance = new Instance($domainName); + + $em->persist($instance); + $em->flush(); + } + return $instance; + } + + /** + * Fetches a user from its username. + * + * @param string $username + * + * @return \Wallabag\UserBundle\Entity\User + */ + private function getUser($username) + { + return $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findOneByUserName($username); + } + + private function getDoctrine() + { + return $this->getContainer()->get('doctrine'); + } +} -- cgit v1.2.3