33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+
67namespace 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+ * @deprecated Use \Magento\Framework\App\ActionInterface
24+ *
25+ * phpcs:disable Magento2.Classes.AbstractApi
1126 * @api
1227 * @SuppressWarnings(PHPMD.NumberOfChildren)
1328 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -45,32 +60,32 @@ abstract class AbstractAction extends \Magento\Framework\App\Action\Action
4560 protected $ _sessionNamespace = self ::SESSION_NAMESPACE ;
4661
4762 /**
48- * @var \Magento\Backend\Helper\Data
63+ * @var BackendHelper
4964 */
5065 protected $ _helper ;
5166
5267 /**
53- * @var \Magento\Backend\Model\ Session
68+ * @var Session
5469 */
5570 protected $ _session ;
5671
5772 /**
58- * @var \Magento\Framework\ AuthorizationInterface
73+ * @var AuthorizationInterface
5974 */
6075 protected $ _authorization ;
6176
6277 /**
63- * @var \Magento\Backend\Model\ Auth
78+ * @var Auth
6479 */
6580 protected $ _auth ;
6681
6782 /**
68- * @var \Magento\Backend\Model\ UrlInterface
83+ * @var UrlInterface
6984 */
7085 protected $ _backendUrl ;
7186
7287 /**
73- * @var \Magento\Framework\Locale\ ResolverInterface
88+ * @var ResolverInterface
7489 */
7590 protected $ _localeResolver ;
7691
@@ -80,14 +95,14 @@ abstract class AbstractAction extends \Magento\Framework\App\Action\Action
8095 protected $ _canUseBaseUrl ;
8196
8297 /**
83- * @var \Magento\Framework\Data\Form\FormKey\Validator
98+ * @var FormKeyValidator
8499 */
85100 protected $ _formKeyValidator ;
86101
87102 /**
88- * @param \Magento\Backend\App\Action\ Context $context
103+ * @param Context $context
89104 */
90- public function __construct (Action \ Context $ context )
105+ public function __construct (Context $ context )
91106 {
92107 parent ::__construct ($ context );
93108 $ this ->_authorization = $ context ->getAuthorization ();
@@ -101,6 +116,95 @@ public function __construct(Action\Context $context)
101116 }
102117
103118 /**
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+ *
104208 * @return bool
105209 */
106210 protected function _isAllowed ()
@@ -119,6 +223,8 @@ protected function _getSession()
119223 }
120224
121225 /**
226+ * Returns instantiated Message\ManagerInterface.
227+ *
122228 * @return \Magento\Framework\Message\ManagerInterface
123229 */
124230 protected function getMessageManager ()
@@ -146,6 +252,8 @@ protected function _setActiveMenu($itemId)
146252 }
147253
148254 /**
255+ * Adds element to Breadcrumbs block
256+ *
149257 * @param string $label
150258 * @param string $title
151259 * @param string|null $link
@@ -158,79 +266,51 @@ protected function _addBreadcrumb($label, $title, $link = null)
158266 }
159267
160268 /**
161- * @param \Magento\Framework\View\Element\AbstractBlock $block
269+ * Adds block to `content` block
270+ *
271+ * @param AbstractBlock $block
162272 * @return $this
163273 */
164- protected function _addContent (\ Magento \ Framework \ View \ Element \ AbstractBlock $ block )
274+ protected function _addContent (AbstractBlock $ block )
165275 {
166276 return $ this ->_moveBlockToContainer ($ block , 'content ' );
167277 }
168278
169279 /**
170- * @param \Magento\Framework\View\Element\AbstractBlock $block
280+ * Moves Block to `left` container
281+ *
282+ * @param AbstractBlock $block
171283 * @return $this
172284 */
173- protected function _addLeft (\ Magento \ Framework \ View \ Element \ AbstractBlock $ block )
285+ protected function _addLeft (AbstractBlock $ block )
174286 {
175287 return $ this ->_moveBlockToContainer ($ block , 'left ' );
176288 }
177289
178290 /**
179- * @param \Magento\Framework\View\Element\AbstractBlock $block
291+ * Adds Block to `js` container
292+ *
293+ * @param AbstractBlock $block
180294 * @return $this
181295 */
182- protected function _addJs (\ Magento \ Framework \ View \ Element \ AbstractBlock $ block )
296+ protected function _addJs (AbstractBlock $ block )
183297 {
184298 return $ this ->_moveBlockToContainer ($ block , 'js ' );
185299 }
186300
187301 /**
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.
191303 *
192- * @param \Magento\Framework\View\Element\ AbstractBlock $block
304+ * @param AbstractBlock $block
193305 * @param string $containerName
194306 * @return $this
195307 */
196- private function _moveBlockToContainer (\ Magento \ Framework \ View \ Element \ AbstractBlock $ block , $ containerName )
308+ private function _moveBlockToContainer (AbstractBlock $ block , $ containerName )
197309 {
198310 $ this ->_view ->getLayout ()->setChild ($ containerName , $ block ->getNameInLayout (), '' );
199311 return $ this ;
200312 }
201313
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-
234314 /**
235315 * Check whether url is checked
236316 *
@@ -239,55 +319,13 @@ public function dispatch(\Magento\Framework\App\RequestInterface $request)
239319 protected function _isUrlChecked ()
240320 {
241321 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 ;
286325 }
287326
288327 /**
289- * Set session locale,
290- * process force locale set through url params
328+ * Set session locale, process force locale set through url params
291329 *
292330 * @return $this
293331 */
@@ -309,8 +347,8 @@ protected function _processLocaleSettings()
309347 * Set redirect into response
310348 *
311349 * @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
314352 * @return \Magento\Framework\App\ResponseInterface
315353 */
316354 protected function _redirect ($ path , $ arguments = [])
@@ -333,19 +371,7 @@ protected function _redirect($path, $arguments = [])
333371 protected function _forward ($ action , $ controller = null , $ module = null , array $ params = null )
334372 {
335373 $ 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 );
349375 }
350376
351377 /**
@@ -359,7 +385,7 @@ protected function _validateSecretKey()
359385 return true ;
360386 }
361387
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 );
363389 if (!$ secretKey || $ secretKey != $ this ->_backendUrl ->getSecretKey ()) {
364390 return false ;
365391 }
0 commit comments