Skip to content

Commit

Permalink
fixed previous merges partially, there still seems to be a problem wi…
Browse files Browse the repository at this point in the history
…th the test client
  • Loading branch information
schmittjoh authored and fabpot committed Nov 17, 2011
1 parent d29715f commit 46e5fa5
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 6 deletions.
@@ -0,0 +1,19 @@
<?php

namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\Reference;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class FormLoginExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$container
->register('localized_form_failure_handler', 'Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Security\LocalizedFormFailureHandler')
->addArgument(new Reference('router'))
;
}
}
@@ -0,0 +1,24 @@
<?php

namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Security;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;

class LocalizedFormFailureHandler implements AuthenticationFailureHandlerInterface
{
private $router;

public function __construct(RouterInterface $router)
{
$this->router = $router;
}

public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
return new RedirectResponse($this->router->generate('localized_login_path', array(), true));
}
}
Expand Up @@ -26,6 +26,23 @@ public function testLoginLogoutProcedure($locale)
$this->assertEquals('Homepage', $client->followRedirect()->text());
}

/**
* @dataProvider getLocales
*/
public function testLoginFailureWithLocalizedFailurePath($locale)
{
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => 'localized_form_failure_handler.yml'));
$client->insulate();

$crawler = $client->request('GET', '/'.$locale.'/login');
$form = $crawler->selectButton('login')->form();
$form['_username'] = 'johannes';
$form['_password'] = 'foobar';
$client->submit($form);

$this->assertRedirect($client->getResponse(), '/'.$locale.'/login');
}

/**
* @dataProvider getLocales
*/
Expand Down
Expand Up @@ -18,7 +18,7 @@ class WebTestCase extends BaseWebTestCase
{
static public function assertRedirect($response, $location)
{
self::assertTrue($response->isRedirect(), 'Response is not a redirect, got status code: '.$response->getStatusCode());
self::assertTrue($response->isRedirect(), 'Response is not a redirect, got status code: '.substr($response, 0, 2000));
self::assertEquals('http://localhost'.$location, $response->headers->get('Location'));
}

Expand Down
@@ -0,0 +1,19 @@
imports:
- { resource: ./../config/default.yml }

security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext

providers:
in_memory:
users:
johannes: { password: test, roles: [ROLE_USER] }

firewalls:
default:
form_login:
login_path: localized_login_path
check_path: localized_check_path
failure_handler: localized_form_failure_handler
anonymous: ~
20 changes: 16 additions & 4 deletions src/Symfony/Component/HttpKernel/EventListener/LocaleListener.php
Expand Up @@ -27,7 +27,8 @@ class LocaleListener implements EventSubscriberInterface
private $router;
private $defaultLocale;

public function __construct($defaultLocale = 'en', RouterInterface $router = null)
public function __construct($defaultLocale = 'en', RouterInterface $router
= null)
{
$this->defaultLocale = $defaultLocale;
$this->router = $router;
Expand All @@ -37,10 +38,17 @@ public function onEarlyKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if ($request->hasPreviousSession()) {
$request->setDefaultLocale($request->getSession()->get('_locale', $this->defaultLocale));
$request->setDefaultLocale($request->getSession()->get('_locale',
$this->defaultLocale));
} else {
$request->setDefaultLocale($this->defaultLocale);
}

if (null !== $this->router && ($context = $this->router->getContext())
&& !$context->hasParameter('_locale')) {
$this->router->getContext()->setParameter('_locale',
$request->getLocale());
}
}

public function onKernelRequest(GetResponseEvent $event)
Expand All @@ -59,14 +67,18 @@ public function onKernelRequest(GetResponseEvent $event)
}

if (null !== $this->router) {
$this->router->getContext()->setParameter('_locale', $request->getLocale());
$this->router->getContext()->setParameter('_locale',
$request->getLocale());
}
}

static public function getSubscribedEvents()
{
return array(
KernelEvents::REQUEST => array(array('onEarlyKernelRequest', 255), array('onKernelRequest', -1)),
KernelEvents::REQUEST => array(
array('onEarlyKernelRequest', 253),
array('onKernelRequest', -1)
),
);
}
}
Expand Up @@ -93,7 +93,10 @@ private function parametersToString(array $parameters)
static public function getSubscribedEvents()
{
return array(
KernelEvents::REQUEST => array(array('onEarlyKernelRequest', 255), array('onKernelRequest', 10)),
KernelEvents::REQUEST => array(
array('onEarlyKernelRequest', 255),
array('onKernelRequest', 10)
),
);
}
}

0 comments on commit 46e5fa5

Please sign in to comment.