Qr.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\Controller\Result\Raw;
  24. use Magento\Framework\View\Result\PageFactory;
  25. use MSP\TwoFactorAuth\Api\TfaInterface;
  26. use MSP\TwoFactorAuth\Controller\Adminhtml\AbstractAction;
  27. use MSP\TwoFactorAuth\Model\Provider\Engine\Google;
  28. /**
  29. * @SuppressWarnings(PHPMD.CamelCaseMethodName)
  30. */
  31. class Qr 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 Raw
  47. */
  48. private $rawResult;
  49. /**
  50. * @var Google
  51. */
  52. private $google;
  53. public function __construct(
  54. Action\Context $context,
  55. Session $session,
  56. PageFactory $pageFactory,
  57. TfaInterface $tfa,
  58. Google $google,
  59. Raw $rawResult
  60. ) {
  61. parent::__construct($context);
  62. $this->tfa = $tfa;
  63. $this->session = $session;
  64. $this->pageFactory = $pageFactory;
  65. $this->rawResult = $rawResult;
  66. $this->google = $google;
  67. }
  68. /**
  69. * Get current user
  70. * @return \Magento\User\Model\User|null
  71. */
  72. private function getUser()
  73. {
  74. return $this->session->getUser();
  75. }
  76. public function execute()
  77. {
  78. $pngData = $this->google->getQrCodeAsPng($this->getUser());
  79. $this->rawResult
  80. ->setHttpResponseCode(200)
  81. ->setHeader('Content-Type', 'image/png')
  82. ->setContents($pngData);
  83. return $this->rawResult;
  84. }
  85. /**
  86. * Check if admin has permissions to visit related pages
  87. *
  88. * @return bool
  89. */
  90. protected function _isAllowed()
  91. {
  92. $user = $this->getUser();
  93. return
  94. $user &&
  95. $this->tfa->getProviderIsAllowed($user->getId(), Google::CODE) &&
  96. !$this->tfa->getProvider(Google::CODE)->isActive($user->getId());
  97. }
  98. }