Auth.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\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\Google;
  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. public function __construct(
  50. Action\Context $context,
  51. Session $session,
  52. PageFactory $pageFactory,
  53. UserConfigManagerInterface $userConfigManager,
  54. TfaInterface $tfa
  55. ) {
  56. parent::__construct($context);
  57. $this->tfa = $tfa;
  58. $this->session = $session;
  59. $this->pageFactory = $pageFactory;
  60. $this->userConfigManager = $userConfigManager;
  61. }
  62. /**
  63. * Get current user
  64. * @return \Magento\User\Model\User|null
  65. */
  66. private function getUser()
  67. {
  68. return $this->session->getUser();
  69. }
  70. public function execute()
  71. {
  72. $this->userConfigManager->setDefaultProvider($this->getUser()->getId(), Google::CODE);
  73. return $this->pageFactory->create();
  74. }
  75. /**
  76. * Check if admin has permissions to visit related pages
  77. *
  78. * @return bool
  79. */
  80. protected function _isAllowed()
  81. {
  82. $user = $this->getUser();
  83. return
  84. $user &&
  85. $this->tfa->getProviderIsAllowed($user->getId(), Google::CODE) &&
  86. $this->tfa->getProvider(Google::CODE)->isActive($user->getId());
  87. }
  88. }