Verify.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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\Registry;
  24. use Magento\Framework\View\Result\PageFactory;
  25. use MSP\TwoFactorAuth\Api\TfaInterface;
  26. use MSP\TwoFactorAuth\Api\UserConfigManagerInterface;
  27. use MSP\TwoFactorAuth\Controller\Adminhtml\AbstractAction;
  28. use MSP\TwoFactorAuth\Model\Provider\Engine\Authy;
  29. /**
  30. * @SuppressWarnings(PHPMD.CamelCaseMethodName)
  31. */
  32. class Verify extends AbstractAction
  33. {
  34. /**
  35. * @var PageFactory
  36. */
  37. private $pageFactory;
  38. /**
  39. * @var Session
  40. */
  41. private $session;
  42. /**
  43. * @var TfaInterface
  44. */
  45. private $tfa;
  46. /**
  47. * @var UserConfigManagerInterface
  48. */
  49. private $userConfigManager;
  50. /**
  51. * @var Registry
  52. */
  53. private $registry;
  54. /**
  55. * Verify constructor.
  56. * @param Action\Context $context
  57. * @param Session $session
  58. * @param TfaInterface $tfa
  59. * @param Registry $registry
  60. * @param UserConfigManagerInterface $userConfigManager
  61. * @param PageFactory $pageFactory
  62. */
  63. public function __construct(
  64. Action\Context $context,
  65. Session $session,
  66. TfaInterface $tfa,
  67. Registry $registry,
  68. UserConfigManagerInterface $userConfigManager,
  69. PageFactory $pageFactory
  70. ) {
  71. parent::__construct($context);
  72. $this->pageFactory = $pageFactory;
  73. $this->session = $session;
  74. $this->tfa = $tfa;
  75. $this->userConfigManager = $userConfigManager;
  76. $this->registry = $registry;
  77. }
  78. /**
  79. * Get current user
  80. * @return \Magento\User\Model\User|null
  81. */
  82. private function getUser()
  83. {
  84. return $this->session->getUser();
  85. }
  86. /**
  87. * Get verify information
  88. * @return verify payload
  89. * @throws \Magento\Framework\Exception\NoSuchEntityException
  90. */
  91. private function getVerifyInformation()
  92. {
  93. $providerConfig = $this->userConfigManager->getProviderConfig($this->getUser()->getId(), Authy::CODE);
  94. if (!isset($providerConfig['verify'])) {
  95. return null;
  96. }
  97. return $providerConfig['verify'];
  98. }
  99. /**
  100. * @inheritdoc
  101. */
  102. public function execute()
  103. {
  104. $verifyInfo = $this->getVerifyInformation();
  105. $this->registry->register('msp_tfa_authy_verify', $verifyInfo);
  106. return $this->pageFactory->create();
  107. }
  108. /**
  109. * @inheritdoc
  110. */
  111. protected function _isAllowed()
  112. {
  113. $user = $this->getUser();
  114. return
  115. $user &&
  116. $this->tfa->getProviderIsAllowed($user->getId(), Authy::CODE) &&
  117. $this->getVerifyInformation() &&
  118. !$this->tfa->getProvider(Authy::CODE)->isActive($user->getId());
  119. }
  120. }