Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractAction.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Backend\App;
7 
17 {
21  const FLAG_IS_URLS_CHECKED = 'check_url_settings';
22 
26  const SESSION_NAMESPACE = 'adminhtml';
27 
31  const ADMIN_RESOURCE = 'Magento_Backend::admin';
32 
38  protected $_publicActions = [];
39 
46 
50  protected $_helper;
51 
55  protected $_session;
56 
60  protected $_authorization;
61 
65  protected $_auth;
66 
70  protected $_backendUrl;
71 
75  protected $_localeResolver;
76 
80  protected $_canUseBaseUrl;
81 
85  protected $_formKeyValidator;
86 
90  public function __construct(Action\Context $context)
91  {
92  parent::__construct($context);
93  $this->_authorization = $context->getAuthorization();
94  $this->_auth = $context->getAuth();
95  $this->_helper = $context->getHelper();
96  $this->_backendUrl = $context->getBackendUrl();
97  $this->_formKeyValidator = $context->getFormKeyValidator();
98  $this->_localeResolver = $context->getLocaleResolver();
99  $this->_canUseBaseUrl = $context->getCanUseBaseUrl();
100  $this->_session = $context->getSession();
101  }
102 
106  protected function _isAllowed()
107  {
108  return $this->_authorization->isAllowed(static::ADMIN_RESOURCE);
109  }
110 
116  protected function _getSession()
117  {
118  return $this->_session;
119  }
120 
124  protected function getMessageManager()
125  {
126  return $this->messageManager;
127  }
128 
135  protected function _setActiveMenu($itemId)
136  {
138  $menuBlock = $this->_view->getLayout()->getBlock('menu');
139  $menuBlock->setActive($itemId);
140  $parents = $menuBlock->getMenuModel()->getParentItems($itemId);
141  foreach ($parents as $item) {
143  $this->_view->getPage()->getConfig()->getTitle()->prepend($item->getTitle());
144  }
145  return $this;
146  }
147 
154  protected function _addBreadcrumb($label, $title, $link = null)
155  {
156  $this->_view->getLayout()->getBlock('breadcrumbs')->addLink($label, $title, $link);
157  return $this;
158  }
159 
164  protected function _addContent(\Magento\Framework\View\Element\AbstractBlock $block)
165  {
166  return $this->_moveBlockToContainer($block, 'content');
167  }
168 
173  protected function _addLeft(\Magento\Framework\View\Element\AbstractBlock $block)
174  {
175  return $this->_moveBlockToContainer($block, 'left');
176  }
177 
182  protected function _addJs(\Magento\Framework\View\Element\AbstractBlock $block)
183  {
184  return $this->_moveBlockToContainer($block, 'js');
185  }
186 
196  private function _moveBlockToContainer(\Magento\Framework\View\Element\AbstractBlock $block, $containerName)
197  {
198  $this->_view->getLayout()->setChild($containerName, $block->getNameInLayout(), '');
199  return $this;
200  }
201 
206  public function dispatch(\Magento\Framework\App\RequestInterface $request)
207  {
208  if ($request->isDispatched() && $request->getActionName() !== 'denied' && !$this->_isAllowed()) {
209  $this->_response->setStatusHeader(403, '1.1', 'Forbidden');
210  if (!$this->_auth->isLoggedIn()) {
211  return $this->_redirect('*/auth/login');
212  }
213  $this->_view->loadLayout(['default', 'adminhtml_denied'], true, true, false);
214  $this->_view->renderLayout();
215  $this->_request->setDispatched(true);
216 
217  return $this->_response;
218  }
219 
220  if ($this->_isUrlChecked()) {
221  $this->_actionFlag->set('', self::FLAG_IS_URLS_CHECKED, true);
222  }
223 
224  $this->_processLocaleSettings();
225 
226  // Need to preload isFirstPageAfterLogin (see https://github.com/magento/magento2/issues/15510)
227  if ($this->_auth->isLoggedIn()) {
228  $this->_auth->getAuthStorage()->isFirstPageAfterLogin();
229  }
230 
231  return parent::dispatch($request);
232  }
233 
239  protected function _isUrlChecked()
240  {
241  return !$this->_actionFlag->get('', self::FLAG_IS_URLS_CHECKED)
242  && !$this->getRequest()->isForwarded()
243  && !$this->_getSession()->getIsUrlNotice(true)
245  }
246 
255  public function _processUrlKeys()
256  {
257  $_isValidFormKey = true;
258  $_isValidSecretKey = true;
259  $_keyErrorMsg = '';
260  if ($this->_auth->isLoggedIn()) {
261  if ($this->getRequest()->isPost()) {
262  $_isValidFormKey = $this->_formKeyValidator->validate($this->getRequest());
263  $_keyErrorMsg = __('Invalid Form Key. Please refresh the page.');
264  } elseif ($this->_backendUrl->useSecretKey()) {
265  $_isValidSecretKey = $this->_validateSecretKey();
266  $_keyErrorMsg = __('You entered an invalid Secret Key. Please refresh the page.');
267  }
268  }
269  if (!$_isValidFormKey || !$_isValidSecretKey) {
270  $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
271  $this->_actionFlag->set('', self::FLAG_NO_POST_DISPATCH, true);
272  if ($this->getRequest()->getQuery('isAjax', false) || $this->getRequest()->getQuery('ajax', false)) {
273  $this->getResponse()->representJson(
274  $this->_objectManager->get(
275  \Magento\Framework\Json\Helper\Data::class
276  )->jsonEncode(
277  ['error' => true, 'message' => $_keyErrorMsg]
278  )
279  );
280  } else {
281  $this->_redirect($this->_backendUrl->getStartupPageUrl());
282  }
283  return false;
284  }
285  return true;
286  }
287 
294  protected function _processLocaleSettings()
295  {
296  $forceLocale = $this->getRequest()->getParam('locale', null);
297  if ($this->_objectManager->get(\Magento\Framework\Validator\Locale::class)->isValid($forceLocale)) {
298  $this->_getSession()->setSessionLocale($forceLocale);
299  }
300 
301  if ($this->_getSession()->getLocale() === null) {
302  $this->_getSession()->setLocale($this->_localeResolver->getLocale());
303  }
304 
305  return $this;
306  }
307 
316  protected function _redirect($path, $arguments = [])
317  {
318  $this->_getSession()->setIsUrlNotice($this->_actionFlag->get('', self::FLAG_IS_URLS_CHECKED));
319  $this->getResponse()->setRedirect($this->getUrl($path, $arguments));
320  return $this->getResponse();
321  }
322 
333  protected function _forward($action, $controller = null, $module = null, array $params = null)
334  {
335  $this->_getSession()->setIsUrlNotice($this->_actionFlag->get('', self::FLAG_IS_URLS_CHECKED));
336  return parent::_forward($action, $controller, $module, $params);
337  }
338 
346  public function getUrl($route = '', $params = [])
347  {
348  return $this->_helper->getUrl($route, $params);
349  }
350 
356  protected function _validateSecretKey()
357  {
358  if (is_array($this->_publicActions) && in_array($this->getRequest()->getActionName(), $this->_publicActions)) {
359  return true;
360  }
361 
362  $secretKey = $this->getRequest()->getParam(\Magento\Backend\Model\UrlInterface::SECRET_KEY_PARAM_NAME, null);
363  if (!$secretKey || $secretKey != $this->_backendUrl->getSecretKey()) {
364  return false;
365  }
366  return true;
367  }
368 }
$title
Definition: default.phtml:14
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
_addJs(\Magento\Framework\View\Element\AbstractBlock $block)
__()
Definition: __.php:13
$block
Definition: block.php:8
$label
Definition: details.phtml:21
__construct(Action\Context $context)
_addContent(\Magento\Framework\View\Element\AbstractBlock $block)
_addBreadcrumb($label, $title, $link=null)
$arguments
_forward($action, $controller=null, $module=null, array $params=null)
dispatch(\Magento\Framework\App\RequestInterface $request)
$controller
Definition: info.phtml:14
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
_addLeft(\Magento\Framework\View\Element\AbstractBlock $block)