From c35adcaf63e0324f07a93c3884c6a2142d3d0085 Mon Sep 17 00:00:00 2001 From: Nathan Dench Date: Fri, 12 May 2017 00:40:49 +1000 Subject: [PATCH 1/5] Remove use of the deprecated `.class` parameter Reference: https://github.com/symfony/symfony/issues/11881 --- security/target_path.rst | 55 ++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/security/target_path.rst b/security/target_path.rst index f2993e91f4e..d0cfafcdfa4 100644 --- a/security/target_path.rst +++ b/security/target_path.rst @@ -17,36 +17,41 @@ the user is redirected back to a page which the browser cannot render. To get around this behavior, you would simply need to extend the ``ExceptionListener`` class and override the default method named ``setTargetPath()``. -First, override the ``security.exception_listener.class`` parameter in your -configuration file. This can be done from your main configuration file (in -``app/config``) or from a configuration file being imported from a bundle: +First, add a ``CompilerPass`` which you will use to override the ``ExceptionListener`` class: -.. configuration-block:: + // src/AppBundle/AppBundle.php + namespace AppBundle; + + use AppBundle\DependencyInjection\Compiler\ExceptionListenerPass; - .. code-block:: yaml - - # app/config/services.yml - parameters: - # ... - security.exception_listener.class: AppBundle\Security\Firewall\ExceptionListener - - .. code-block:: xml - - - - - AppBundle\Security\Firewall\ExceptionListener - - - .. code-block:: php + class AppBundle extends Bundle + { + public function build(ContainerBuilder $container) + { + $container->addCompilerPass(new ExceptionListenerPass()); + } + } + +Next, create the ``ExceptionListenerPass`` to replace the definition of you're firewall's ``ExceptionListener``: - // app/config/services.php - use AppBundle\Security\Firewall\ExceptionListener; + // src/AppBundle/DependencyInjection/Compiler/ExceptionListenerPass.php + namespace AppBundle\DependencyInjection\Compiler; + + use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; + use Symfony\Component\DependencyInjection\ContainerBuilder; + use AppBundle\Security\Firewall\ExceptionListener; - // ... - $container->setParameter('security.exception_listener.class', ExceptionListener::class); + class ExceptionListenerPass implements CompilerPassInterface + { + public function process(ContainerBuilder $container) + { + // use firewall name as suffix e.g. 'secured_area' + $definition = $container->getDefinition('security.exception_listener.secured_area'); + $definition->setClass('AppBundle\Security\Firewall\ExceptionListener'); + } + } -Next, create your own ``ExceptionListener``:: +Finally, create your own ``ExceptionListener``:: // src/AppBundle/Security/Firewall/ExceptionListener.php namespace AppBundle\Security\Firewall; From 435bbfe0b2bf2fc8e8e44b3b17f4028431b8c19f Mon Sep 17 00:00:00 2001 From: Nathan Dench Date: Fri, 12 May 2017 00:45:53 +1000 Subject: [PATCH 2/5] Fix grammar. --- security/target_path.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/security/target_path.rst b/security/target_path.rst index d0cfafcdfa4..c34127d2091 100644 --- a/security/target_path.rst +++ b/security/target_path.rst @@ -32,7 +32,8 @@ First, add a ``CompilerPass`` which you will use to override the ``ExceptionList } } -Next, create the ``ExceptionListenerPass`` to replace the definition of you're firewall's ``ExceptionListener``: +Next, create the ``ExceptionListenerPass`` to replace the definition of ``ExceptionListener``. +Make sure you use the name of the firewall you have configured in ``app/config``: // src/AppBundle/DependencyInjection/Compiler/ExceptionListenerPass.php namespace AppBundle\DependencyInjection\Compiler; @@ -45,7 +46,7 @@ Next, create the ``ExceptionListenerPass`` to replace the definition of you're f { public function process(ContainerBuilder $container) { - // use firewall name as suffix e.g. 'secured_area' + // Use the name of your firewall as suffix e.g. 'secured_area' $definition = $container->getDefinition('security.exception_listener.secured_area'); $definition->setClass('AppBundle\Security\Firewall\ExceptionListener'); } From 7f08786f4e0ac382e77607a59e75ccaff7fca862 Mon Sep 17 00:00:00 2001 From: Nathan Dench Date: Fri, 12 May 2017 09:53:12 +1000 Subject: [PATCH 3/5] fix indentation errors --- security/target_path.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/security/target_path.rst b/security/target_path.rst index c34127d2091..16706393d3a 100644 --- a/security/target_path.rst +++ b/security/target_path.rst @@ -17,7 +17,7 @@ the user is redirected back to a page which the browser cannot render. To get around this behavior, you would simply need to extend the ``ExceptionListener`` class and override the default method named ``setTargetPath()``. -First, add a ``CompilerPass`` which you will use to override the ``ExceptionListener`` class: +First, add a ``CompilerPass`` which you will use to override the ``ExceptionListener`` class:: // src/AppBundle/AppBundle.php namespace AppBundle; @@ -33,7 +33,7 @@ First, add a ``CompilerPass`` which you will use to override the ``ExceptionList } Next, create the ``ExceptionListenerPass`` to replace the definition of ``ExceptionListener``. -Make sure you use the name of the firewall you have configured in ``app/config``: +Make sure you use the name of the firewall you have configured in ``app/config``:: // src/AppBundle/DependencyInjection/Compiler/ExceptionListenerPass.php namespace AppBundle\DependencyInjection\Compiler; From 9d71a18e94882342d564a4f834cfe70fe782443f Mon Sep 17 00:00:00 2001 From: Nathan Dench Date: Sat, 13 May 2017 07:15:33 +1000 Subject: [PATCH 4/5] Fix from review --- security/target_path.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/security/target_path.rst b/security/target_path.rst index 16706393d3a..10e0323e257 100644 --- a/security/target_path.rst +++ b/security/target_path.rst @@ -17,7 +17,7 @@ the user is redirected back to a page which the browser cannot render. To get around this behavior, you would simply need to extend the ``ExceptionListener`` class and override the default method named ``setTargetPath()``. -First, add a ``CompilerPass`` which you will use to override the ``ExceptionListener`` class:: +First, add a compiler pass which you will use to replace the ``ExceptionListener`` class:: // src/AppBundle/AppBundle.php namespace AppBundle; @@ -33,7 +33,7 @@ First, add a ``CompilerPass`` which you will use to override the ``ExceptionList } Next, create the ``ExceptionListenerPass`` to replace the definition of ``ExceptionListener``. -Make sure you use the name of the firewall you have configured in ``app/config``:: +Make sure you use the name of the firewall you have configured your security configuration:: // src/AppBundle/DependencyInjection/Compiler/ExceptionListenerPass.php namespace AppBundle\DependencyInjection\Compiler; @@ -48,7 +48,7 @@ Make sure you use the name of the firewall you have configured in ``app/config`` { // Use the name of your firewall as suffix e.g. 'secured_area' $definition = $container->getDefinition('security.exception_listener.secured_area'); - $definition->setClass('AppBundle\Security\Firewall\ExceptionListener'); + $definition->setClass(ExceptionListener::class); } } From 92c3e4eeb9b6f4173a133fdf350b629244fe6122 Mon Sep 17 00:00:00 2001 From: Nathan Dench Date: Sat, 13 May 2017 15:52:19 +1000 Subject: [PATCH 5/5] Reorder the steps to be more intuitive --- security/target_path.rst | 65 ++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/security/target_path.rst b/security/target_path.rst index 10e0323e257..e78932aada4 100644 --- a/security/target_path.rst +++ b/security/target_path.rst @@ -15,25 +15,35 @@ URI was an XMLHttpRequest which returned a non-HTML or partial HTML response, the user is redirected back to a page which the browser cannot render. To get around this behavior, you would simply need to extend the ``ExceptionListener`` -class and override the default method named ``setTargetPath()``. +class and override the default method named ``setTargetPath()``:: -First, add a compiler pass which you will use to replace the ``ExceptionListener`` class:: + // src/AppBundle/Security/Firewall/ExceptionListener.php + namespace AppBundle\Security\Firewall; - // src/AppBundle/AppBundle.php - namespace AppBundle; - - use AppBundle\DependencyInjection\Compiler\ExceptionListenerPass; + use Symfony\Component\HttpFoundation\Request; + use Symfony\Component\Security\Http\Firewall\ExceptionListener as BaseExceptionListener; - class AppBundle extends Bundle - { - public function build(ContainerBuilder $container) + class ExceptionListener extends BaseExceptionListener + { + protected function setTargetPath(Request $request) { - $container->addCompilerPass(new ExceptionListenerPass()); + // Do not save target path for XHR requests + // You can add any more logic here you want + // Note that non-GET requests are already ignored + if ($request->isXmlHttpRequest()) { + return; + } + + parent::setTargetPath($request); } } - -Next, create the ``ExceptionListenerPass`` to replace the definition of ``ExceptionListener``. -Make sure you use the name of the firewall you have configured your security configuration:: + +By preventing ``setTargetPath()`` from being called on the parent, the Security component +won't retain the request URI. Add as much or as little logic here as required for your scenario! + +Next, create the ``ExceptionListenerPass`` to replace the definition of the default +``ExceptionListener`` with the one you just created. Make sure to use the name of +the firewall in your security configuration:: // src/AppBundle/DependencyInjection/Compiler/ExceptionListenerPass.php namespace AppBundle\DependencyInjection\Compiler; @@ -46,33 +56,24 @@ Make sure you use the name of the firewall you have configured your security con { public function process(ContainerBuilder $container) { - // Use the name of your firewall as suffix e.g. 'secured_area' + // Use the name of your firewall for the suffix e.g. 'secured_area' $definition = $container->getDefinition('security.exception_listener.secured_area'); $definition->setClass(ExceptionListener::class); } } -Finally, create your own ``ExceptionListener``:: - - // src/AppBundle/Security/Firewall/ExceptionListener.php - namespace AppBundle\Security\Firewall; +Finally, add a compiler pass to tie it all together:: - use Symfony\Component\HttpFoundation\Request; - use Symfony\Component\Security\Http\Firewall\ExceptionListener as BaseExceptionListener; + // src/AppBundle/AppBundle.php + namespace AppBundle; + + use AppBundle\DependencyInjection\Compiler\ExceptionListenerPass; - class ExceptionListener extends BaseExceptionListener - { - protected function setTargetPath(Request $request) + class AppBundle extends Bundle + { + public function build(ContainerBuilder $container) { - // Do not save target path for XHR requests - // You can add any more logic here you want - // Note that non-GET requests are already ignored - if ($request->isXmlHttpRequest()) { - return; - } - - parent::setTargetPath($request); + $container->addCompilerPass(new ExceptionListenerPass()); } } -Add as much or as little logic here as required for your scenario!