@@ -209,14 +209,14 @@ protected function _initCategory()
209209 */
210210 public function execute ()
211211 {
212- if ( $ this ->_request -> getParam (ActionInterface:: PARAM_NAME_URL_ENCODED )) {
213- //phpcs:ignore Magento2.Legacy.ObsoleteResponse
214- return $ this -> resultRedirectFactory -> create ()-> setUrl ( $ this -> _redirect -> getRedirectUrl ()) ;
212+ $ urlEncodedRedirect = $ this ->handleUrlEncodedRedirect ();
213+ if ( $ urlEncodedRedirect ) {
214+ return $ urlEncodedRedirect ;
215215 }
216216
217217 $ category = $ this ->_initCategory ();
218218 if (!$ category ) {
219- return $ this ->resultForwardFactory -> create ()-> forward ( ' noroute ' );
219+ return $ this ->handleCategoryNotFound ( );
220220 }
221221
222222 $ pageRedirect = $ this ->handlePageRedirect ($ category );
@@ -233,14 +233,74 @@ public function execute()
233233 return $ page ;
234234 }
235235
236+ /**
237+ * Handle URL encoded redirect
238+ *
239+ * @return \Magento\Framework\Controller\Result\Redirect|null
240+ */
241+ private function handleUrlEncodedRedirect ()
242+ {
243+ if ($ this ->_request ->getParam (ActionInterface::PARAM_NAME_URL_ENCODED )) {
244+ //phpcs:ignore Magento2.Legacy.ObsoleteResponse
245+ return $ this ->resultRedirectFactory ->create ()->setUrl ($ this ->_redirect ->getRedirectUrl ());
246+ }
247+ return null ;
248+ }
249+
250+ /**
251+ * Handle category not found scenarios
252+ *
253+ * @return ResultInterface
254+ * @throws NoSuchEntityException
255+ */
256+ private function handleCategoryNotFound ()
257+ {
258+ if ($ this ->getResponse ()->isRedirect ()) {
259+ return $ this ->getResponse ();
260+ }
261+ $ categoryId = (int )$ this ->getRequest ()->getParam ('id ' , false );
262+ if ($ categoryId && $ this ->isB2BPermissionDenial ($ categoryId )) {
263+ $ this ->getResponse ()->setBody ('' );
264+ return $ this ->getResponse ();
265+ }
266+ $ resultForward = $ this ->resultForwardFactory ->create ();
267+ $ resultForward ->forward ('noroute ' );
268+ return $ resultForward ;
269+ }
270+
271+ /**
272+ * Check if this is a B2B permission denial case
273+ *
274+ * @param int $categoryId
275+ * @return bool
276+ */
277+ private function isB2BPermissionDenial (int $ categoryId ): bool
278+ {
279+ try {
280+ $ existingCategory = $ this ->categoryRepository ->get (
281+ $ categoryId ,
282+ $ this ->_storeManager ->getStore ()->getId ()
283+ );
284+ return $ existingCategory ->getIsActive ()
285+ && $ existingCategory ->isInRootCategoryList ()
286+ && !$ this ->categoryHelper ->canShow ($ existingCategory );
287+ } catch (NoSuchEntityException $ e ) {
288+ return false ;
289+ }
290+ }
291+
236292 /**
237293 * Category handle page redirect
238294 *
239- * @param Category $category
295+ * @param Category|false $category
240296 * @return Redirect|null
241297 */
242- private function handlePageRedirect (Category $ category ): ?Redirect
298+ private function handlePageRedirect ($ category ): ?Redirect
243299 {
300+ if (!$ category || !($ category instanceof Category)) {
301+ return null ;
302+ }
303+
244304 if ($ this ->_request ->getParam (Toolbar::PAGE_PARM_NAME ) < 0 ) {
245305 return $ this ->resultRedirectFactory ->create ()
246306 ->setHttpResponseCode (301 )
0 commit comments