fixed previous merges partially, there still seems to be a problem with the test client
This commit is contained in:
parent
d29715f128
commit
46e5fa5c87
@ -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));
|
||||
}
|
||||
}
|
@ -26,6 +26,23 @@ class LocalizedRoutesAsPathTest extends WebTestCase
|
||||
$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
|
||||
*/
|
||||
|
@ -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'));
|
||||
}
|
||||
|
||||
|
@ -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: ~
|
@ -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;
|
||||
@ -37,10 +38,17 @@ class LocaleListener implements EventSubscriberInterface
|
||||
{
|
||||
$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)
|
||||
@ -59,14 +67,18 @@ class LocaleListener implements EventSubscriberInterface
|
||||
}
|
||||
|
||||
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)
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -93,7 +93,10 @@ class RouterListener implements EventSubscriberInterface
|
||||
static public function getSubscribedEvents()
|
||||
{
|
||||
return array(
|
||||
KernelEvents::REQUEST => array(array('onEarlyKernelRequest', 255), array('onKernelRequest', 10)),
|
||||
KernelEvents::REQUEST => array(
|
||||
array('onEarlyKernelRequest', 255),
|
||||
array('onKernelRequest', 10)
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user