Index.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\Model\Auth\Session;
  22. use Magento\Backend\App\Action;
  23. use Magento\Framework\Exception\LocalizedException;
  24. use MSP\TwoFactorAuth\Api\TfaInterface;
  25. use MSP\TwoFactorAuth\Api\UserConfigManagerInterface;
  26. use MSP\TwoFactorAuth\Controller\Adminhtml\AbstractAction;
  27. class Index extends AbstractAction
  28. {
  29. /**
  30. * @var TfaInterface
  31. */
  32. private $tfa;
  33. /**
  34. * @var Session
  35. */
  36. private $session;
  37. /**
  38. * @var UserConfigManagerInterface
  39. */
  40. private $userConfigManager;
  41. /**
  42. * @var Action\Context
  43. */
  44. private $context;
  45. public function __construct(
  46. Action\Context $context,
  47. Session $session,
  48. UserConfigManagerInterface $userConfigManager,
  49. TfaInterface $tfa
  50. ) {
  51. parent::__construct($context);
  52. $this->tfa = $tfa;
  53. $this->session = $session;
  54. $this->userConfigManager = $userConfigManager;
  55. $this->context = $context;
  56. }
  57. /**
  58. * Get current user
  59. * @return \Magento\User\Model\User|null
  60. */
  61. private function getUser()
  62. {
  63. return $this->session->getUser();
  64. }
  65. /**
  66. * @inheritdoc
  67. */
  68. public function execute()
  69. {
  70. $user = $this->getUser();
  71. $providersToConfigure = $this->tfa->getProvidersToActivate($user->getId());
  72. if (!empty($providersToConfigure)) {
  73. return $this->_redirect($providersToConfigure[0]->getConfigureAction());
  74. }
  75. $providerCode = '';
  76. $defaultProviderCode = $this->userConfigManager->getDefaultProvider($user->getId());
  77. if ($this->tfa->getProviderIsAllowed($user->getId(), $defaultProviderCode)) {
  78. $providerCode = $defaultProviderCode;
  79. }
  80. if (!$providerCode) {
  81. $providers = $this->tfa->getUserProviders($user->getId());
  82. if (!empty($providers)) {
  83. $providerCode = $providers[0]->getCode();
  84. }
  85. }
  86. if (!$providerCode) {
  87. return $this->_redirect($this->context->getBackendUrl()->getStartupPageUrl());
  88. }
  89. if ($provider = $this->tfa->getProvider($providerCode)) {
  90. return $this->_redirect($provider->getAuthAction());
  91. }
  92. throw new LocalizedException(__('Internal error accessing 2FA index page'));
  93. }
  94. }