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
+ * phpcs:disable Magento2.Classes.AbstractApi
11
24
* @api
12
25
* @SuppressWarnings(PHPMD.NumberOfChildren)
13
26
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -45,32 +58,32 @@ abstract class AbstractAction extends \Magento\Framework\App\Action\Action
45
58
protected $ _sessionNamespace = self ::SESSION_NAMESPACE ;
46
59
47
60
/**
48
- * @var \Magento\Backend\Helper\Data
61
+ * @var BackendHelper
49
62
*/
50
63
protected $ _helper ;
51
64
52
65
/**
53
- * @var \Magento\Backend\Model\ Session
66
+ * @var Session
54
67
*/
55
68
protected $ _session ;
56
69
57
70
/**
58
- * @var \Magento\Framework\ AuthorizationInterface
71
+ * @var AuthorizationInterface
59
72
*/
60
73
protected $ _authorization ;
61
74
62
75
/**
63
- * @var \Magento\Backend\Model\ Auth
76
+ * @var Auth
64
77
*/
65
78
protected $ _auth ;
66
79
67
80
/**
68
- * @var \Magento\Backend\Model\ UrlInterface
81
+ * @var UrlInterface
69
82
*/
70
83
protected $ _backendUrl ;
71
84
72
85
/**
73
- * @var \Magento\Framework\Locale\ ResolverInterface
86
+ * @var ResolverInterface
74
87
*/
75
88
protected $ _localeResolver ;
76
89
@@ -80,14 +93,14 @@ abstract class AbstractAction extends \Magento\Framework\App\Action\Action
80
93
protected $ _canUseBaseUrl ;
81
94
82
95
/**
83
- * @var \Magento\Framework\Data\Form\FormKey\Validator
96
+ * @var FormKeyValidator
84
97
*/
85
98
protected $ _formKeyValidator ;
86
99
87
100
/**
88
- * @param \Magento\Backend\App\Action\ Context $context
101
+ * @param Context $context
89
102
*/
90
- public function __construct (Action \ Context $ context )
103
+ public function __construct (Context $ context )
91
104
{
92
105
parent ::__construct ($ context );
93
106
$ this ->_authorization = $ context ->getAuthorization ();
@@ -101,6 +114,95 @@ public function __construct(Action\Context $context)
101
114
}
102
115
103
116
/**
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
+ *
104
206
* @return bool
105
207
*/
106
208
protected function _isAllowed ()
@@ -119,6 +221,8 @@ protected function _getSession()
119
221
}
120
222
121
223
/**
224
+ * Returns instantiated Message\ManagerInterface.
225
+ *
122
226
* @return \Magento\Framework\Message\ManagerInterface
123
227
*/
124
228
protected function getMessageManager ()
@@ -146,6 +250,8 @@ protected function _setActiveMenu($itemId)
146
250
}
147
251
148
252
/**
253
+ * Adds element to Breadcrumbs block
254
+ *
149
255
* @param string $label
150
256
* @param string $title
151
257
* @param string|null $link
@@ -158,79 +264,51 @@ protected function _addBreadcrumb($label, $title, $link = null)
158
264
}
159
265
160
266
/**
161
- * @param \Magento\Framework\View\Element\AbstractBlock $block
267
+ * Adds block to `content` block
268
+ *
269
+ * @param AbstractBlock $block
162
270
* @return $this
163
271
*/
164
- protected function _addContent (\ Magento \ Framework \ View \ Element \ AbstractBlock $ block )
272
+ protected function _addContent (AbstractBlock $ block )
165
273
{
166
274
return $ this ->_moveBlockToContainer ($ block , 'content ' );
167
275
}
168
276
169
277
/**
170
- * @param \Magento\Framework\View\Element\AbstractBlock $block
278
+ * Moves Block to `left` container
279
+ *
280
+ * @param AbstractBlock $block
171
281
* @return $this
172
282
*/
173
- protected function _addLeft (\ Magento \ Framework \ View \ Element \ AbstractBlock $ block )
283
+ protected function _addLeft (AbstractBlock $ block )
174
284
{
175
285
return $ this ->_moveBlockToContainer ($ block , 'left ' );
176
286
}
177
287
178
288
/**
179
- * @param \Magento\Framework\View\Element\AbstractBlock $block
289
+ * Adds Block to `js` container
290
+ *
291
+ * @param AbstractBlock $block
180
292
* @return $this
181
293
*/
182
- protected function _addJs (\ Magento \ Framework \ View \ Element \ AbstractBlock $ block )
294
+ protected function _addJs (AbstractBlock $ block )
183
295
{
184
296
return $ this ->_moveBlockToContainer ($ block , 'js ' );
185
297
}
186
298
187
299
/**
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.
191
301
*
192
- * @param \Magento\Framework\View\Element\ AbstractBlock $block
302
+ * @param AbstractBlock $block
193
303
* @param string $containerName
194
304
* @return $this
195
305
*/
196
- private function _moveBlockToContainer (\ Magento \ Framework \ View \ Element \ AbstractBlock $ block , $ containerName )
306
+ private function _moveBlockToContainer (AbstractBlock $ block , $ containerName )
197
307
{
198
308
$ this ->_view ->getLayout ()->setChild ($ containerName , $ block ->getNameInLayout (), '' );
199
309
return $ this ;
200
310
}
201
311
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
312
/**
235
313
* Check whether url is checked
236
314
*
@@ -239,55 +317,13 @@ public function dispatch(\Magento\Framework\App\RequestInterface $request)
239
317
protected function _isUrlChecked ()
240
318
{
241
319
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 ;
245
323
}
246
324
247
325
/**
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
291
327
*
292
328
* @return $this
293
329
*/
@@ -309,8 +345,8 @@ protected function _processLocaleSettings()
309
345
* Set redirect into response
310
346
*
311
347
* @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
314
350
* @return \Magento\Framework\App\ResponseInterface
315
351
*/
316
352
protected function _redirect ($ path , $ arguments = [])
@@ -333,19 +369,7 @@ protected function _redirect($path, $arguments = [])
333
369
protected function _forward ($ action , $ controller = null , $ module = null , array $ params = null )
334
370
{
335
371
$ 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 );
349
373
}
350
374
351
375
/**
@@ -359,7 +383,7 @@ protected function _validateSecretKey()
359
383
return true ;
360
384
}
361
385
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 );
363
387
if (!$ secretKey || $ secretKey != $ this ->_backendUrl ->getSecretKey ()) {
364
388
return false ;
365
389
}
0 commit comments