8000 [SecurityBundle] use TokenStorageInterface instead of deprecated Secu… · symfony/symfony@cd4af9d · GitHub
[go: up one dir, main page]

Skip to content

Commit cd4af9d

Browse files
author
Hugo Hamon
committed
[SecurityBundle] use TokenStorageInterface instead of deprecated SecurityContextInterface in SecurityDataCollector and added unit tests suite.
1 parent acb1b85 commit cd4af9d

File tree

3 files changed

+120
-8
lines changed

3 files changed

+120
-8
lines changed

src/Symfony/Bundle/SecurityBundle/DataCollector/SecurityDataCollector.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111

1212
namespace Symfony\Bundle\SecurityBundle\DataCollector;
1313

14-
use Symfony\Component\Security\Core\SecurityContextInterface;
14+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Response;
1717
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
18+
use Symfony\Component\Security\Core\Role\RoleInterface;
1819

1920
/**
2021
* SecurityDataCollector.
@@ -23,27 +24,32 @@
2324
*/
2425
class SecurityDataCollector extends DataCollector
2526
{
26-
private $context;
27+
private $tokenStorage;
2728

28-
public function __construct(SecurityContextInterface $context = null)
29+
/**
30+
* Constructor.
31+
*
32+
* @param TokenStorageInterface $tokenStorage
33+
*/
34+
public function __construct(TokenStorageInterface $tokenStorage = null)
2935
{
30-
$this->context = $context;
36+
$this->tokenStorage = $tokenStorage;
3137
}
3238

3339
/**
3440
* {@inheritdoc}
3541
*/
3642
public function collect(Request $request, Response $response, \Exception $exception = null)
3743
{
38-
if (null === $this->context) {
44+
if (null === $this->tokenStorage) {
3945
$this->data = array(
4046
'enabled' => false,
4147
'authenticated' => false,
4248
'token_class' => null,
4349
'user' => '',
4450
'roles' => array(),
4551
);
46-
} elseif (null === $token = $this->context->getToken()) {
52+
} elseif (null === $token = $this->tokenStorage->getToken()) {
4753
$this->data = array(
4854
'enabled' => true,
4955
'authenticated' => false,
@@ -57,7 +63,7 @@ public function collect(Request $request, Response $response, \Exception $except
5763
'authenticated' => $token->isAuthenticated(),
5864
'token_class' => get_class($token),
5965
'user' => $token->getUsername(),
60-
'roles' => array_map(function ($role) { return $role->getRole();}, $token->getRoles()),
66+
'roles' => array_map(function (RoleInterface $role) { return $role->getRole();}, $token->getRoles()),
6167
);
6268
}
6369
}

src/Symfony/Bundle/SecurityBundle/Resources/config/collectors.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<services>
1212
<service id="data_collector.security" class="%data_collector.security.class%" public="false">
1313
<tag name="data_collector" template="@Security/Collector/security.html.twig" id="security" />
14-
<argument type="service" id="security.context" on-invalid="ignore" />
14+
<argument type="service" id="security.token_storage" on-invalid="ignore" />
1515
</service>
1616
</services>
1717
</container>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\SecurityBundle\Tests\DataCollector;
4+
5+
use Symfony\Bundle\SecurityBundle\DataCollector\SecurityDataCollector;
6+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
7+
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
8+
use Symfony\Component\Security\Core\Role\Role;
9+
10+
class SecurityDataCollectorTest extends \PHPUnit_Framework_TestCase
11+
{
12+
/**
13+
* Dummy request object.
14+
*/
15+
private $request;
16+
17+
/**
18+
* Dummy response object.
19+
*/
20+
private $response;
21+
22+
protected function setUp()
23+
{
24+
$this->request = $this
25+
->getMockBuilder('Symfony\Component\HttpFoundation\Request')
26+
->disableOriginalConstructor()
27+
9E7A ->getMock()
28+
;
29+
30+
$this->response = $this
31+
->getMockBuilder('Symfony\Component\HttpFoundation\Response')
32+
->disableOriginalConstructor()
33+
->getMock()
34+
;
35+
}
36+
37+
protected function tearDown()
38+
{
39+
$this->request = null;
40+
$this->response = null;
41+
}
42+
43+
public function testCollectWhenSecurityIsDisabled()
44+
{
45+
$collector = new SecurityDataCollector();
46+
$collector->collect($this->request, $this->response);
47+
48+
$this->assertSame('security', $collector->getName());
49+
$this->assertFalse($collector->isEnabled());
50+
$this->assertFalse($collector->isAuthenticated());
51+
$this->assertNull($collector->getTokenClass());
52+
$this->assertCount(0, $collector->getRoles());
53+
$this->assertEmpty($collector->getUser());
54+
}
55+
56+
/** @dataProvider provideTokenStorage */
57+
public function testCollectWhenAuthenticationTokenIsNull($tokenStorage)
58+
{
59+
$collector = new SecurityDataCollector($tokenStorage);
60+
$collector->collect($this->request, $this->response);
61+
62+
$this->assertTrue($collector->isEnabled());
63+
$this->assertFalse($collector->isAuthenticated());
64+
$this->assertNull($collector->getTokenClass());
65+
$this->assertCount(0, $collector->getRoles());
66+
$this->assertEmpty($collector->getUser());
67+
}
68+
69+
public function provideTokenStorage()
70+
{
71+
return array(
72+
array(new TokenStorage()),
73+
array($this->getMock('Symfony\Component\Security\Core\SecurityContextInterface')),
74+
);
75+
}
76+
77+
/** @dataProvider provideRoles */
78+
public function testCollectAuthenticationTokenAndRoles(array $roles, array $normalizedRoles)
79+
{
80+
$tokenStorage = new TokenStorage();
81+
$tokenStorage->setToken(new UsernamePasswordToken('hhamon', 'P4$$w0rD', 'provider', $roles));
82+
83+
$collector = new SecurityDataCollector($tokenStorage);
84+
$collector->collect($this->request, $this->response);
85+
86+
$this->assertTrue($collector->isEnabled());
87+
$this->assertTrue($collector->isAuthenticated());
88+
$this->assertSame('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $collector->getTokenClass());
89+
$this->assertSame($normalizedRoles, $collector->getRoles());
90+
$this->assertSame('hhamon', $collector->getUser());
91+
}
92+
93+
public function provideRoles()
94+
{
95+
return array(
96+
array(
97+
array('ROLE_USER'),
98+
array('ROLE_USER'),
99+
),
100+
array(
101+
array(new Role('ROLE_USER')),
102+
array('ROLE_USER'),
103+
),
104+
);
105+
}
106+
}

0 commit comments

Comments
 (0)
0