Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BackendAuthentication.php
Go to the documentation of this file.
1 <?php
9 
14 
20 class BackendAuthentication extends \Magento\Backend\App\Action\Plugin\Authentication
21 {
26 
30  protected $logger;
31 
35  protected $authorization;
36 
40  protected $aclResources;
41 
58  public function __construct(
59  \Magento\Backend\Model\Auth $auth,
60  \Magento\Backend\Model\UrlInterface $url,
62  \Magento\Framework\App\ActionFlag $actionFlag,
63  \Magento\Framework\Message\ManagerInterface $messageManager,
64  \Magento\Backend\Model\UrlInterface $backendUrl,
65  \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory,
66  \Magento\Backend\App\BackendAppList $backendAppList,
67  \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
68  \Magento\Framework\HTTP\Authentication $httpAuthentication,
69  \Psr\Log\LoggerInterface $logger,
70  \Magento\Framework\AuthorizationInterface $authorization,
71  array $aclResources
72  ) {
73  $this->httpAuthentication = $httpAuthentication;
74  $this->logger = $logger;
75  $this->authorization = $authorization;
76  $this->aclResources = $aclResources;
77  parent::__construct(
78  $auth,
79  $url,
80  $response,
81  $actionFlag,
87  );
88  }
89 
101  public function aroundDispatch(AbstractAction $subject, \Closure $proceed, RequestInterface $request)
102  {
103  $resource = isset($this->aclResources[$request->getControllerName()])
104  ? isset($this->aclResources[$request->getControllerName()][$request->getActionName()])
105  ? $this->aclResources[$request->getControllerName()][$request->getActionName()]
106  : $this->aclResources[$request->getControllerName()]
107  : null;
108 
109  $type = $request->getParam('type');
110  $resourceType = isset($this->aclResources[$type]) ? $this->aclResources[$type] : null;
111 
112  if (!$resource || !$resourceType) {
113  return parent::aroundDispatch($subject, $proceed, $request);
114  }
115 
116  $session = $this->_auth->getAuthStorage();
117 
118  // Try to login using HTTP-authentication
119  if (!$session->isLoggedIn()) {
120  list($login, $password) = $this->httpAuthentication->getCredentials();
121  try {
122  $this->_auth->login($login, $password);
123  } catch (AuthenticationException $e) {
124  $this->logger->critical($e);
125  }
126  }
127 
128  // Verify if logged in and authorized
129  if (!$session->isLoggedIn() || !$this->authorization->isAllowed($resource)
130  || !$this->authorization->isAllowed($resourceType)) {
131  $this->httpAuthentication->setAuthenticationFailed('RSS Feeds');
132  return $this->_response;
133  }
134 
135  return parent::aroundDispatch($subject, $proceed, $request);
136  }
137 }
$response
Definition: 404.php:11
aroundDispatch(AbstractAction $subject, \Closure $proceed, RequestInterface $request)
$resource
Definition: bulk.php:12
$type
Definition: item.phtml:13
__construct(\Magento\Backend\Model\Auth $auth, \Magento\Backend\Model\UrlInterface $url, ResponseInterface $response, \Magento\Framework\App\ActionFlag $actionFlag, \Magento\Framework\Message\ManagerInterface $messageManager, \Magento\Backend\Model\UrlInterface $backendUrl, \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory, \Magento\Backend\App\BackendAppList $backendAppList, \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator, \Magento\Framework\HTTP\Authentication $httpAuthentication, \Psr\Log\LoggerInterface $logger, \Magento\Framework\AuthorizationInterface $authorization, array $aclResources)