BackendAuthentication.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * RSS Backend Authentication plugin
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Rss\App\Action\Plugin;
  9. use Magento\Backend\App\AbstractAction;
  10. use Magento\Framework\App\RequestInterface;
  11. use Magento\Framework\App\ResponseInterface;
  12. use Magento\Framework\Exception\AuthenticationException;
  13. /**
  14. * @api
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. * @since 100.0.2
  17. */
  18. class BackendAuthentication extends \Magento\Backend\App\Action\Plugin\Authentication
  19. {
  20. /**
  21. * @var \Magento\Framework\HTTP\Authentication
  22. */
  23. protected $httpAuthentication;
  24. /**
  25. * @var \Psr\Log\LoggerInterface
  26. */
  27. protected $logger;
  28. /**
  29. * @var \Magento\Framework\AuthorizationInterface
  30. */
  31. protected $authorization;
  32. /**
  33. * @var array
  34. */
  35. protected $aclResources;
  36. /**
  37. * @param \Magento\Backend\Model\Auth $auth
  38. * @param \Magento\Backend\Model\UrlInterface $url
  39. * @param ResponseInterface $response
  40. * @param \Magento\Framework\App\ActionFlag $actionFlag
  41. * @param \Magento\Framework\Message\ManagerInterface $messageManager
  42. * @param \Magento\Backend\Model\UrlInterface $backendUrl
  43. * @param \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory
  44. * @param \Magento\Backend\App\BackendAppList $backendAppList
  45. * @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
  46. * @param \Magento\Framework\HTTP\Authentication $httpAuthentication
  47. * @param \Psr\Log\LoggerInterface $logger
  48. * @param \Magento\Framework\AuthorizationInterface $authorization
  49. * @param array $aclResources
  50. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  51. */
  52. public function __construct(
  53. \Magento\Backend\Model\Auth $auth,
  54. \Magento\Backend\Model\UrlInterface $url,
  55. ResponseInterface $response,
  56. \Magento\Framework\App\ActionFlag $actionFlag,
  57. \Magento\Framework\Message\ManagerInterface $messageManager,
  58. \Magento\Backend\Model\UrlInterface $backendUrl,
  59. \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory,
  60. \Magento\Backend\App\BackendAppList $backendAppList,
  61. \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
  62. \Magento\Framework\HTTP\Authentication $httpAuthentication,
  63. \Psr\Log\LoggerInterface $logger,
  64. \Magento\Framework\AuthorizationInterface $authorization,
  65. array $aclResources
  66. ) {
  67. $this->httpAuthentication = $httpAuthentication;
  68. $this->logger = $logger;
  69. $this->authorization = $authorization;
  70. $this->aclResources = $aclResources;
  71. parent::__construct(
  72. $auth,
  73. $url,
  74. $response,
  75. $actionFlag,
  76. $messageManager,
  77. $backendUrl,
  78. $resultRedirectFactory,
  79. $backendAppList,
  80. $formKeyValidator
  81. );
  82. }
  83. /**
  84. * Replace standard admin login form with HTTP Basic authentication
  85. *
  86. * @param AbstractAction $subject
  87. * @param callable $proceed
  88. * @param RequestInterface $request
  89. * @return ResponseInterface
  90. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  91. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  92. * @SuppressWarnings(PHPMD.NPathComplexity)
  93. */
  94. public function aroundDispatch(AbstractAction $subject, \Closure $proceed, RequestInterface $request)
  95. {
  96. $resource = isset($this->aclResources[$request->getControllerName()])
  97. ? isset($this->aclResources[$request->getControllerName()][$request->getActionName()])
  98. ? $this->aclResources[$request->getControllerName()][$request->getActionName()]
  99. : $this->aclResources[$request->getControllerName()]
  100. : null;
  101. $type = $request->getParam('type');
  102. $resourceType = isset($this->aclResources[$type]) ? $this->aclResources[$type] : null;
  103. if (!$resource || !$resourceType) {
  104. return parent::aroundDispatch($subject, $proceed, $request);
  105. }
  106. $session = $this->_auth->getAuthStorage();
  107. // Try to login using HTTP-authentication
  108. if (!$session->isLoggedIn()) {
  109. list($login, $password) = $this->httpAuthentication->getCredentials();
  110. try {
  111. $this->_auth->login($login, $password);
  112. } catch (AuthenticationException $e) {
  113. $this->logger->critical($e);
  114. }
  115. }
  116. // Verify if logged in and authorized
  117. if (!$session->isLoggedIn() || !$this->authorization->isAllowed($resource)
  118. || !$this->authorization->isAllowed($resourceType)) {
  119. $this->httpAuthentication->setAuthenticationFailed('RSS Feeds');
  120. return $this->_response;
  121. }
  122. return parent::aroundDispatch($subject, $proceed, $request);
  123. }
  124. }