Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dfe8c81

Browse files
Merge branch '3.4' into 4.4
* 3.4: Handle fetch mode deprecation of DBAL 2.11. Fixed handling of CSRF logout error
2 parents 9f52228 + e1e314d commit dfe8c81

File tree

6 files changed

+115
-7
lines changed

6 files changed

+115
-7
lines changed

src/Symfony/Bridge/Doctrine/Security/RememberMe/DoctrineTokenProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function loadTokenBySeries($series)
6363
$paramValues = ['series' => $series];
6464
$paramTypes = ['series' => \PDO::PARAM_STR];
6565
$stmt = $this->conn->executeQuery($sql, $paramValues, $paramTypes);
66-
$row = $stmt->fetch(\PDO::FETCH_ASSOC);
66+
$row = method_exists($stmt, 'fetchAssociative') ? $stmt->fetchAssociative() : $stmt->fetch(\PDO::FETCH_ASSOC);
6767

6868
if ($row) {
6969
return new PersistentToken($row['class'], $row['username'], $series, $row['value'], new \DateTime($row['last_used']));
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace Security\RememberMe;
4+
5+
use Doctrine\DBAL\DriverManager;
6+
use PHPUnit\Framework\TestCase;
7+
use Symfony\Bridge\Doctrine\Security\RememberMe\DoctrineTokenProvider;
8+
use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentToken;
9+
use Symfony\Component\Security\Core\Exception\TokenNotFoundException;
10+
11+
/**
12+
* @requires extension pdo_sqlite
13+
*/
14+
class DoctrineTokenProviderTest extends TestCase
15+
{
16+
public static function setUpBeforeClass()
17+
{
18+
if (\PHP_VERSION_ID >= 80000) {
19+
self::markTestSkipped('Doctrine DBAL 2.x is incompatible with PHP 8.');
20+
}
21+
}
22+
23+
public function testCreateNewToken()
24+
{
25+
$provider = $this->bootstrapProvider();
26+
27+
$token = new PersistentToken('someClass', 'someUser', 'someSeries', 'tokenValue', new \DateTime('2013-01-26T18:23:51'));
28+
$provider->createNewToken($token);
29+
30+
$this->assertEquals($provider->loadTokenBySeries('someSeries'), $token);
31+
}
32+
33+
public function testLoadTokenBySeriesThrowsNotFoundException()
34+
{
35+
$provider = $this->bootstrapProvider();
36+
37+
$this->expectException(TokenNotFoundException::class);
38+
$provider->loadTokenBySeries('someSeries');
39+
}
40+
41+
public function testUpdateToken()
42+
{
43+
$provider = $this->bootstrapProvider();
44+
45+
$token = new PersistentToken('someClass', 'someUser', 'someSeries', 'tokenValue', new \DateTime('2013-01-26T18:23:51'));
46+
$provider->createNewToken($token);
47+
$provider->updateToken('someSeries', 'newValue', $lastUsed = new \DateTime('2014-06-26T22:03:46'));
48+
$token = $provider->loadTokenBySeries('someSeries');
49+
50+
$this->assertEquals('newValue', $token->getTokenValue());
51+
$this->assertEquals($token->getLastUsed(), $lastUsed);
52+
}
53+
54+
public function testDeleteToken()
55+
{
56+
$provider = $this->bootstrapProvider();
57+
$token = new PersistentToken('someClass', 'someUser', 'someSeries', 'tokenValue', new \DateTime('2013-01-26T18:23:51'));
58+
$provider->createNewToken($token);
59+
$provider->deleteTokenBySeries('someSeries');
60+
61+
$this->expectException(TokenNotFoundException::class);
62+
63+
$provider->loadTokenBySeries('someSeries');
64+
}
65+
66+
/**
67+
* @return DoctrineTokenProvider
68+
*/
69+
private function bootstrapProvider()
70+
{
71+
$connection = DriverManager::getConnection([
72+
'driver' => 'pdo_sqlite',
73+
'url' => 'sqlite:///:memory:',
74+
]);
75+
$connection->executeUpdate(<<< 'SQL'
76+
CREATE TABLE rememberme_token (
77+
series char(88) UNIQUE PRIMARY KEY NOT NULL,
78+
value char(88) NOT NULL,
79+
lastUsed datetime NOT NULL,
80+
class varchar(100) NOT NULL,
81+
username varchar(200) NOT NULL
82+
);
83+
SQL
84+
);
85+
86+
return new DoctrineTokenProvider($connection);
87+
}
88+
}

src/Symfony/Component/Cache/Tests/Traits/PdoPruneableTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ protected function isPruned($cache, string $name): bool
2424
$getPdoConn = $o->getMethod('getConnection');
2525
$getPdoConn->setAccessible(true);
2626

27-
/** @var \Doctrine\DBAL\Statement $select */
27+
/** @var \Doctrine\DBAL\Statement|\PDOStatement $select */
2828
$select = $getPdoConn->invoke($cache)->prepare('SELECT 1 FROM cache_items WHERE item_id LIKE :id');
2929
$select->bindValue(':id', sprintf('%%%s', $name));
3030
$select->execute();
3131

32-
return 0 === \count($select->fetchAll(\PDO::FETCH_COLUMN));
32+
return 1 !== (int) (method_exists($select, 'fetchOne') ? $select->fetchOne() : $select->fetch(\PDO::FETCH_COLUMN));
3333
}
3434
}

src/Symfony/Component/Cache/Traits/PdoTrait.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,13 @@ protected function doFetch(array $ids)
194194
}
195195
$stmt->execute();
196196

197-
while ($row = $stmt->fetch(\PDO::FETCH_NUM)) {
197+
if (method_exists($stmt, 'iterateNumeric')) {
198+
$stmt = $stmt->iterateNumeric();
199+
} else {
200+
$stmt->setFetchMode(\PDO::FETCH_NUM);
201+
}
202+
203+
foreach ($stmt as $row) {
198204
if (null === $row[1]) {
199205
$expired[] = $row[0];
200206
} else {

src/Symfony/Component/Security/Http/Firewall/ExceptionListener.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function onKernelException(GetResponseForExceptionEvent $event)
111111
}
112112

113113
if ($exception instanceof LogoutException) {
114-
$this->handleLogoutException($exception);
114+
$this->handleLogoutException($event, $exception);
115115

116116
return;
117117
}
@@ -181,10 +181,12 @@ private function handleAccessDeniedException(GetResponseForExceptionEvent $event
181181
}
182182
}
183183

184-
private function handleLogoutException(LogoutException $exception): void
184+
private function handleLogoutException(GetResponseForExceptionEvent $event, LogoutException $exception): void
185185
{
186+
$event->setException(new AccessDeniedHttpException($exception->getMessage(), $exception));
187+
186188
if (null !== $this->logger) {
187-
$this->logger->info('A LogoutException was thrown.', ['exception' => $exception]);
189+
$this->logger->info('A LogoutException was thrown; wrapping with AccessDeniedHttpException', ['exception' => $exception]);
188190
}
189191
}
190192

src/Symfony/Component/Security/Http/Tests/Firewall/ExceptionListenerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
2222
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
2323
use Symfony\Component\Security\Core\Exception\AuthenticationException;
24+
use Symfony\Component\Security\Core\Exception\LogoutException;
2425
use Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface;
2526
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
2627
use Symfony\Component\Security\Http\Firewall\ExceptionListener;
@@ -157,6 +158,17 @@ public function testAccessDeniedExceptionNotFullFledged(\Exception $exception, \
157158
$this->assertSame(null === $eventException ? $exception : $eventException, $event->getThrowable()->getPrevious());
158159
}
159160

161+
public function testLogoutException()
162+
{
163+
$event = $this->createEvent(new LogoutException('Invalid CSRF.'));
164+
165+
$listener = $this->createExceptionListener();
166+
$listener->onKernelException($event);
167+
168+
$this->assertEquals('Invalid CSRF.', $event->getException()->getMessage());
169+
$this->assertEquals(403, $event->getException()->getStatusCode());
170+
}
171+
160172
public function getAccessDeniedExceptionProvider()
161173
{
162174
return [

0 commit comments

Comments
 (0)