Reset.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * MageSpecialist
  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@magespecialist.it so we can send you a copy immediately.
  14. *
  15. * @category MSP
  16. * @package MSP_TwoFactorAuth
  17. * @copyright Copyright (c) 2017 Skeeller srl (http://www.magespecialist.it)
  18. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. */
  20. namespace MSP\TwoFactorAuth\Controller\Adminhtml\Tfa;
  21. use Magento\Backend\App\Action\Context;
  22. use Magento\Framework\Exception\LocalizedException;
  23. use Magento\User\Model\UserFactory;
  24. use Magento\User\Model\ResourceModel\User as UserResourceModel;
  25. use MSP\TwoFactorAuth\Api\TfaInterface;
  26. use MSP\TwoFactorAuth\Controller\Adminhtml\AbstractAction;
  27. /**
  28. * @SuppressWarnings(PHPMD.CamelCaseMethodName)
  29. */
  30. class Reset extends AbstractAction
  31. {
  32. /**
  33. * @var UserResourceModel
  34. */
  35. private $userResourceModel;
  36. /**
  37. * @var UserFactory
  38. */
  39. private $userInterfaceFactory;
  40. /**
  41. * @var TfaInterface
  42. */
  43. private $tfa;
  44. public function __construct(
  45. Context $context,
  46. UserResourceModel $userResourceModel,
  47. TfaInterface $tfa,
  48. UserFactory $userFactory
  49. ) {
  50. parent::__construct($context);
  51. $this->userResourceModel = $userResourceModel;
  52. $this->userInterfaceFactory = $userFactory;
  53. $this->tfa = $tfa;
  54. }
  55. /**
  56. * @inheritdoc
  57. */
  58. public function execute()
  59. {
  60. $userId = $this->getRequest()->getParam('id');
  61. $providerCode = $this->getRequest()->getParam('provider');
  62. $user = $this->userInterfaceFactory->create();
  63. $this->userResourceModel->load($user, $userId);
  64. if (!$user->getId()) {
  65. throw new LocalizedException(__('Invalid user'));
  66. }
  67. $provider = $this->tfa->getProvider($providerCode);
  68. if (!$provider) {
  69. throw new LocalizedException(__('Unknown provider'));
  70. }
  71. $provider->resetConfiguration($user->getId());
  72. $this->messageManager->addSuccessMessage(__('Configuration has been reset for this user'));
  73. return $this->_redirect('adminhtml/user/edit', ['user_id' => $userId]);
  74. }
  75. /**
  76. * @inheritdoc
  77. */
  78. protected function _isAllowed()
  79. {
  80. return parent::_isAllowed() && $this->_authorization->isAllowed('MSP_TwoFactorAuth::tfa');
  81. }
  82. }