TwoStepVerificationCommand.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\UserBundle\Command;
  11. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. class TwoStepVerificationCommand extends ContainerAwareCommand
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function configure()
  22. {
  23. $this->setName('sonata:user:two-step-verification');
  24. $this->addArgument('username', InputArgument::REQUIRED, 'The username to protect with a two step verification process');
  25. $this->addOption('reset', null, InputOption::VALUE_NONE, 'Reset the current two step verification token');
  26. $this->setDescription('Generate a two step verification process to secure an access (Ideal for super admin protection)');
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function execute(InputInterface $input, OutputInterface $output)
  32. {
  33. if (!$this->getContainer()->has('sonata.user.google.authenticator.provider')) {
  34. throw new \RuntimeException('Two Step Verification process is not enabled');
  35. }
  36. $helper = $this->getContainer()->get('sonata.user.google.authenticator.provider');
  37. $manager = $this->getContainer()->get('fos_user.user_manager');
  38. $user = $manager->findUserByUsernameOrEmail($input->getArgument('username'));
  39. if (!$user) {
  40. throw new \RuntimeException(sprintf('Unable to find the username : %s', $input->getArgument('username')));
  41. }
  42. if (!$user->getTwoStepVerificationCode() || $input->getOption('reset')) {
  43. $user->setTwoStepVerificationCode($helper->generateSecret());
  44. $manager->updateUser($user);
  45. }
  46. $output->writeln(array(
  47. sprintf('<info>Username</info> : %s', $input->getArgument('username')),
  48. sprintf('<info>Secret</info> : %s', $user->getTwoStepVerificationCode()),
  49. sprintf('<info>Url</info> : %s', $helper->getUrl($user)),
  50. ));
  51. }
  52. }