Skip to content

Commit a74f4a5

Browse files
authored
ENGCOM-6882: Eliminate the need for inheritance for action controllers. #26778
2 parents 24c2169 + b5e47d8 commit a74f4a5

File tree

44 files changed

+1630
-785
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1630
-785
lines changed

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

Lines changed: 138 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,24 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Backend\App;
78

9+
use Magento\Backend\App\Action\Context;
10+
use Magento\Backend\Helper\Data as BackendHelper;
11+
use Magento\Backend\Model\Auth;
12+
use Magento\Backend\Model\Session;
13+
use Magento\Backend\Model\UrlInterface;
14+
use Magento\Framework\App\RequestInterface;
15+
use Magento\Framework\AuthorizationInterface;
16+
use Magento\Framework\Data\Form\FormKey\Validator as FormKeyValidator;
17+
use Magento\Framework\Locale\ResolverInterface;
18+
use Magento\Framework\View\Element\AbstractBlock;
19+
820
/**
921
* Generic backend controller
1022
*
23+
* phpcs:disable Magento2.Classes.AbstractApi
1124
* @api
1225
* @SuppressWarnings(PHPMD.NumberOfChildren)
1326
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -45,32 +58,32 @@ abstract class AbstractAction extends \Magento\Framework\App\Action\Action
4558
protected $_sessionNamespace = self::SESSION_NAMESPACE;
4659

4760
/**
48-
* @var \Magento\Backend\Helper\Data
61+
* @var BackendHelper
4962
*/
5063
protected $_helper;
5164

5265
/**
53-
* @var \Magento\Backend\Model\Session
66+
* @var Session
5467
*/
5568
protected $_session;
5669

5770
/**
58-
* @var \Magento\Framework\AuthorizationInterface
71+
* @var AuthorizationInterface
5972
*/
6073
protected $_authorization;
6174

6275
/**
63-
* @var \Magento\Backend\Model\Auth
76+
* @var Auth
6477
*/
6578
protected $_auth;
6679

6780
/**
68-
* @var \Magento\Backend\Model\UrlInterface
81+
* @var UrlInterface
6982
*/
7083
protected $_backendUrl;
7184

7285
/**
73-
* @var \Magento\Framework\Locale\ResolverInterface
86+
* @var ResolverInterface
7487
*/
7588
protected $_localeResolver;
7689

@@ -80,14 +93,14 @@ abstract class AbstractAction extends \Magento\Framework\App\Action\Action
8093
protected $_canUseBaseUrl;
8194

8295
/**
83-
* @var \Magento\Framework\Data\Form\FormKey\Validator
96+
* @var FormKeyValidator
8497
*/
8598
protected $_formKeyValidator;
8699

87100
/**
88-
* @param \Magento\Backend\App\Action\Context $context
101+
* @param Context $context
89102
*/
90-
public function __construct(Action\Context $context)
103+
public function __construct(Context $context)
91104
{
92105
parent::__construct($context);
93106
$this->_authorization = $context->getAuthorization();
@@ -101,6 +114,95 @@ public function __construct(Action\Context $context)
101114
}
102115

103116
/**
117+
* Dispatches the Action
118+
*
119+
* @param RequestInterface $request
120+
* @return \Magento\Framework\App\ResponseInterface
121+
*/
122+
public function dispatch(RequestInterface $request)
123+
{
124+
if ($request->isDispatched() && $request->getActionName() !== 'denied' && !$this->_isAllowed()) {
125+
$this->_response->setStatusHeader(403, '1.1', 'Forbidden');
126+
if (!$this->_auth->isLoggedIn()) {
127+
return $this->_redirect('*/auth/login');
128+
}
129+
130+
$this->_view->loadLayout(['default', 'adminhtml_denied'], true, true, false);
131+
$this->_view->renderLayout();
132+
$this->_request->setDispatched(true);
133+
134+
return $this->_response;
135+
}
136+
137+
if ($this->_isUrlChecked()) {
138+
$this->_actionFlag->set('', self::FLAG_IS_URLS_CHECKED, true);
139+
}
140+
141+
$this->_processLocaleSettings();
142+
143+
// Need to preload isFirstPageAfterLogin (see https://github.com/magento/magento2/issues/15510)
144+
if ($this->_auth->isLoggedIn()) {
145+
$this->_auth->getAuthStorage()->isFirstPageAfterLogin();
146+
}
147+
148+
return parent::dispatch($request);
149+
}
150+
151+
/**
152+
* Check url keys. If non valid - redirect
153+
*
154+
* @return bool
155+
*
156+
* @see \Magento\Backend\App\Request\BackendValidator for default request validation.
157+
*/
158+
public function _processUrlKeys()
159+
{
160+
$_isValidFormKey = true;
161+
$_isValidSecretKey = true;
162+
$_keyErrorMsg = '';
163+
if ($this->_auth->isLoggedIn()) {
164+
if ($this->getRequest()->isPost()) {
165+
$_isValidFormKey = $this->_formKeyValidator->validate($this->getRequest());
166+
$_keyErrorMsg = __('Invalid Form Key. Please refresh the page.');
167+
} elseif ($this->_backendUrl->useSecretKey()) {
168+
$_isValidSecretKey = $this->_validateSecretKey();
169+
$_keyErrorMsg = __('You entered an invalid Secret Key. Please refresh the page.');
170+
}
171+
}
172+
if (!$_isValidFormKey || !$_isValidSecretKey) {
173+
$this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
174+
$this->_actionFlag->set('', self::FLAG_NO_POST_DISPATCH, true);
175+
if ($this->getRequest()->getQuery('isAjax', false) || $this->getRequest()->getQuery('ajax', false)) {
176+
$this->getResponse()->representJson(
177+
$this->_objectManager->get(
178+
\Magento\Framework\Json\Helper\Data::class
179+
)->jsonEncode(
180+
['error' => true, 'message' => $_keyErrorMsg]
181+
)
182+
);
183+
} else {
184+
$this->_redirect($this->_backendUrl->getStartupPageUrl());
185+
}
186+
return false;
187+
}
188+
return true;
189+
}
190+
191+
/**
192+
* Generate url by route and parameters
193+
*
194+
* @param string $route
195+
* @param array $params
196+
* @return string
197+
*/
198+
public function getUrl($route = '', $params = [])
199+
{
200+
return $this->_helper->getUrl($route, $params);
201+
}
202+
203+
/**
204+
* Determines whether current user is allowed to access Action
205+
*
104206
* @return bool
105207
*/
106208
protected function _isAllowed()
@@ -119,6 +221,8 @@ protected function _getSession()
119221
}
120222

121223
/**
224+
* Returns instantiated Message\ManagerInterface.
225+
*
122226
* @return \Magento\Framework\Message\ManagerInterface
123227
*/
124228
protected function getMessageManager()
@@ -146,6 +250,8 @@ protected function _setActiveMenu($itemId)
146250
}
147251

148252
/**
253+
* Adds element to Breadcrumbs block
254+
*
149255
* @param string $label
150256
* @param string $title
151257
* @param string|null $link
@@ -158,79 +264,51 @@ protected function _addBreadcrumb($label, $title, $link = null)
158264
}
159265

160266
/**
161-
* @param \Magento\Framework\View\Element\AbstractBlock $block
267+
* Adds block to `content` block
268+
*
269+
* @param AbstractBlock $block
162270
* @return $this
163271
*/
164-
protected function _addContent(\Magento\Framework\View\Element\AbstractBlock $block)
272+
protected function _addContent(AbstractBlock $block)
165273
{
166274
return $this->_moveBlockToContainer($block, 'content');
167275
}
168276

169277
/**
170-
* @param \Magento\Framework\View\Element\AbstractBlock $block
278+
* Moves Block to `left` container
279+
*
280+
* @param AbstractBlock $block
171281
* @return $this
172282
*/
173-
protected function _addLeft(\Magento\Framework\View\Element\AbstractBlock $block)
283+
protected function _addLeft(AbstractBlock $block)
174284
{
175285
return $this->_moveBlockToContainer($block, 'left');
176286
}
177287

178288
/**
179-
* @param \Magento\Framework\View\Element\AbstractBlock $block
289+
* Adds Block to `js` container
290+
*
291+
* @param AbstractBlock $block
180292
* @return $this
181293
*/
182-
protected function _addJs(\Magento\Framework\View\Element\AbstractBlock $block)
294+
protected function _addJs(AbstractBlock $block)
183295
{
184296
return $this->_moveBlockToContainer($block, 'js');
185297
}
186298

187299
/**
188-
* Set specified block as an anonymous child to specified container
189-
*
190-
* The block will be moved to the container from previous parent after all other elements
300+
* Set specified block as an anonymous child to specified container.
191301
*
192-
* @param \Magento\Framework\View\Element\AbstractBlock $block
302+
* @param AbstractBlock $block
193303
* @param string $containerName
194304
* @return $this
195305
*/
196-
private function _moveBlockToContainer(\Magento\Framework\View\Element\AbstractBlock $block, $containerName)
306+
private function _moveBlockToContainer(AbstractBlock $block, $containerName)
197307
{
198308
$this->_view->getLayout()->setChild($containerName, $block->getNameInLayout(), '');
199309
return $this;
200310
}
201311

202-
/**
203-
* @param \Magento\Framework\App\RequestInterface $request
204-
* @return \Magento\Framework\App\ResponseInterface
205-
*/
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-
234312
/**
235313
* Check whether url is checked
236314
*
@@ -239,55 +317,13 @@ public function dispatch(\Magento\Framework\App\RequestInterface $request)
239317
protected function _isUrlChecked()
240318
{
241319
return !$this->_actionFlag->get('', self::FLAG_IS_URLS_CHECKED)
242-
&& !$this->getRequest()->isForwarded()
243-
&& !$this->_getSession()->getIsUrlNotice(true)
244-
&& !$this->_canUseBaseUrl;
320+
&& !$this->getRequest()->isForwarded()
321+
&& !$this->_getSession()->getIsUrlNotice(true)
322+
&& !$this->_canUseBaseUrl;
245323
}
246324

247325
/**
248-
* Check url keys. If non valid - redirect
249-
*
250-
* @return bool
251-
*
252-
* @see \Magento\Backend\App\Request\BackendValidator for default
253-
* request validation.
254-
*/
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-
288-
/**
289-
* Set session locale,
290-
* process force locale set through url params
326+
* Set session locale, process force locale set through url params
291327
*
292328
* @return $this
293329
*/
@@ -309,8 +345,8 @@ protected function _processLocaleSettings()
309345
* Set redirect into response
310346
*
311347
* @TODO MAGETWO-28356: Refactor controller actions to new ResultInterface
312-
* @param string $path
313-
* @param array $arguments
348+
* @param string $path
349+
* @param array $arguments
314350
* @return \Magento\Framework\App\ResponseInterface
315351
*/
316352
protected function _redirect($path, $arguments = [])
@@ -333,19 +369,7 @@ protected function _redirect($path, $arguments = [])
333369
protected function _forward($action, $controller = null, $module = null, array $params = null)
334370
{
335371
$this->_getSession()->setIsUrlNotice($this->_actionFlag->get('', self::FLAG_IS_URLS_CHECKED));
336-
return parent::_forward($action, $controller, $module, $params);
337-
}
338-
339-
/**
340-
* Generate url by route and parameters
341-
*
342-
* @param string $route
343-
* @param array $params
344-
* @return string
345-
*/
346-
public function getUrl($route = '', $params = [])
347-
{
348-
return $this->_helper->getUrl($route, $params);
372+
parent::_forward($action, $controller, $module, $params);
349373
}
350374

351375
/**
@@ -359,7 +383,7 @@ protected function _validateSecretKey()
359383
return true;
360384
}
361385

362-
$secretKey = $this->getRequest()->getParam(\Magento\Backend\Model\UrlInterface::SECRET_KEY_PARAM_NAME, null);
386+
$secretKey = $this->getRequest()->getParam(UrlInterface::SECRET_KEY_PARAM_NAME, null);
363387
if (!$secretKey || $secretKey != $this->_backendUrl->getSecretKey()) {
364388
return false;
365389
}

0 commit comments

Comments
 (0)