vendor/sonata-project/user-bundle/src/Command/TwoStepVerificationCommand.php line 30

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Sonata\UserBundle\Command;
  12. use FOS\UserBundle\Model\UserManagerInterface;
  13. use Sonata\UserBundle\GoogleAuthenticator\Helper;
  14. use Sonata\UserBundle\Model\UserInterface;
  15. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  16. use Symfony\Component\Console\Input\InputArgument;
  17. use Symfony\Component\Console\Input\InputInterface;
  18. use Symfony\Component\Console\Input\InputOption;
  19. use Symfony\Component\Console\Output\OutputInterface;
  20. /**
  21.  * NEXT_MAJOR: Remove this command.
  22.  *
  23.  * @deprecated since sonata-project/user-bundle 4.14, it will be removed on 5.0.
  24.  */
  25. class TwoStepVerificationCommand extends ContainerAwareCommand
  26. {
  27.     /**
  28.      * @var ?Helper
  29.      */
  30.     private $helper;
  31.     /**
  32.      * @var ?UserManagerInterface
  33.      */
  34.     private $userManager;
  35.     /**
  36.      * NEXT_MAJOR: make $helper and $userManager mandatory (but still nullable).
  37.      */
  38.     public function __construct(
  39.         ?string $name,
  40.         ?Helper $helper null,
  41.         ?UserManagerInterface $userManager null
  42.     ) {
  43.         parent::__construct($name);
  44.         $this->helper $helper;
  45.         $this->userManager $userManager;
  46.     }
  47.     public function configure(): void
  48.     {
  49.         $this->setName('sonata:user:two-step-verification');
  50.         $this->addArgument(
  51.             'username',
  52.             InputArgument::REQUIRED,
  53.             'The username to protect with a two step verification process'
  54.         );
  55.         $this->addOption('reset'nullInputOption::VALUE_NONE'Reset the current two step verification token');
  56.         $this->setDescription(
  57.             'Generate a two step verification process to secure an access (Ideal for super admin protection)'
  58.         );
  59.     }
  60.     public function execute(InputInterface $inputOutputInterface $output): void
  61.     {
  62.         if (null === $this->helper && !$this->getContainer()->has('sonata.user.google.authenticator.provider')) {
  63.             throw new \RuntimeException('Two Step Verification process is not enabled');
  64.         }
  65.         if (null === $this->helper) {
  66.             @trigger_error(sprintf(
  67.                 'Not providing the $helper argument of "%s::__construct()" is deprecated since 4.3.0 and will no longer be possible in 5.0',
  68.                 __CLASS__
  69.             ), \E_USER_DEPRECATED);
  70.             $helper $this->getContainer()->get('sonata.user.google.authenticator.provider');
  71.             \assert($helper instanceof Helper);
  72.             $this->helper $helper;
  73.         }
  74.         if (null === $this->userManager) {
  75.             @trigger_error(sprintf(
  76.                 'Not providing the $userManager argument of "%s::__construct()" is deprecated since 4.3.0 and will no longer be possible in 5.0',
  77.                 __CLASS__
  78.             ), \E_USER_DEPRECATED);
  79.             $manager $this->getContainer()->get('fos_user.user_manager');
  80.             \assert($manager instanceof UserManagerInterface);
  81.             $this->userManager $manager;
  82.         }
  83.         $user $this->userManager->findUserByUsernameOrEmail($input->getArgument('username'));
  84.         \assert($user instanceof UserInterface);
  85.         if (!$user) {
  86.             throw new \RuntimeException(sprintf('Unable to find the username : %s'$input->getArgument('username')));
  87.         }
  88.         if (!$user->getTwoStepVerificationCode() || $input->getOption('reset')) {
  89.             $user->setTwoStepVerificationCode($this->helper->generateSecret());
  90.             $this->userManager->updateUser($user);
  91.         }
  92.         $output->writeln([
  93.             sprintf('<info>Username</info> : %s'$input->getArgument('username')),
  94.             sprintf('<info>Secret</info> : %s'$user->getTwoStepVerificationCode()),
  95.             sprintf('<info>Url</info> : %s'$this->helper->getUrl($user)),
  96.         ]);
  97.     }
  98. }