3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \Backend \App ;
7
8
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
+
8
20
/**
9
21
* Generic backend controller
10
22
*
23
+ * @deprecated Use \Magento\Framework\App\ActionInterface
24
+ *
25
+ * phpcs:disable Magento2.Classes.AbstractApi
11
26
* @api
12
27
* @SuppressWarnings(PHPMD.NumberOfChildren)
13
28
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -45,32 +60,32 @@ abstract class AbstractAction extends \Magento\Framework\App\Action\Action
45
60
protected $ _sessionNamespace = self ::SESSION_NAMESPACE ;
46
61
47
62
/**
48
- * @var \Magento\Backend\Helper\Data
63
+ * @var BackendHelper
49
64
*/
50
65
protected $ _helper ;
51
66
52
67
/**
53
- * @var \Magento\Backend\Model\ Session
68
+ * @var Session
54
69
*/
55
70
protected $ _session ;
56
71
57
72
/**
58
- * @var \Magento\Framework\ AuthorizationInterface
73
+ * @var AuthorizationInterface
59
74
*/
60
75
protected $ _authorization ;
61
76
62
77
/**
63
- * @var \Magento\Backend\Model\ Auth
78
+ * @var Auth
64
79
*/
65
80
protected $ _auth ;
66
81
67
82
/**
68
- * @var \Magento\Backend\Model\ UrlInterface
83
+ * @var UrlInterface
69
84
*/
70
85
protected $ _backendUrl ;
71
86
72
87
/**
73
- * @var \Magento\Framework\Locale\ ResolverInterface
88
+ * @var ResolverInterface
74
89
*/
75
90
protected $ _localeResolver ;
76
91
@@ -80,14 +95,14 @@ abstract class AbstractAction extends \Magento\Framework\App\Action\Action
80
95
protected $ _canUseBaseUrl ;
81
96
82
97
/**
83
- * @var \Magento\Framework\Data\Form\FormKey\Validator
98
+ * @var FormKeyValidator
84
99
*/
85
100
protected $ _formKeyValidator ;
86
101
87
102
/**
88
- * @param \Magento\Backend\App\Action\ Context $context
103
+ * @param Context $context
89
104
*/
90
- public function __construct (Action \ Context $ context )
105
+ public function __construct (Context $ context )
91
106
{
92
107
parent ::__construct ($ context );
93
108
$ this ->_authorization = $ context ->getAuthorization ();
@@ -101,6 +116,95 @@ public function __construct(Action\Context $context)
101
116
}
102
117
103
118
/**
119
+ * Dispatches the Action
120
+ *
121
+ * @param RequestInterface $request
122
+ * @return \Magento\Framework\App\ResponseInterface
123
+ */
124
+ public function dispatch (RequestInterface $ request )
125
+ {
126
+ if ($ request ->isDispatched () && $ request ->getActionName () !== 'denied ' && !$ this ->_isAllowed ()) {
127
+ $ this ->_response ->setStatusHeader (403 , '1.1 ' , 'Forbidden ' );
128
+ if (!$ this ->_auth ->isLoggedIn ()) {
129
+ return $ this ->_redirect ('*/auth/login ' );
130
+ }
131
+
132
+ $ this ->_view ->loadLayout (['default ' , 'adminhtml_denied ' ], true , true , false );
133
+ $ this ->_view ->renderLayout ();
134
+ $ this ->_request ->setDispatched (true );
135
+
136
+ return $ this ->_response ;
137
+ }
138
+
139
+ if ($ this ->_isUrlChecked ()) {
140
+ $ this ->_actionFlag ->set ('' , self ::FLAG_IS_URLS_CHECKED , true );
141
+ }
142
+
143
+ $ this ->_processLocaleSettings ();
144
+
145
+ // Need to preload isFirstPageAfterLogin (see https://github.com/magento/magento2/issues/15510)
146
+ if ($ this ->_auth ->isLoggedIn ()) {
147
+ $ this ->_auth ->getAuthStorage ()->isFirstPageAfterLogin ();
148
+ }
149
+
150
+ return parent ::dispatch ($ request );
151
+ }
152
+
153
+ /**
154
+ * Check url keys. If non valid - redirect
155
+ *
156
+ * @return bool
157
+ *
158
+ * @see \Magento\Backend\App\Request\BackendValidator for default request validation.
159
+ */
160
+ public function _processUrlKeys ()
161
+ {
162
+ $ _isValidFormKey = true ;
163
+ $ _isValidSecretKey = true ;
164
+ $ _keyErrorMsg = '' ;
165
+ if ($ this ->_auth ->isLoggedIn ()) {
166
+ if ($ this ->getRequest ()->isPost ()) {
167
+ $ _isValidFormKey = $ this ->_formKeyValidator ->validate ($ this ->getRequest ());
168
+ $ _keyErrorMsg = __ ('Invalid Form Key. Please refresh the page. ' );
169
+ } elseif ($ this ->_backendUrl ->useSecretKey ()) {
170
+ $ _isValidSecretKey = $ this ->_validateSecretKey ();
171
+ $ _keyErrorMsg = __ ('You entered an invalid Secret Key. Please refresh the page. ' );
172
+ }
173
+ }
174
+ if (!$ _isValidFormKey || !$ _isValidSecretKey ) {
175
+ $ this ->_actionFlag ->set ('' , self ::FLAG_NO_DISPATCH , true );
176
+ $ this ->_actionFlag ->set ('' , self ::FLAG_NO_POST_DISPATCH , true );
177
+ if ($ this ->getRequest ()->getQuery ('isAjax ' , false ) || $ this ->getRequest ()->getQuery ('ajax ' , false )) {
178
+ $ this ->getResponse ()->representJson (
179
+ $ this ->_objectManager ->get (
180
+ \Magento \Framework \Json \Helper \Data::class
181
+ )->jsonEncode (
182
+ ['error ' => true , 'message ' => $ _keyErrorMsg ]
183
+ )
184
+ );
185
+ } else {
186
+ $ this ->_redirect ($ this ->_backendUrl ->getStartupPageUrl ());
187
+ }
188
+ return false ;
189
+ }
190
+ return true ;
191
+ }
192
+
193
+ /**
194
+ * Generate url by route and parameters
195
+ *
196
+ * @param string $route
197
+ * @param array $params
198
+ * @return string
199
+ */
200
+ public function getUrl ($ route = '' , $ params = [])
201
+ {
202
+ return $ this ->_helper ->getUrl ($ route , $ params );
203
+ }
204
+
205
+ /**
206
+ * Determines whether current user is allowed to access Action
207
+ *
104
208
* @return bool
105
209
*/
106
210
protected function _isAllowed ()
@@ -119,6 +223,8 @@ protected function _getSession()
119
223
}
120
224
121
225
/**
226
+ * Returns instantiated Message\ManagerInterface.
227
+ *
122
228
* @return \Magento\Framework\Message\ManagerInterface
123
229
*/
124
230
protected function getMessageManager ()
@@ -146,6 +252,8 @@ protected function _setActiveMenu($itemId)
146
252
}
147
253
148
254
/**
255
+ * Adds element to Breadcrumbs block
256
+ *
149
257
* @param string $label
150
258
* @param string $title
151
259
* @param string|null $link
@@ -158,79 +266,51 @@ protected function _addBreadcrumb($label, $title, $link = null)
158
266
}
159
267
160
268
/**
161
- * @param \Magento\Framework\View\Element\AbstractBlock $block
269
+ * Adds block to `content` block
270
+ *
271
+ * @param AbstractBlock $block
162
272
* @return $this
163
273
*/
164
- protected function _addContent (\ Magento \ Framework \ View \ Element \ AbstractBlock $ block )
274
+ protected function _addContent (AbstractBlock $ block )
165
275
{
166
276
return $ this ->_moveBlockToContainer ($ block , 'content ' );
167
277
}
168
278
169
279
/**
170
- * @param \Magento\Framework\View\Element\AbstractBlock $block
280
+ * Moves Block to `left` container
281
+ *
282
+ * @param AbstractBlock $block
171
283
* @return $this
172
284
*/
173
- protected function _addLeft (\ Magento \ Framework \ View \ Element \ AbstractBlock $ block )
285
+ protected function _addLeft (AbstractBlock $ block )
174
286
{
175
287
return $ this ->_moveBlockToContainer ($ block , 'left ' );
176
288
}
177
289
178
290
/**
179
- * @param \Magento\Framework\View\Element\AbstractBlock $block
291
+ * Adds Block to `js` container
292
+ *
293
+ * @param AbstractBlock $block
180
294
* @return $this
181
295
*/
182
- protected function _addJs (\ Magento \ Framework \ View \ Element \ AbstractBlock $ block )
296
+ protected function _addJs (AbstractBlock $ block )
183
297
{
184
298
return $ this ->_moveBlockToContainer ($ block , 'js ' );
185
299
}
186
300
187
301
/**
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
302
+ * Set specified block as an anonymous child to specified container.
191
303
*
192
- * @param \Magento\Framework\View\Element\ AbstractBlock $block
304
+ * @param AbstractBlock $block
193
305
* @param string $containerName
194
306
* @return $this
195
307
*/
196
- private function _moveBlockToContainer (\ Magento \ Framework \ View \ Element \ AbstractBlock $ block , $ containerName )
308
+ private function _moveBlockToContainer (AbstractBlock $ block , $ containerName )
197
309
{
198
310
$ this ->_view ->getLayout ()->setChild ($ containerName , $ block ->getNameInLayout (), '' );
199
311
return $ this ;
200
312
}
201
313
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
-
234
314
/**
235
315
* Check whether url is checked
236
316
*
@@ -239,55 +319,13 @@ public function dispatch(\Magento\Framework\App\RequestInterface $request)
239
319
protected function _isUrlChecked ()
240
320
{
241
321
return !$ this ->_actionFlag ->get ('' , self ::FLAG_IS_URLS_CHECKED )
242
- && !$ this ->getRequest ()->isForwarded ()
243
- && !$ this ->_getSession ()->getIsUrlNotice (true )
244
- && !$ this ->_canUseBaseUrl ;
245
- }
246
-
247
- /**
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 ;
322
+ && !$ this ->getRequest ()->isForwarded ()
323
+ && !$ this ->_getSession ()->getIsUrlNotice (true )
324
+ && !$ this ->_canUseBaseUrl ;
286
325
}
287
326
288
327
/**
289
- * Set session locale,
290
- * process force locale set through url params
328
+ * Set session locale, process force locale set through url params
291
329
*
292
330
* @return $this
293
331
*/
@@ -309,8 +347,8 @@ protected function _processLocaleSettings()
309
347
* Set redirect into response
310
348
*
311
349
* @TODO MAGETWO-28356: Refactor controller actions to new ResultInterface
312
- * @param string $path
313
- * @param array $arguments
350
+ * @param string $path
351
+ * @param array $arguments
314
352
* @return \Magento\Framework\App\ResponseInterface
315
353
*/
316
354
protected function _redirect ($ path , $ arguments = [])
@@ -333,19 +371,7 @@ protected function _redirect($path, $arguments = [])
333
371
protected function _forward ($ action , $ controller = null , $ module = null , array $ params = null )
334
372
{
335
373
$ 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 );
374
+ parent ::_forward ($ action , $ controller , $ module , $ params );
349
375
}
350
376
351
377
/**
@@ -359,7 +385,7 @@ protected function _validateSecretKey()
359
385
return true ;
360
386
}
361
387
362
- $ secretKey = $ this ->getRequest ()->getParam (\ Magento \ Backend \ Model \ UrlInterface::SECRET_KEY_PARAM_NAME , null );
388
+ $ secretKey = $ this ->getRequest ()->getParam (UrlInterface::SECRET_KEY_PARAM_NAME , null );
363
389
if (!$ secretKey || $ secretKey != $ this ->_backendUrl ->getSecretKey ()) {
364
390
return false ;
365
391
}
0 commit comments