PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/Backend/App/AbstractAction.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 361 lines | 171 code | 38 blank | 152 comment | 23 complexity | 2658e632dc2351e441e8d682051965fb MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\App;
  7. /**
  8. * Generic backend controller
  9. *
  10. * @SuppressWarnings(PHPMD.NumberOfChildren)
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. abstract class AbstractAction extends \Magento\Framework\App\Action\Action
  14. {
  15. /**
  16. * Name of "is URLs checked" flag
  17. */
  18. const FLAG_IS_URLS_CHECKED = 'check_url_settings';
  19. /**
  20. * Session namespace to refer in other places
  21. */
  22. const SESSION_NAMESPACE = 'adminhtml';
  23. /**
  24. * Authorization level of a basic admin session
  25. */
  26. const ADMIN_RESOURCE = 'Magento_Backend::admin';
  27. /**
  28. * Array of actions which can be processed without secret key validation
  29. *
  30. * @var array
  31. */
  32. protected $_publicActions = [];
  33. /**
  34. * Namespace for session.
  35. *
  36. * @var string
  37. */
  38. protected $_sessionNamespace = self::SESSION_NAMESPACE;
  39. /**
  40. * @var \Magento\Backend\Helper\Data
  41. */
  42. protected $_helper;
  43. /**
  44. * @var \Magento\Backend\Model\Session
  45. */
  46. protected $_session;
  47. /**
  48. * @var \Magento\Framework\AuthorizationInterface
  49. */
  50. protected $_authorization;
  51. /**
  52. * @var \Magento\Backend\Model\Auth
  53. */
  54. protected $_auth;
  55. /**
  56. * @var \Magento\Backend\Model\UrlInterface
  57. */
  58. protected $_backendUrl;
  59. /**
  60. * @var \Magento\Framework\Locale\ResolverInterface
  61. */
  62. protected $_localeResolver;
  63. /**
  64. * @var bool
  65. */
  66. protected $_canUseBaseUrl;
  67. /**
  68. * @var \Magento\Framework\Data\Form\FormKey\Validator
  69. */
  70. protected $_formKeyValidator;
  71. /**
  72. * @param \Magento\Backend\App\Action\Context $context
  73. */
  74. public function __construct(Action\Context $context)
  75. {
  76. parent::__construct($context);
  77. $this->_authorization = $context->getAuthorization();
  78. $this->_auth = $context->getAuth();
  79. $this->_helper = $context->getHelper();
  80. $this->_backendUrl = $context->getBackendUrl();
  81. $this->_formKeyValidator = $context->getFormKeyValidator();
  82. $this->_localeResolver = $context->getLocaleResolver();
  83. $this->_canUseBaseUrl = $context->getCanUseBaseUrl();
  84. $this->_session = $context->getSession();
  85. }
  86. /**
  87. * @return bool
  88. */
  89. protected function _isAllowed()
  90. {
  91. return $this->_authorization->isAllowed(static::ADMIN_RESOURCE);
  92. }
  93. /**
  94. * Retrieve adminhtml session model object
  95. *
  96. * @return \Magento\Backend\Model\Session
  97. */
  98. protected function _getSession()
  99. {
  100. return $this->_session;
  101. }
  102. /**
  103. * @return \Magento\Framework\Message\ManagerInterface
  104. */
  105. protected function getMessageManager()
  106. {
  107. return $this->messageManager;
  108. }
  109. /**
  110. * Define active menu item in menu block
  111. *
  112. * @param string $itemId current active menu item
  113. * @return $this
  114. */
  115. protected function _setActiveMenu($itemId)
  116. {
  117. /** @var $menuBlock \Magento\Backend\Block\Menu */
  118. $menuBlock = $this->_view->getLayout()->getBlock('menu');
  119. $menuBlock->setActive($itemId);
  120. $parents = $menuBlock->getMenuModel()->getParentItems($itemId);
  121. foreach ($parents as $item) {
  122. /** @var $item \Magento\Backend\Model\Menu\Item */
  123. $this->_view->getPage()->getConfig()->getTitle()->prepend($item->getTitle());
  124. }
  125. return $this;
  126. }
  127. /**
  128. * @param string $label
  129. * @param string $title
  130. * @param string|null $link
  131. * @return $this
  132. */
  133. protected function _addBreadcrumb($label, $title, $link = null)
  134. {
  135. $this->_view->getLayout()->getBlock('breadcrumbs')->addLink($label, $title, $link);
  136. return $this;
  137. }
  138. /**
  139. * @param \Magento\Framework\View\Element\AbstractBlock $block
  140. * @return $this
  141. */
  142. protected function _addContent(\Magento\Framework\View\Element\AbstractBlock $block)
  143. {
  144. return $this->_moveBlockToContainer($block, 'content');
  145. }
  146. /**
  147. * @param \Magento\Framework\View\Element\AbstractBlock $block
  148. * @return $this
  149. */
  150. protected function _addLeft(\Magento\Framework\View\Element\AbstractBlock $block)
  151. {
  152. return $this->_moveBlockToContainer($block, 'left');
  153. }
  154. /**
  155. * @param \Magento\Framework\View\Element\AbstractBlock $block
  156. * @return $this
  157. */
  158. protected function _addJs(\Magento\Framework\View\Element\AbstractBlock $block)
  159. {
  160. return $this->_moveBlockToContainer($block, 'js');
  161. }
  162. /**
  163. * Set specified block as an anonymous child to specified container
  164. *
  165. * The block will be moved to the container from previous parent after all other elements
  166. *
  167. * @param \Magento\Framework\View\Element\AbstractBlock $block
  168. * @param string $containerName
  169. * @return $this
  170. */
  171. private function _moveBlockToContainer(\Magento\Framework\View\Element\AbstractBlock $block, $containerName)
  172. {
  173. $this->_view->getLayout()->setChild($containerName, $block->getNameInLayout(), '');
  174. return $this;
  175. }
  176. /**
  177. * @param \Magento\Framework\App\RequestInterface $request
  178. * @return \Magento\Framework\App\ResponseInterface
  179. */
  180. public function dispatch(\Magento\Framework\App\RequestInterface $request)
  181. {
  182. if (!$this->_processUrlKeys()) {
  183. return parent::dispatch($request);
  184. }
  185. if ($request->isDispatched() && $request->getActionName() !== 'denied' && !$this->_isAllowed()) {
  186. $this->_response->setStatusHeader(403, '1.1', 'Forbidden');
  187. if (!$this->_auth->isLoggedIn()) {
  188. return $this->_redirect('*/auth/login');
  189. }
  190. $this->_view->loadLayout(['default', 'adminhtml_denied'], true, true, false);
  191. $this->_view->renderLayout();
  192. $this->_request->setDispatched(true);
  193. return $this->_response;
  194. }
  195. if ($this->_isUrlChecked()) {
  196. $this->_actionFlag->set('', self::FLAG_IS_URLS_CHECKED, true);
  197. }
  198. $this->_processLocaleSettings();
  199. return parent::dispatch($request);
  200. }
  201. /**
  202. * Check whether url is checked
  203. *
  204. * @return bool
  205. */
  206. protected function _isUrlChecked()
  207. {
  208. return !$this->_actionFlag->get('', self::FLAG_IS_URLS_CHECKED)
  209. && !$this->getRequest()->isForwarded()
  210. && !$this->_getSession()->getIsUrlNotice(true)
  211. && !$this->_canUseBaseUrl;
  212. }
  213. /**
  214. * Check url keys. If non valid - redirect
  215. *
  216. * @return bool
  217. */
  218. public function _processUrlKeys()
  219. {
  220. $_isValidFormKey = true;
  221. $_isValidSecretKey = true;
  222. $_keyErrorMsg = '';
  223. if ($this->_auth->isLoggedIn()) {
  224. if ($this->getRequest()->isPost()) {
  225. $_isValidFormKey = $this->_formKeyValidator->validate($this->getRequest());
  226. $_keyErrorMsg = __('Invalid Form Key. Please refresh the page.');
  227. } elseif ($this->_backendUrl->useSecretKey()) {
  228. $_isValidSecretKey = $this->_validateSecretKey();
  229. $_keyErrorMsg = __('You entered an invalid Secret Key. Please refresh the page.');
  230. }
  231. }
  232. if (!$_isValidFormKey || !$_isValidSecretKey) {
  233. $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
  234. $this->_actionFlag->set('', self::FLAG_NO_POST_DISPATCH, true);
  235. if ($this->getRequest()->getQuery('isAjax', false) || $this->getRequest()->getQuery('ajax', false)) {
  236. $this->getResponse()->representJson(
  237. $this->_objectManager->get(
  238. \Magento\Framework\Json\Helper\Data::class
  239. )->jsonEncode(
  240. ['error' => true, 'message' => $_keyErrorMsg]
  241. )
  242. );
  243. } else {
  244. $this->_redirect($this->_backendUrl->getStartupPageUrl());
  245. }
  246. return false;
  247. }
  248. return true;
  249. }
  250. /**
  251. * Set session locale,
  252. * process force locale set through url params
  253. *
  254. * @return $this
  255. */
  256. protected function _processLocaleSettings()
  257. {
  258. $forceLocale = $this->getRequest()->getParam('locale', null);
  259. if ($this->_objectManager->get(\Magento\Framework\Validator\Locale::class)->isValid($forceLocale)) {
  260. $this->_getSession()->setSessionLocale($forceLocale);
  261. }
  262. if ($this->_getSession()->getLocale() === null) {
  263. $this->_getSession()->setLocale($this->_localeResolver->getLocale());
  264. }
  265. return $this;
  266. }
  267. /**
  268. * Set redirect into response
  269. *
  270. * @TODO MAGETWO-28356: Refactor controller actions to new ResultInterface
  271. * @param string $path
  272. * @param array $arguments
  273. * @return \Magento\Framework\App\ResponseInterface
  274. */
  275. protected function _redirect($path, $arguments = [])
  276. {
  277. $this->_getSession()->setIsUrlNotice($this->_actionFlag->get('', self::FLAG_IS_URLS_CHECKED));
  278. $this->getResponse()->setRedirect($this->getUrl($path, $arguments));
  279. return $this->getResponse();
  280. }
  281. /**
  282. * Forward to action
  283. *
  284. * @TODO MAGETWO-28356: Refactor controller actions to new ResultInterface
  285. * @param string $action
  286. * @param string|null $controller
  287. * @param string|null $module
  288. * @param array|null $params
  289. * @return void
  290. */
  291. protected function _forward($action, $controller = null, $module = null, array $params = null)
  292. {
  293. $this->_getSession()->setIsUrlNotice($this->_actionFlag->get('', self::FLAG_IS_URLS_CHECKED));
  294. return parent::_forward($action, $controller, $module, $params);
  295. }
  296. /**
  297. * Generate url by route and parameters
  298. *
  299. * @param string $route
  300. * @param array $params
  301. * @return string
  302. */
  303. public function getUrl($route = '', $params = [])
  304. {
  305. return $this->_helper->getUrl($route, $params);
  306. }
  307. /**
  308. * Validate Secret Key
  309. *
  310. * @return bool
  311. */
  312. protected function _validateSecretKey()
  313. {
  314. if (is_array($this->_publicActions) && in_array($this->getRequest()->getActionName(), $this->_publicActions)) {
  315. return true;
  316. }
  317. $secretKey = $this->getRequest()->getParam(\Magento\Backend\Model\UrlInterface::SECRET_KEY_PARAM_NAME, null);
  318. if (!$secretKey || $secretKey != $this->_backendUrl->getSecretKey()) {
  319. return false;
  320. }
  321. return true;
  322. }
  323. }