Browse Source

made form login configurable

Fabien Potencier 14 years ago
parent
commit
dd4f87b8c2

+ 20 - 2
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/SecurityExtension.php

@@ -206,11 +206,11 @@ class SecurityExtension extends Extension
             $listeners[] = new Reference('security.logout_listener');
 
             if (isset($firewall['logout']['path'])) {
-                $container->setParameter('security.authentication.form.logout_path', $firewall['logout']['path']);
+                $container->setParameter('security.logout.path', $firewall['logout']['path']);
             }
 
             if (isset($firewall['logout']['target'])) {
-                $container->setParameter('security.authentication.form.target_path', $firewall['logout']['target']);
+                $container->setParameter('security.logout.target_path', $firewall['logout']['target']);
             }
         }
 
@@ -446,6 +446,24 @@ class SecurityExtension extends Extension
         $arguments[1] = new Reference($provider);
         $listener->setArguments($arguments);
 
+        $options = array(
+            'check_path'                     => '/login_check',
+            'login_path'                     => '/login',
+            'always_use_default_target_path' => false,
+            'default_target_path'            => '/',
+            'target_path_parameter'          => '_target_path',
+            'use_referer'                    => false,
+            'failure_path'                   => null,
+            'failure_forward'                => false,
+        );
+        foreach (array_keys($options) as $key) {
+            if (isset($config[$key])) {
+                $options[$key] = $config[$key];
+            }
+        }
+        $container->setParameter('security.authentication.form.options', $options);
+        $container->setParameter('security.authentication.form.login_path', $options['login_path']);
+
         return array($provider, $listenerId);
     }
 

+ 4 - 9
src/Symfony/Bundle/FrameworkBundle/Resources/config/security.xml

@@ -25,14 +25,7 @@
 
         <parameter key="security.authentication.form_entry_point.class">Symfony\Component\HttpKernel\Security\EntryPoint\FormAuthenticationEntryPoint</parameter>
         <parameter key="security.authentication.form.login_path">/login</parameter>
-        <parameter key="security.authentication.form.check_path">/login_check</parameter>
-        <parameter key="security.authentication.form.logout_path">/logout</parameter>
-        <parameter key="security.authentication.form.target_path">/</parameter>
         <parameter key="security.authentication.listener.form.class">Symfony\Component\HttpKernel\Security\Firewall\UsernamePasswordFormAuthenticationListener</parameter>
-        <parameter key="security.authentication.listener.options">
-            <parameter key="login_path">%security.authentication.form.login_path%</parameter>
-            <parameter key="check_path">%security.authentication.form.check_path%</parameter>
-        </parameter>
 
         <parameter key="security.authentication.basic_entry_point.class">Symfony\Component\HttpKernel\Security\EntryPoint\BasicAuthenticationEntryPoint</parameter>
         <parameter key="security.authentication.basic_entry_point.realm">Symfony2</parameter>
@@ -53,6 +46,8 @@
         <parameter key="security.channel_listener.class">Symfony\Component\HttpKernel\Security\Firewall\ChannelListener</parameter>
 
         <parameter key="security.logout_listener.class">Symfony\Component\HttpKernel\Security\Firewall\LogoutListener</parameter>
+        <parameter key="security.logout.path">/logout</parameter>
+        <parameter key="security.logout.target_path">/</parameter>
 
         <parameter key="security.authentication.switchuser_listener.class">Symfony\Component\HttpKernel\Security\Firewall\SwitchUserListener</parameter>
         <parameter key="security.authentication.switchuser.role">ROLE_ALLOWED_TO_SWITCH</parameter>
@@ -118,8 +113,8 @@
 
         <service id="security.logout_listener" class="%security.logout_listener.class%">
             <argument type="service" id="security.context" />
-            <argument>%security.authentication.form.logout_path%</argument>
-            <argument>%security.authentication.form.target_path%</argument>
+            <argument>%security.logout.path%</argument>
+            <argument>%security.logout.target_path%</argument>
         </service>
 
         <service id="security.channel_listener" class="%security.channel_listener.class%">

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/config/security_templates.xml

@@ -8,7 +8,7 @@
         <service id="security.authentication.listener.form" class="%security.authentication.listener.form.class%">
             <argument type="service" id="security.context" />
             <argument type="service" id="security.authentication.manager" />
-            <argument type="collection">%security.authentication.listener.options%</argument>
+            <argument type="collection">%security.authentication.form.options%</argument>
             <argument type="service" id="logger" on-invalid="null" />
         </service>
 

+ 20 - 20
src/Symfony/Component/HttpKernel/Security/Firewall/FormAuthenticationListener.php

@@ -49,14 +49,14 @@ abstract class FormAuthenticationListener
         $this->logger = $logger;
 
         $this->options = array_merge(array(
-            'check_path'                    => '/login_check',
-            'login_path'                    => '/login',
-            'always_use_default_target_url' => false,
-            'default_target_url'            => '/',
-            'target_url_parameter'          => '_target_url',
-            'use_referer'                   => false,
-            'failure_url'                   => null,
-            'failure_forward'               => false,
+            'check_path'                     => '/login_check',
+            'login_path'                     => '/login',
+            'always_use_default_target_path' => false,
+            'default_target_path'            => '/',
+            'target_path_parameter'          => '_target_path',
+            'use_referer'                    => false,
+            'failure_path'                   => null,
+            'failure_forward'                => false,
         ), $options);
     }
 
@@ -118,28 +118,28 @@ abstract class FormAuthenticationListener
 
         $this->securityContext->setToken(null);
 
-        if (null === $this->options['failure_url']) {
-            $this->options['failure_url'] = $this->options['login_path'];
+        if (null === $this->options['failure_path']) {
+            $this->options['failure_path'] = $this->options['login_path'];
         }
 
         if ($this->options['failure_forward']) {
             if (null !== $this->logger) {
-                $this->logger->debug(sprintf('Forwarding to %s', $this->options['failure_url']));
+                $this->logger->debug(sprintf('Forwarding to %s', $this->options['failure_path']));
             }
 
-            $subRequest = Request::create($this->options['failure_url']);
+            $subRequest = Request::create($this->options['failure_path']);
             $subRequest->attributes->set(SecurityContext::AUTHENTICATION_ERROR, $failed->getMessage());
 
             return $event->getSubject()->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
         } else {
             if (null !== $this->logger) {
-                $this->logger->debug(sprintf('Redirecting to %s', $this->options['failure_url']));
+                $this->logger->debug(sprintf('Redirecting to %s', $this->options['failure_path']));
             }
 
             $request->getSession()->set(SecurityContext::AUTHENTICATION_ERROR, $failed->getMessage());
 
             $response = new Response();
-            $response->setRedirect(0 !== strpos($this->options['failure_url'], 'http') ? $request->getUriForPath($this->options['failure_url']) : $this->options['failure_url'], 302);
+            $response->setRedirect(0 !== strpos($this->options['failure_path'], 'http') ? $request->getUriForPath($this->options['failure_path']) : $this->options['failure_path'], 302);
 
             return $response;
         }
@@ -169,17 +169,17 @@ abstract class FormAuthenticationListener
      */
     protected function determineTargetUrl(Request $request)
     {
-        if ($this->options['always_use_default_target_url']) {
-            return $this->options['default_target_url'];
+        if ($this->options['always_use_default_target_path']) {
+            return $this->options['default_target_path'];
         }
 
-        if ($targetUrl = $request->get($this->options['target_url_parameter'])) {
+        if ($targetUrl = $request->get($this->options['target_path_parameter'])) {
             return $targetUrl;
         }
 
         $session = $request->getSession();
-        if ($targetUrl = $session->get('_security.target_url')) {
-            $session->remove('_security.target_url');
+        if ($targetUrl = $session->get('_security.target_path')) {
+            $session->remove('_security.target_path');
 
             return $targetUrl;
         }
@@ -189,6 +189,6 @@ abstract class FormAuthenticationListener
             return $targetUrl;
         }
 
-        return $this->options['default_target_url'];
+        return $this->options['default_target_path'];
     }
 }