ControllerActionPredispatch.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\Observer;
  21. use Magento\Backend\Model\Auth\Session;
  22. use Magento\Backend\Model\UrlInterface;
  23. use Magento\Framework\App\ActionFlag;
  24. use Magento\Framework\App\Action\Action;
  25. use Magento\Framework\Event\Observer;
  26. use Magento\Framework\Event\ObserverInterface;
  27. use MSP\TwoFactorAuth\Api\TfaInterface;
  28. use MSP\TwoFactorAuth\Api\TfaSessionInterface;
  29. use MSP\TwoFactorAuth\Api\TrustedManagerInterface;
  30. class ControllerActionPredispatch implements ObserverInterface
  31. {
  32. /**
  33. * @var TfaInterface
  34. */
  35. private $tfa;
  36. /**
  37. * @var ActionFlag
  38. */
  39. private $actionFlag;
  40. /**
  41. * @var UrlInterface
  42. */
  43. private $url;
  44. /**
  45. * @var TfaSessionInterface
  46. */
  47. private $tfaSession;
  48. /**
  49. * @var Session
  50. */
  51. private $session;
  52. /**
  53. * @var TrustedManagerInterface
  54. */
  55. private $trustedManager;
  56. public function __construct(
  57. TfaInterface $tfa,
  58. ActionFlag $actionFlag,
  59. UrlInterface $url,
  60. Session $session,
  61. TfaSessionInterface $tfaSession,
  62. TrustedManagerInterface $trustedManager
  63. ) {
  64. $this->tfa = $tfa;
  65. $this->actionFlag = $actionFlag;
  66. $this->url = $url;
  67. $this->tfaSession = $tfaSession;
  68. $this->session = $session;
  69. $this->trustedManager = $trustedManager;
  70. }
  71. /**
  72. * Get current user
  73. * @return \Magento\User\Model\User|null
  74. */
  75. private function getUser()
  76. {
  77. return $this->session->getUser();
  78. }
  79. /**
  80. * @param Observer $observer
  81. * @return void
  82. */
  83. public function execute(Observer $observer)
  84. {
  85. if (!$this->tfa->isEnabled()) {
  86. return;
  87. }
  88. /** @var $controllerAction \Magento\Backend\App\AbstractAction */
  89. $controllerAction = $observer->getEvent()->getControllerAction();
  90. $fullActionName = $controllerAction->getRequest()->getFullActionName();
  91. if (in_array($fullActionName, $this->tfa->getAllowedUrls())) {
  92. return;
  93. }
  94. $user = $this->getUser();
  95. if ($user && !empty($this->tfa->getUserProviders($user->getId()))) {
  96. $accessGranted = ($this->tfaSession->isGranted() || $this->trustedManager->isTrustedDevice()) &&
  97. empty($this->tfa->getProvidersToActivate($user->getId()));
  98. if (!$accessGranted) {
  99. $this->actionFlag->set('', Action::FLAG_NO_DISPATCH, true);
  100. $url = $this->url->getUrl('msp_twofactorauth/tfa/index');
  101. $controllerAction->getResponse()->setRedirect($url);
  102. }
  103. }
  104. }
  105. }