<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	>
<channel>
	<title>
	Comments on: [Symfony 2] Security Bundle: Set User Locale on Form Login	</title>
	<atom:link href="https://nerdpress.org/2011/08/14/symfony-2-security-bundle-set-user-locale-on-form-login/feed/" rel="self" type="application/rss+xml" />
	<link>https://nerdpress.org/2011/08/14/symfony-2-security-bundle-set-user-locale-on-form-login/</link>
	<description>...dev, tech problems and solutions.</description>
	<lastBuildDate>Fri, 14 Oct 2011 09:20:43 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>
		By: Johannes Heinen		</title>
		<link>https://nerdpress.org/2011/08/14/symfony-2-security-bundle-set-user-locale-on-form-login/#comment-170</link>

		<dc:creator><![CDATA[Johannes Heinen]]></dc:creator>
		<pubDate>Wed, 24 Aug 2011 10:05:37 +0000</pubDate>
		<guid isPermaLink="false">https://nerdpress.org/?p=1574#comment-170</guid>

					<description><![CDATA[:D Plus 1!]]></description>
			<content:encoded><![CDATA[<p>:D Plus 1!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Max Girkens		</title>
		<link>https://nerdpress.org/2011/08/14/symfony-2-security-bundle-set-user-locale-on-form-login/#comment-169</link>

		<dc:creator><![CDATA[Max Girkens]]></dc:creator>
		<pubDate>Wed, 24 Aug 2011 10:01:18 +0000</pubDate>
		<guid isPermaLink="false">https://nerdpress.org/?p=1574#comment-169</guid>

					<description><![CDATA[your&#039;re welcome :),

hopefully i didn&#039;t mix up too much :P
but might be slightly more understandable than google translate.

btw:
http://dict.leo.org/forum/viewUnsolvedquery.php?idThread=176140&amp;idForum=1&amp;lp=ende&amp;lang=de]]></description>
			<content:encoded><![CDATA[<p>your&#8217;re welcome :),</p>
<p>hopefully i didn&#8217;t mix up too much :P<br />
but might be slightly more understandable than google translate.</p>
<p>btw:<br />
<a href="http://dict.leo.org/forum/viewUnsolvedquery.php?idThread=176140&#038;idForum=1&#038;lp=ende&#038;lang=de" rel="nofollow ugc">http://dict.leo.org/forum/viewUnsolvedquery.php?idThread=176140&#038;idForum=1&#038;lp=ende&#038;lang=de</a></p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Joshi		</title>
		<link>https://nerdpress.org/2011/08/14/symfony-2-security-bundle-set-user-locale-on-form-login/#comment-168</link>

		<dc:creator><![CDATA[Joshi]]></dc:creator>
		<pubDate>Wed, 24 Aug 2011 09:56:25 +0000</pubDate>
		<guid isPermaLink="false">https://nerdpress.org/?p=1574#comment-168</guid>

					<description><![CDATA[Hey, 

Max was kind enough translating the article, thanks for the effort! Feel free to reuse it in any way, and thanks for your inquiry :)

The Security Bundle is a bit magical. You have to define a HTML-Form, the rest gets configured somehow elsewhere (post-parameter names like &quot;_username&quot;, &quot;_password&quot; etc., the referrer redirect, Remember-Me functionality etc., all that is handled by the firewall. You just have to define a Login-Route, a Stub-Controller + Action-Callable (which actually never gets executed, as the firewall comes first), done. 

Very comfortable - as long as you don&#039;t start asking questions. But how do you execute functions directly after the login, without altering the Security-Bundle Code?

&lt;!--more--&gt;

Most of the time you&#039;ll try to find a Hook, Entry-Point or Config-Variable.
In Symfony 2 of course, you&#039;ll be looking for events, but the Documentation does not say anything about that. 
So back to the source-code. 
There has to be a possibility - and there is. It&#039;s to be found in the AbstractAuthentificationListener:

vendorsymfonysrcSymfonyComponentSecurityHttpFirewallAbstractAuthentificationListener.php
[sourcecode lang=&quot;php&quot;]

namespace SymfonyComponentSecurityHttpFirewall;

//...

abstract class AbstractAuthenticationListener implements ListenerInterface
{
    // ...
    private function onSuccess(GetResponseEvent $event, Request $request, TokenInterface $token)
    {
        if (null !== $this-&#062;logger) {
            $this-&#062;logger-&#062;info(sprintf(&#039;User &#034;%s&#034; has been authenticated successfully&#039;, $token-&#062;getUsername()));
        }

        $this-&#062;securityContext-&#062;setToken($token);

        $session = $request-&#062;getSession();
        $session-&#062;remove(SecurityContextInterface::AUTHENTICATION_ERROR);
        $session-&#062;remove(SecurityContextInterface::LAST_USERNAME);

        if (null !== $this-&#062;dispatcher) {
            $loginEvent = new InteractiveLoginEvent($request, $token);
            $this-&#062;dispatcher-&#062;dispatch(SecurityEvents::INTERACTIVE_LOGIN, $loginEvent);
        }

        if (null !== $this-&#062;successHandler) {
            $response = $this-&#062;successHandler-&#062;onAuthenticationSuccess($request, $token);
        } else {
            $response = $this-&#062;httpUtils-&#062;createRedirectResponse($request, $this-&#062;determineTargetUrl($request));
        }

        if (null !== $this-&#062;rememberMeServices) {
            $this-&#062;rememberMeServices-&#062;loginSuccess($request, $response, $token);
        }

        return $response;
    }
[/sourcecode]

The class is just printed as an excerpt here, interesting are lines 233 and 238 in the original source.

line 233 and the following notifies all AbstractAuthentificationListener which listen to SecurityEvents::INTERACTIVE_LOGIN, if a Event-Dispatcher is defined. (Which can be expected in the sf2 standard distribution)

line 238 is something special, a &quot;Security-Bundle-specific&quot; Event.
there&#039;s a possibility to set a DIC-Service in the firewall settings in the security.yml via &quot;success_handler&quot;.

 Man hat die Möglichkeit, in der jeweiligen Firewall-Einstellung für den Form-Login in der security.yml mit success_handler which implements SymfonyComponentSecurityHttpAuthenticationAuthenticationSuccessHandlerInterface.


This looks like this:

[sourcecode lang=&quot;Python&quot;]
      secured_area:
            pattern:    ^/demo/secured/
            form_login:
                check_path: /demo/secured/login_check
                login_path: /demo/secured/login
                success_handler: dvlp_core.login_success_handler
[/sourcecode]

Implementation of the Callback Method onAuthenticationSuccess(Request $request, TokenInterface $token) of the Interface is not that easy though, as it expects an instance of SymfonyComponentHttpFoundationResponse as return value,
which you have to configure by yourself (including Redirect-Adress, referrer-logic and processing of the user config)

That&#039;s why we&#039;ll leave that alone for the moment and use the Event-Dispatcher, to hook into the login.
Therefor we define a new Server in one of our Bundles and tag it as Eventlistener:

Resourcesconfigservices.xml:
[sourcecode lang=&quot;xml&quot;]
    &#060;parameters&#062;
        &#060;parameter key=&#034;dvlp_core.event_listener.class&#034;&#062;DvlpCoreBundleEventListener&#060;/parameter&#062;
    &#060;/parameters&#062;
    &#060;service id=&#034;dvlp_core.event_listener&#034; class=&#034;%dvlp_core.event_listener.class%&#034;&#062;
        &#060;tag name=&#034;kernel.event_listener&#034; event=&#034;security.interactive_login&#034; method=&#034;onSecurityInteractiveLogin&#034; /&#062;
    &#060;/service&#062;
[/sourcecode]

Now implement the listener. The listener should set the Session-Locale &quot;on login&quot; in the locale-settings of the current (DB)-User.

DvlpCoreBundleEventListener.php:

[sourcecode lang=&quot;php&quot;]
class EventListener
{
    public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
    {
        $token = $event-&#062;getAuthenticationToken();
        $session = $event-&#062;getRequest()-&#062;getSession();
        $session-&#062;setLocale($token-&#062;getUser()-&#062;getLocale());
    }
}
[/sourcecode]

And That&#039;s that -Events to the Rescue.
Unfortunately there seems to be not so much documentation on that atm.
But I&#039;ll be happy to be proven wrong.

Have fun trying that at home.]]></description>
			<content:encoded><![CDATA[<p>Hey, </p>
<p>Max was kind enough translating the article, thanks for the effort! Feel free to reuse it in any way, and thanks for your inquiry :)</p>
<p>The Security Bundle is a bit magical. You have to define a HTML-Form, the rest gets configured somehow elsewhere (post-parameter names like &#8220;_username&#8221;, &#8220;_password&#8221; etc., the referrer redirect, Remember-Me functionality etc., all that is handled by the firewall. You just have to define a Login-Route, a Stub-Controller + Action-Callable (which actually never gets executed, as the firewall comes first), done. </p>
<p>Very comfortable &#8211; as long as you don&#8217;t start asking questions. But how do you execute functions directly after the login, without altering the Security-Bundle Code?</p>
<p><!--more--></p>
<p>Most of the time you&#8217;ll try to find a Hook, Entry-Point or Config-Variable.<br />
In Symfony 2 of course, you&#8217;ll be looking for events, but the Documentation does not say anything about that.<br />
So back to the source-code.<br />
There has to be a possibility &#8211; and there is. It&#8217;s to be found in the AbstractAuthentificationListener:</p>
<p>vendorsymfonysrcSymfonyComponentSecurityHttpFirewallAbstractAuthentificationListener.php</p>
<pre class="brush: php; title: ; notranslate">

namespace SymfonyComponentSecurityHttpFirewall;

//...

abstract class AbstractAuthenticationListener implements ListenerInterface
{
    // ...
    private function onSuccess(GetResponseEvent $event, Request $request, TokenInterface $token)
    {
        if (null !== $this-&gt;logger) {
            $this-&gt;logger-&gt;info(sprintf('User &quot;%s&quot; has been authenticated successfully', $token-&gt;getUsername()));
        }

        $this-&gt;securityContext-&gt;setToken($token);

        $session = $request-&gt;getSession();
        $session-&gt;remove(SecurityContextInterface::AUTHENTICATION_ERROR);
        $session-&gt;remove(SecurityContextInterface::LAST_USERNAME);

        if (null !== $this-&gt;dispatcher) {
            $loginEvent = new InteractiveLoginEvent($request, $token);
            $this-&gt;dispatcher-&gt;dispatch(SecurityEvents::INTERACTIVE_LOGIN, $loginEvent);
        }

        if (null !== $this-&gt;successHandler) {
            $response = $this-&gt;successHandler-&gt;onAuthenticationSuccess($request, $token);
        } else {
            $response = $this-&gt;httpUtils-&gt;createRedirectResponse($request, $this-&gt;determineTargetUrl($request));
        }

        if (null !== $this-&gt;rememberMeServices) {
            $this-&gt;rememberMeServices-&gt;loginSuccess($request, $response, $token);
        }

        return $response;
    }
</pre>
<p>The class is just printed as an excerpt here, interesting are lines 233 and 238 in the original source.</p>
<p>line 233 and the following notifies all AbstractAuthentificationListener which listen to SecurityEvents::INTERACTIVE_LOGIN, if a Event-Dispatcher is defined. (Which can be expected in the sf2 standard distribution)</p>
<p>line 238 is something special, a &#8220;Security-Bundle-specific&#8221; Event.<br />
there&#8217;s a possibility to set a DIC-Service in the firewall settings in the security.yml via &#8220;success_handler&#8221;.</p>
<p> Man hat die Möglichkeit, in der jeweiligen Firewall-Einstellung für den Form-Login in der security.yml mit success_handler which implements SymfonyComponentSecurityHttpAuthenticationAuthenticationSuccessHandlerInterface.</p>
<p>This looks like this:</p>
<pre class="brush: python; title: ; notranslate">
      secured_area:
            pattern:    ^/demo/secured/
            form_login:
                check_path: /demo/secured/login_check
                login_path: /demo/secured/login
                success_handler: dvlp_core.login_success_handler
</pre>
<p>Implementation of the Callback Method onAuthenticationSuccess(Request $request, TokenInterface $token) of the Interface is not that easy though, as it expects an instance of SymfonyComponentHttpFoundationResponse as return value,<br />
which you have to configure by yourself (including Redirect-Adress, referrer-logic and processing of the user config)</p>
<p>That&#8217;s why we&#8217;ll leave that alone for the moment and use the Event-Dispatcher, to hook into the login.<br />
Therefor we define a new Server in one of our Bundles and tag it as Eventlistener:</p>
<p>Resourcesconfigservices.xml:</p>
<pre class="brush: xml; title: ; notranslate">
    &lt;parameters&gt;
        &lt;parameter key=&quot;dvlp_core.event_listener.class&quot;&gt;DvlpCoreBundleEventListener&lt;/parameter&gt;
    &lt;/parameters&gt;
    &lt;service id=&quot;dvlp_core.event_listener&quot; class=&quot;%dvlp_core.event_listener.class%&quot;&gt;
        &lt;tag name=&quot;kernel.event_listener&quot; event=&quot;security.interactive_login&quot; method=&quot;onSecurityInteractiveLogin&quot; /&gt;
    &lt;/service&gt;
</pre>
<p>Now implement the listener. The listener should set the Session-Locale &#8220;on login&#8221; in the locale-settings of the current (DB)-User.</p>
<p>DvlpCoreBundleEventListener.php:</p>
<pre class="brush: php; title: ; notranslate">
class EventListener
{
    public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
    {
        $token = $event-&gt;getAuthenticationToken();
        $session = $event-&gt;getRequest()-&gt;getSession();
        $session-&gt;setLocale($token-&gt;getUser()-&gt;getLocale());
    }
}
</pre>
<p>And That&#8217;s that -Events to the Rescue.<br />
Unfortunately there seems to be not so much documentation on that atm.<br />
But I&#8217;ll be happy to be proven wrong.</p>
<p>Have fun trying that at home.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: cordoval		</title>
		<link>https://nerdpress.org/2011/08/14/symfony-2-security-bundle-set-user-locale-on-form-login/#comment-167</link>

		<dc:creator><![CDATA[cordoval]]></dc:creator>
		<pubDate>Tue, 23 Aug 2011 17:47:48 +0000</pubDate>
		<guid isPermaLink="false">https://nerdpress.org/?p=1574#comment-167</guid>

					<description><![CDATA[I tried to translate this into english with google but couldn&#039;t, would you mind posting one version in english or somehow facilitate this? If not perhaps you could allow me to post a republish version of this in english on my blog http://www.craftitonline.com thanks]]></description>
			<content:encoded><![CDATA[<p>I tried to translate this into english with google but couldn&#8217;t, would you mind posting one version in english or somehow facilitate this? If not perhaps you could allow me to post a republish version of this in english on my blog <a href="http://www.craftitonline.com" rel="nofollow ugc">http://www.craftitonline.com</a> thanks</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
