Skip to content

Http util fixes #1739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 22, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions UPDATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ RC4 to RC5
Session::getAttributes() -> Session::all()
Session::setAttributes() -> Session::replace()

* {_locale} is not supported in paths in the access_control section anymore. You can
rewrite the paths using a regular expression such as "(?:[a-z]{2})".

RC3 to RC4
----------

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Symfony\Bundle\SecurityBundle\Tests\Functional;

class AuthenticationCommencingTest extends WebTestCase
{
public function testAuthenticationIsCommencingIfAccessDeniedExceptionIsWrapped()
{
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => 'config.yml'));
$client->insulate();

$client->request('GET', '/secure-but-not-covered-by-access-control');
$this->assertRedirect($client->getResponse(), '/login');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

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

use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerAware;

class LocalizedController extends ContainerAware
{
public function loginAction()
{
// get the login error if there is one
if ($this->container->get('request')->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $this->container->get('request')->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else {
$error = $this->container->get('request')->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
}

return $this->container->get('templating')->renderResponse('FormLoginBundle:Localized:login.html.twig', array(
// last username entered by the user
'last_username' => $this->container->get('request')->getSession()->get(SecurityContext::LAST_USERNAME),
'error' => $error,
));
}

public function loginCheckAction()
{
throw new \RuntimeException('loginCheckAction() should never be called.');
}

public function logoutAction()
{
throw new \RuntimeException('logoutAction() should never be called.');
}

public function secureAction()
{
throw new \RuntimeException('secureAction() should never be called.');
}

public function profileAction()
{
return new Response('Profile');
}

public function homepageAction()
{
return new Response('Homepage');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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

use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\DependencyInjection\ContainerAware;
Expand Down Expand Up @@ -42,4 +43,9 @@ public function loginCheckAction()
{
return new Response('', 400);
}

public function secureAction()
{
throw new \Exception('Wrapper', 0, new \Exception('Another Wrapper', 0, new AccessDeniedException()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
localized_login_path:
pattern: /{_locale}/login
defaults: { _controller: FormLoginBundle:Localized:login }
requirements: { _locale: "^[a-z]{2}$" }

localized_check_path:
pattern: /{_locale}/login_check
defaults: { _controller: FormLoginBundle:Localized:loginCheck }
requirements: { _locale: "^[a-z]{2}$" }

localized_default_target_path:
pattern: /{_locale}/profile
defaults: { _controller: FormLoginBundle:Localized:profile }
requirements: { _locale: "^[a-z]{2}$" }

localized_logout_path:
pattern: /{_locale}/logout
defaults: { _controller: FormLoginBundle:Localized:logout }
requirements: { _locale: "^[a-z]{2}$" }

localized_logout_target_path:
pattern: /{_locale}/
defaults: { _controller: FormLoginBundle:Localized:homepage }
requirements: { _locale: "^[a-z]{2}$" }

localized_secure_path:
pattern: /{_locale}/secure/
defaults: { _controller: FormLoginBundle:Localized:secure }
requirements: { _locale: "^[a-z]{2}$" }

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ form_login_custom_target_path:
pattern: /foo
defaults: { _controller: FormLoginBundle:Login:afterLogin }

form_login_default_target_path:
pattern: /profile
defaults: { _controller: FormLoginBundle:Login:afterLogin }

form_login_redirect_to_protected_resource_after_login:
pattern: /protected-resource
defaults: { _controller: FormLoginBundle:Login:afterLogin }

form_logout:
pattern: /logout_path

form_secure_action:
pattern: /secure-but-not-covered-by-access-control
defaults: { _controller: FormLoginBundle:Login:secure }
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends "::base.html.twig" %}

{% block body %}

{% if error %}
<div>{{ error.message }}</div>
{% endif %}

<form action="{{ path('localized_check_path') }}" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />

<label for="password">Password:</label>
<input type="password" id="password" name="_password" />

<input type="hidden" name="_target_path" value="" />

<input type="submit" name="login" />
</form>

{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,33 @@
*/
class FormLoginTest extends WebTestCase
{
public function testFormLogin()
/**
* @dataProvider getConfigs
*/
public function testFormLogin($config)
{
$client = $this->createClient(array('test_case' => 'StandardFormLogin'));
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config));
$client->insulate();

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

$this->assertRedirect($client->getResponse(), '/');
$this->assertRedirect($client->getResponse(), '/profile');

$text = $client->followRedirect()->text();
$this->assertContains('Hello johannes!', $text);
$this->assertContains('You\'re browsing to path "/".', $text);
$this->assertContains('You\'re browsing to path "/profile".', $text);
}

public function testFormLoginWithCustomTargetPath()
/**
* @dataProvider getConfigs
*/
public function testFormLoginWithCustomTargetPath($config)
{
$client = $this->createClient(array('test_case' => 'StandardFormLogin'));
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config));
$client->insulate();

$form = $client->request('GET', '/login')->selectButton('login')->form();
$form['_username'] = 'johannes';
Expand All @@ -49,9 +57,13 @@ public function testFormLoginWithCustomTargetPath()
$this->assertContains('You\'re browsing to path "/foo".', $text);
}

public function testFormLoginRedirectsToProtectedResourceAfterLogin()
/**
* @dataProvider getConfigs
*/
public function testFormLoginRedirectsToProtectedResourceAfterLogin($config)
{
$client = $this->createClient(array('test_case' => 'StandardFormLogin'));
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config));
$client->insulate();

$client->request('GET', '/protected-resource');
$this->assertRedirect($client->getResponse(), '/login');
Expand All @@ -67,6 +79,14 @@ public function testFormLoginRedirectsToProtectedResourceAfterLogin()
$this->assertContains('You\'re browsing to path "/protected-resource".', $text);
}

public function getConfigs()
{
return array(
array('config.yml'),
array('routes_as_path.yml'),
);
}

protected function setUp()
{
parent::setUp();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Symfony\Bundle\SecurityBundle\Tests\Functional;

class LocalizedRoutesAsPathTest extends WebTestCase
{
/**
* @dataProvider getLocales
*/
public function testLoginLogoutProcedure($locale)
{
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => 'localized_routes.yml'));
$client->insulate();

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

$this->assertRedirect($client->getResponse(), '/'.$locale.'/profile');
$this->assertEquals('Profile', $client->followRedirect()->text());

$client->request('GET', '/'.$locale.'/logout');
$this->assertRedirect($client->getResponse(), '/'.$locale.'/');
$this->assertEquals('Homepage', $client->followRedirect()->text());
}

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

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

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

$crawler = $client->request('GET', '/'.$locale.'/secure/');
$this->assertEquals(1, count($crawler->selectButton('login')), (string) $client->getResponse());
}

public function getLocales()
{
return array(array('en'), array('de'));
}

protected function setUp()
{
parent::setUp();

$this->deleteTmpDir('StandardFormLogin');
}

protected function tearDown()
{
parent::setUp();

$this->deleteTmpDir('StandardFormLogin');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,37 @@

class SecurityRoutingIntegrationTest extends WebTestCase
{
public function testRoutingErrorIsNotExposedForProtectedResourceWhenAnonymous()
/**
* @dataProvider getConfigs
*/
public function testRoutingErrorIsNotExposedForProtectedResourceWhenAnonymous($config)
{
$client = $this->createClient(array('test_case' => 'StandardFormLogin'));
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config));
$client->insulate();
$client->request('GET', '/protected_resource');

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

public function testRoutingErrorIsExposedWhenNotProtected()
/**
* @dataProvider getConfigs
*/
public function testRoutingErrorIsExposedWhenNotProtected($config)
{
$client = $this->createClient(array('test_case' => 'StandardFormLogin'));
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config));
$client->insulate();
$client->request('GET', '/unprotected_resource');

$this->assertEquals(404, $client->getResponse()->getStatusCode());
$this->assertEquals(404, $client->getResponse()->getStatusCode(), (string) $client->getResponse());
}

public function testRoutingErrorIsNotExposedForProtectedResourceWhenLoggedInWithInsufficientRights()
/**
* @dataProvider getConfigs
*/
public function testRoutingErrorIsNotExposedForProtectedResourceWhenLoggedInWithInsufficientRights($config)
{
$client = $this->createClient(array('test_case' => 'StandardFormLogin'));
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config));
$client->insulate();

$form = $client->request('GET', '/login')->selectButton('login')->form();
$form['_username'] = 'johannes';
Expand All @@ -43,6 +55,11 @@ public function testRoutingErrorIsNotExposedForProtectedResourceWhenLoggedInWith
$this->assertNotEquals(404, $client->getResponse()->getStatusCode());
}

public function getConfigs()
{
return array(array('config.yml'), array('routes_as_path.yml'));
}

protected function setUp()
{
parent::setUp();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class WebTestCase extends BaseWebTestCase
{
static public function assertRedirect($response, $location)
{
self::assertTrue($response->isRedirect());
self::assertTrue($response->isRedirect(), 'Response is not a redirect, got status code: '.$response->getStatusCode());
self::assertEquals('http://localhost'.$location, $response->headers->get('Location'));
}

Expand Down
Loading