Configurepost.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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\Google;
  21. use Magento\Backend\Model\Auth\Session;
  22. use Magento\Backend\App\Action;
  23. use Magento\Framework\Controller\Result\JsonFactory;
  24. use Magento\Framework\DataObjectFactory;
  25. use MSP\TwoFactorAuth\Model\AlertInterface;
  26. use MSP\TwoFactorAuth\Api\TfaInterface;
  27. use MSP\TwoFactorAuth\Api\TfaSessionInterface;
  28. use MSP\TwoFactorAuth\Controller\Adminhtml\AbstractAction;
  29. use MSP\TwoFactorAuth\Model\Provider\Engine\Google;
  30. /**
  31. * @SuppressWarnings(PHPMD.CamelCaseMethodName)
  32. */
  33. class Configurepost extends AbstractAction
  34. {
  35. /**
  36. * @var TfaInterface
  37. */
  38. private $tfa;
  39. /**
  40. * @var Session
  41. */
  42. private $session;
  43. /**
  44. * @var JsonFactory
  45. */
  46. private $jsonFactory;
  47. /**
  48. * @var Google
  49. */
  50. private $google;
  51. /**
  52. * @var TfaSessionInterface
  53. */
  54. private $tfaSession;
  55. /**
  56. * @var DataObjectFactory
  57. */
  58. private $dataObjectFactory;
  59. /**
  60. * @var AlertInterface
  61. */
  62. private $alert;
  63. public function __construct(
  64. Action\Context $context,
  65. Session $session,
  66. JsonFactory $jsonFactory,
  67. Google $google,
  68. TfaSessionInterface $tfaSession,
  69. TfaInterface $tfa,
  70. AlertInterface $alert,
  71. DataObjectFactory $dataObjectFactory
  72. ) {
  73. parent::__construct($context);
  74. $this->tfa = $tfa;
  75. $this->session = $session;
  76. $this->jsonFactory = $jsonFactory;
  77. $this->google = $google;
  78. $this->tfaSession = $tfaSession;
  79. $this->dataObjectFactory = $dataObjectFactory;
  80. $this->alert = $alert;
  81. }
  82. /**
  83. * Get current user
  84. * @return \Magento\User\Model\User|null
  85. */
  86. private function getUser()
  87. {
  88. return $this->session->getUser();
  89. }
  90. /**
  91. * @inheritdoc
  92. * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface
  93. * @throws \Magento\Framework\Exception\NoSuchEntityException
  94. */
  95. public function execute()
  96. {
  97. $response = $this->jsonFactory->create();
  98. $user = $this->getUser();
  99. if ($this->google->verify($user, $this->dataObjectFactory->create([
  100. 'data' => $this->getRequest()->getParams(),
  101. ]))) {
  102. $this->tfa->getProvider(Google::CODE)->activate($user->getId());
  103. $this->tfaSession->grantAccess();
  104. $this->alert->event(
  105. 'MSP_TwoFactorAuth',
  106. 'New Google Authenticator code issued',
  107. AlertInterface::LEVEL_INFO,
  108. $user->getUserName()
  109. );
  110. $response->setData([
  111. 'success' => true,
  112. ]);
  113. } else {
  114. $response->setData([
  115. 'success' => false,
  116. 'message' => 'Invalid code',
  117. ]);
  118. }
  119. return $response;
  120. }
  121. /**
  122. * Check if admin has permissions to visit related pages
  123. *
  124. * @return bool
  125. */
  126. protected function _isAllowed()
  127. {
  128. $user = $this->getUser();
  129. return
  130. $user &&
  131. $this->tfa->getProviderIsAllowed($user->getId(), Google::CODE) &&
  132. !$this->tfa->getProvider(Google::CODE)->isActive($user->getId());
  133. }
  134. }