Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Commit f7d4e3b

Browse files
Merge branch '3.1'
* 3.1: (22 commits) [travis] Fix deps=low/high builds [Form] Fix depreciation triggers fixed CS skip test with current phpunit bridge Fix for #19183 to add support for new PHP MongoDB extension in sessions. [Console] Fix for block() padding formatting after #19189 [Security][Guard] check if session exist before using it bumped Symfony version to 3.1.3 updated VERSION for 3.1.2 updated CHANGELOG for 3.1.2 bumped Symfony version to 3.0.9 updated VERSION for 3.0.8 updated CHANGELOG for 3.0.8 bumped Symfony version to 2.8.9 updated VERSION for 2.8.8 updated CHANGELOG for 2.8.8 bumped Symfony version to 2.7.16 updated VERSION for 2.7.15 update CONTRIBUTORS for 2.7.15 updated CHANGELOG for 2.7.15 ... Conflicts: src/Symfony/Component/HttpKernel/Kernel.php
2 parents 02478ad + 8a9f6c7 commit f7d4e3b

File tree

3 files changed

+233
-68
lines changed

3 files changed

+233
-68
lines changed

Guard/Authenticator/AbstractFormLoginAuthenticator.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Security\Guard\Authenticator;
1313

14+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
1415
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
1516
use Symfony\Component\HttpFoundation\RedirectResponse;
1617
use Symfony\Component\HttpFoundation\Request;
@@ -45,7 +46,10 @@ abstract protected function getLoginUrl();
4546
*/
4647
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
4748
{
48-
$request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);
49+
if ($request->getSession() instanceof SessionInterface) {
50+
$request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);
51+
}
52+
4953
$url = $this->getLoginUrl();
5054

5155
return new RedirectResponse($url);
@@ -65,12 +69,16 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token,
6569
@trigger_error(sprintf('The AbstractFormLoginAuthenticator::onAuthenticationSuccess() implementation was deprecated in Symfony 3.1 and will be removed in Symfony 4.0. You should implement this method yourself in %s and remove getDefaultSuccessRedirectUrl().', get_class($this)), E_USER_DEPRECATED);
6670

6771
if (!method_exists($this, 'getDefaultSuccessRedirectUrl')) {
68-
throw new \Exception(sprintf('You must implement onAuthenticationSuccess() or getDefaultSuccessRedirectURL() in %s.', get_class($this)));
72+
throw new \Exception(sprintf('You must implement onAuthenticationSuccess() or getDefaultSuccessRedirectUrl() in %s.', get_class($this)));
6973
}
7074

71-
// if the user hits a secure page and start() was called, this was
75+
$targetPath = null;
76+
77+
// if the user hit a secure page and start() was called, this was
7278
// the URL they were on, and probably where you want to redirect to
73-
$targetPath = $this->getTargetPath($request->getSession(), $providerKey);
79+
if ($request->getSession() instanceof SessionInterface) {
80+
$targetPath = $this->getTargetPath($request->getSession(), $providerKey);
81+
}
7482

7583
if (!$targetPath) {
7684
$targetPath = $this->getDefaultSuccessRedirectUrl();

Guard/Tests/Authenticator/AbstractFormLoginAuthenticatorTest.php

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Guard\Tests\Authenticator;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
16+
use Symfony\Component\Security\Core\User\UserInterface;
17+
use Symfony\Component\Security\Core\User\UserProviderInterface;
18+
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
19+
20+
/**
21+
* @author Jean Pasdeloup <jpasdeloup@sedona.fr>
22+
*/
23+
class FormLoginAuthenticatorTest extends \PHPUnit_Framework_TestCase
24+
{
25+
private $requestWithoutSession;
26+
private $requestWithSession;
27+
private $authenticator;
28+
29+
const LOGIN_URL = 'http://login';
30+
const DEFAULT_SUCCESS_URL = 'http://defaultsuccess';
31+
const CUSTOM_SUCCESS_URL = 'http://customsuccess';
32+
33+
public function testAuthenticationFailureWithoutSession()
34+
{
35+
$failureResponse = $this->authenticator->onAuthenticationFailure($this->requestWithoutSession, new AuthenticationException());
36+
37+
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $failureResponse);
38+
$this->assertEquals(self::LOGIN_URL, $failureResponse->getTargetUrl());
39+
}
40+
41+
public function testAuthenticationFailureWithSession()
42+
{
43+
$this->requestWithSession->getSession()
44+
->expects($this->once())
45+
->method('set');
46+
47+
$failureResponse = $this->authenticator->onAuthenticationFailure($this->requestWithSession, new AuthenticationException());
48+
49+
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $failureResponse);
50+
$this->assertEquals(self::LOGIN_URL, $failureResponse->getTargetUrl());
51+
}
52+
53+
/**
54+
* @group legacy
55+
*/
56+
public function testAuthenticationSuccessWithoutSession()
57+
{
58+
$token = $this->getMockBuilder('Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface')
59+
->disableOriginalConstructor()
60+
->getMock();
61+
62+
$redirectResponse = $this->authenticator->onAuthenticationSuccess($this->requestWithoutSession, $token, 'providerkey');
63+
64+
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $redirectResponse);
65+
$this->assertEquals(self::DEFAULT_SUCCESS_URL, $redirectResponse->getTargetUrl());
66+
}
67+
68+
/**
69+
* @group legacy
70+
*/
71+
public function testAuthenticationSuccessWithSessionButEmpty()
72+
{
73+
$token = $this->getMockBuilder('Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface')
74+
->disableOriginalConstructor()
75+
->getMock();
76+
$this->requestWithSession->getSession()
77+
->expects($this->once())
78+
->method('get')
79+
->will($this->returnValue(null));
80+
81+
$redirectResponse = $this->authenticator->onAuthenticationSuccess($this->requestWithSession, $token, 'providerkey');
82+
83+
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $redirectResponse);
84+
$this->assertEquals(self::DEFAULT_SUCCESS_URL, $redirectResponse->getTargetUrl());
85+
}
86+
87+
/**
88+
* @group legacy
89+
*/
90+
public function testAuthenticationSuccessWithSessionAndTarget()
91+
{
92+
$token = $this->getMockBuilder('Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface')
93+
->disableOriginalConstructor()
94+
->getMock();
95+
$this->requestWithSession->getSession()
96+
->expects($this->once())
97+
->method('get')
98+
->will($this->returnValue(self::CUSTOM_SUCCESS_URL));
99+
100+
$redirectResponse = $this->authenticator->onAuthenticationSuccess($this->requestWithSession, $token, 'providerkey');
101+
102+
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $redirectResponse);
103+
$this->assertEquals(self::CUSTOM_SUCCESS_URL, $redirectResponse->getTargetUrl());
104+
}
105+
106+
public function testRememberMe()
107+
{
108+
$doSupport = $this->authenticator->supportsRememberMe();
109+
110+
$this->assertTrue($doSupport);
111+
}
112+
113+
public function testStartWithoutSession()
114+
{
115+
$failureResponse = $this->authenticator->start($this->requestWithoutSession, new AuthenticationException());
116+
117+
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $failureResponse);
118+
$this->assertEquals(self::LOGIN_URL, $failureResponse->getTargetUrl());
119+
}
120+
121+
public function testStartWithSession()
122+
{
123+
$failureResponse = $this->authenticator->start($this->requestWithSession, new AuthenticationException());
124+
125+
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $failureResponse);
126+
$this->assertEquals(self::LOGIN_URL, $failureResponse->getTargetUrl());
127+
}
128+
129+
protected function setUp()
130+
{
131+
$this->requestWithoutSession = new Request(array(), array(), array(), array(), array(), array());
132+
$this->requestWithSession = new Request(array(), array(), array(), array(), array(), array());
133+
134+
$session = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\Session\\SessionInterface')
135+
->disableOriginalConstructor()
136+
->getMock();
137+
$this->requestWithSession->setSession($session);
138+
139+
$this->authenticator = new TestFormLoginAuthenticator();
140+
$this->authenticator
141+
->setLoginUrl(self::LOGIN_URL)
142+
->setDefaultSuccessRedirectUrl(self::DEFAULT_SUCCESS_URL)
143+
;
144+
}
145+
146+
protected function tearDown()
147+
{
148+
$this->request = null;
149+
$this->requestWithSession = null;
150+
}
151+
}
152+
153+
class TestFormLoginAuthenticator extends AbstractFormLoginAuthenticator
154+
{
155+
private $loginUrl;
156+
private $defaultSuccessRedirectUrl;
157+
158+
/**
159+
* @param mixed $defaultSuccessRedirectUrl
160+
*
161+
* @return TestFormLoginAuthenticator
162+
*/
163+
public function setDefaultSuccessRedirectUrl($defaultSuccessRedirectUrl)
164+
{
165+
$this->defaultSuccessRedirectUrl = $defaultSuccessRedirectUrl;
166+
167+
return $this;
168+
}
169+
170+
/**
171+
* @param mixed $loginUrl
172+
*
173+
* @return TestFormLoginAuthenticator
174+
*/
175+
public function setLoginUrl($loginUrl)
176+
{
177+
$this->loginUrl = $loginUrl;
178+
179+
return $this;
180+
}
181+
182+
/**
183+
* {@inheritdoc}
184+
*/
185+
protected function getLoginUrl()
186+
{
187+
return $this->loginUrl;
188+
}
189+
190+
/**
191+
* {@inheritdoc}
192+
*/
193+
protected function getDefaultSuccessRedirectUrl()
194+
{
195+
return $this->defaultSuccessRedirectUrl;
196+
}
197+
198+
/**
199+
* {@inheritdoc}
200+
*/
201+
public function getCredentials(Request $request)
202+
{
203+
return 'credentials';
204+
}
205+
206+
/**
207+
* {@inheritdoc}
208+
*/
209+
public function getUser($credentials, UserProviderInterface $userProvider)
210+
{
211+
return $userProvider->loadUserByUsername($credentials);
212+
}
213+
214+
/**
215+
* {@inheritdoc}
216+
*/
217+
public function checkCredentials($credentials, UserInterface $user)
218+
{
219+
return true;
220+
}
221+
}

0 commit comments

Comments
 (0)