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