Configurepost.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\Authy;
  21. use Magento\Backend\App\Action;
  22. use Magento\Backend\Model\Auth\Session;
  23. use Magento\Framework\Controller\Result\JsonFactory;
  24. use Magento\Framework\View\Result\PageFactory;
  25. use MSP\TwoFactorAuth\Model\AlertInterface;
  26. use MSP\TwoFactorAuth\Api\TfaInterface;
  27. use MSP\TwoFactorAuth\Api\UserConfigManagerInterface;
  28. use MSP\TwoFactorAuth\Controller\Adminhtml\AbstractAction;
  29. use MSP\TwoFactorAuth\Model\Provider\Engine\Authy;
  30. /**
  31. * @SuppressWarnings(PHPMD.CamelCaseMethodName)
  32. */
  33. class Configurepost extends AbstractAction
  34. {
  35. /**
  36. * @var PageFactory
  37. */
  38. private $jsonFactory;
  39. /**
  40. * @var Session
  41. */
  42. private $session;
  43. /**
  44. * @var TfaInterface
  45. */
  46. private $tfa;
  47. /**
  48. * @var AlertInterface
  49. */
  50. private $alert;
  51. /**
  52. * @var Authy\Verification
  53. */
  54. private $verification;
  55. /**
  56. * @var UserConfigManagerInterface
  57. */
  58. private $userConfigManager;
  59. /**
  60. * Configurepost constructor.
  61. * @param Action\Context $context
  62. * @param Session $session
  63. * @param Authy\Verification $verification
  64. * @param UserConfigManagerInterface $userConfigManager
  65. * @param TfaInterface $tfa
  66. * @param AlertInterface $alert
  67. * @param JsonFactory $jsonFactory
  68. */
  69. public function __construct(
  70. Action\Context $context,
  71. Session $session,
  72. Authy\Verification $verification,
  73. UserConfigManagerInterface $userConfigManager,
  74. TfaInterface $tfa,
  75. AlertInterface $alert,
  76. JsonFactory $jsonFactory
  77. ) {
  78. parent::__construct($context);
  79. $this->jsonFactory = $jsonFactory;
  80. $this->session = $session;
  81. $this->tfa = $tfa;
  82. $this->alert = $alert;
  83. $this->verification = $verification;
  84. $this->userConfigManager = $userConfigManager;
  85. }
  86. /**
  87. * Get current user
  88. * @return \Magento\User\Model\User|null
  89. */
  90. private function getUser()
  91. {
  92. return $this->session->getUser();
  93. }
  94. /**
  95. * @inheritdoc
  96. */
  97. public function execute()
  98. {
  99. $request = $this->getRequest();
  100. $response = $this->jsonFactory->create();
  101. try {
  102. $this->verification->request(
  103. $this->getUser(),
  104. $request->getParam('tfa_country'),
  105. $request->getParam('tfa_phone'),
  106. $request->getParam('tfa_method'),
  107. $res
  108. );
  109. $this->alert->event(
  110. 'MSP_TwoFactorAuth',
  111. 'New authy verification request via ' . $request->getParam('tfa_method'),
  112. AlertInterface::LEVEL_INFO,
  113. $this->getUser()->getUserName()
  114. );
  115. $response->setData([
  116. 'success' => true,
  117. 'message' => $res['message'],
  118. 'seconds_to_expire' => (int) $res['seconds_to_expire'],
  119. ]);
  120. } catch (\Exception $e) {
  121. $this->alert->event(
  122. 'MSP_TwoFactorAuth',
  123. 'Authy verification request failure via ' . $request->getParam('tfa_method'),
  124. AlertInterface::LEVEL_ERROR,
  125. $this->getUser()->getUserName(),
  126. AlertInterface::ACTION_LOG,
  127. $e->getMessage()
  128. );
  129. $response->setData(['success' => false, 'message' => $e->getMessage()]);
  130. }
  131. return $response;
  132. }
  133. /**
  134. * @inheritdoc
  135. */
  136. protected function _isAllowed()
  137. {
  138. $user = $this->getUser();
  139. return
  140. $user &&
  141. $this->tfa->getProviderIsAllowed($user->getId(), Authy::CODE) &&
  142. !$this->tfa->getProvider(Authy::CODE)->isActive($user->getId());
  143. }
  144. }