TfaReset.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * IDEALIAGroup srl
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to info@idealiagroup.com so we can send you a copy immediately.
  14. *
  15. * @category MSP
  16. * @package MSP_TwoFactorAuth
  17. * @copyright Copyright (c) 2016 IDEALIAGroup srl (http://www.idealiagroup.com)
  18. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. */
  20. namespace MSP\TwoFactorAuth\Command;
  21. use Magento\Framework\Exception\LocalizedException;
  22. use MSP\TwoFactorAuth\Api\ProviderPoolInterface;
  23. use MSP\TwoFactorAuth\Api\UserConfigManagerInterface;
  24. use Symfony\Component\Console\Command\Command;
  25. use Symfony\Component\Console\Input\InputArgument;
  26. use Symfony\Component\Console\Input\InputInterface;
  27. use Symfony\Component\Console\Output\OutputInterface;
  28. use Magento\User\Model\UserFactory;
  29. use Magento\User\Model\ResourceModel\User;
  30. class TfaReset extends Command
  31. {
  32. /**
  33. * @var UserConfigManagerInterface
  34. */
  35. private $userConfigManager;
  36. /**
  37. * @var User
  38. */
  39. private $userResource;
  40. /**
  41. * @var UserFactory
  42. */
  43. private $userFactory;
  44. /**
  45. * @var ProviderPoolInterface
  46. */
  47. private $providerPool;
  48. public function __construct(
  49. UserConfigManagerInterface $userConfigManager,
  50. ProviderPoolInterface $providerPool,
  51. UserFactory $userFactory,
  52. User $userResource
  53. ) {
  54. parent::__construct();
  55. $this->userConfigManager = $userConfigManager;
  56. $this->userResource = $userResource;
  57. $this->userFactory = $userFactory;
  58. $this->providerPool = $providerPool;
  59. }
  60. protected function configure()
  61. {
  62. $this->setName('msp:security:tfa:reset');
  63. $this->setDescription('Reset configuration for one user');
  64. $this->addArgument('user', InputArgument::REQUIRED, __('Username'));
  65. $this->addArgument('provider', InputArgument::REQUIRED, __('Provider code'));
  66. parent::configure();
  67. }
  68. /**
  69. * @SuppressWarnings("PHPMD.UnusedFormalParameter")
  70. * @throws LocalizedException
  71. */
  72. protected function execute(InputInterface $input, OutputInterface $output)
  73. {
  74. $userName = $input->getArgument('user');
  75. $providerCode = $input->getArgument('provider');
  76. $user = $this->userFactory->create();
  77. $this->userResource->load($user, $userName, 'username');
  78. if (!$user->getId()) {
  79. throw new LocalizedException(__('Unknown user %1', $userName));
  80. }
  81. $provider = $this->providerPool->getProviderByCode($providerCode);
  82. $this->userConfigManager->resetProviderConfig($user->getId(), $providerCode);
  83. $output->writeln('' . __('Provider %1 has been reset for user %2', $provider->getName(), $userName));
  84. }
  85. }