Auth.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\Model\Auth\Session;
  22. use Magento\Backend\App\Action;
  23. use Magento\Framework\View\Result\PageFactory;
  24. use MSP\TwoFactorAuth\Api\TfaInterface;
  25. use MSP\TwoFactorAuth\Api\UserConfigManagerInterface;
  26. use MSP\TwoFactorAuth\Controller\Adminhtml\AbstractAction;
  27. use MSP\TwoFactorAuth\Model\Provider\Engine\Authy;
  28. /**
  29. * @SuppressWarnings(PHPMD.CamelCaseMethodName)
  30. */
  31. class Auth extends AbstractAction
  32. {
  33. /**
  34. * @var TfaInterface
  35. */
  36. private $tfa;
  37. /**
  38. * @var Session
  39. */
  40. private $session;
  41. /**
  42. * @var PageFactory
  43. */
  44. private $pageFactory;
  45. /**
  46. * @var UserConfigManagerInterface
  47. */
  48. private $userConfigManager;
  49. /**
  50. * Auth constructor.
  51. * @param Action\Context $context
  52. * @param Session $session
  53. * @param PageFactory $pageFactory
  54. * @param UserConfigManagerInterface $userConfigManager
  55. * @param TfaInterface $tfa
  56. */
  57. public function __construct(
  58. Action\Context $context,
  59. Session $session,
  60. PageFactory $pageFactory,
  61. UserConfigManagerInterface $userConfigManager,
  62. TfaInterface $tfa
  63. ) {
  64. parent::__construct($context);
  65. $this->tfa = $tfa;
  66. $this->session = $session;
  67. $this->pageFactory = $pageFactory;
  68. $this->userConfigManager = $userConfigManager;
  69. }
  70. /**
  71. * Get current user
  72. * @return \Magento\User\Model\User|null
  73. */
  74. private function getUser()
  75. {
  76. return $this->session->getUser();
  77. }
  78. /**
  79. * @inheritdoc
  80. * @throws \Magento\Framework\Exception\NoSuchEntityException
  81. */
  82. public function execute()
  83. {
  84. $this->userConfigManager->setDefaultProvider($this->getUser()->getId(), Authy::CODE);
  85. return $this->pageFactory->create();
  86. }
  87. /**
  88. * @inheritdoc
  89. */
  90. protected function _isAllowed()
  91. {
  92. $user = $this->getUser();
  93. return
  94. $user &&
  95. $this->tfa->getProviderIsAllowed($user->getId(), Authy::CODE) &&
  96. $this->tfa->getProvider(Authy::CODE)->isActive($user->getId());
  97. }
  98. }