10000 minor #13045 [2.6] [SecurityBundle] use TokenStorageInterface instead… · symfony/symfony@188963f · GitHub
[go: up one dir, main page]

Skip to content

Commit 188963f

Browse files
committed
minor #13045 [2.6] [SecurityBundle] use TokenStorageInterface instead of deprecated SecurityContextInterface in SecurityDataCollector and added unit tests suite. (hhamon)
This PR was merged into the 2.6 branch. Discussion ---------- [2.6] [SecurityBundle] use TokenStorageInterface instead of deprecated SecurityContextInterface in SecurityDataCollector and added unit tests suite. | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | ~ | License | MIT | Doc PR | ~ Commits ------- ea4cf33 [SecurityBundle] use TokenStorageInterface instead of deprecated SecurityContextInterface in SecurityDataCollector and added unit tests suite.
2 parents c9129dd + ea4cf33 commit 188963f

File tree

3 files changed

+104
-8
lines changed

3 files changed

+104
-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|null $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: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
public function testCollectWhenSecurityIsDisabled()
13+
{
14+
$collector = new SecurityDataCollector();
15+
$collector->collect($this->getRequest(), $this->getResponse());
16+
17+
$this->assertSame('security', $collector->getName());
18+
$this->assertFalse($collector->isEnabled());
19+
$this->assertFalse($collector->isAuthenticated());
20+
$this->assertNull($collector->getTokenClass());
21+
$this->assertCount(0, $collector->getRoles());
22+
$this->assertEmpty($collector->getUser());
23+
}
24+
25+
/** @dataProvider provideTokenStorage */
26+
public function testCollectWhenAuthenticationTokenIsNull($tokenStorage)
27+
{
28+
$collector = new SecurityDataCollector($tokenStorage);
29+
$collector->collect($this->getRequest(), $this->getResponse());
30+
31+
$this->assertTrue($collector->isEnabled());
32+
$this->assertFalse($collector->isAuthenticated());
33+
$this->assertNull($collector->getTokenClass());
34+
$this->assertCount(0, $collector->getRoles());
35+
$this->assertEmpty($collector->getUser());
36+
}
37+
38+
public function provideTokenStorage()
39+
{
40+
return array(
41+
array(new TokenStorage()),
42+
array($this->getMock('Symfony\Component\Security\Core\SecurityContextInterface')),
43+
);
44+
}
45+
46+
/** @dataProvider provideRoles */
47+
public function testCollectAuthenticationTokenAndRoles(array $roles, array $normalizedRoles)
48+
{
49+
$tokenStorage = new TokenStorage();
50+
$tokenStorage->setToken(new UsernamePasswordToken('hhamon', 'P4$$w0rD', 'provider', $roles));
51+
52+
$collector = new SecurityDataCollector($tokenStorage);
53+
$collector->collect($this->getRequest(), $this->getResponse());
54+
55+
$this->assertTrue($collector->isEnabled());
56+
$this->assertTrue($collector->isAuthenticated());
57+
$this->assertSame('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $collector->getTokenClass());
58+
$this->assertSame($normalizedRoles, $collector->getRoles());
59+
$this->assertSame('hhamon', $collector->getUser());
60+
}
61+
62+
public function provideRoles()
63+
{
64+
return array(
65+
array(
66+
array('ROLE_USER'),
67+
array('ROLE_USER'),
68+
),
69+
array(
70+
array(new Role('ROLE_USER')),
71+
array('ROLE_USER'),
72+
),
73+
);
74+
}
75+
76+
private function getRequest()
77+
{
78+
return $this
79+
->getMockBuilder('Symfony\Component\HttpFoundation\Request')
80+
->disableOriginalConstructor()
81+
->getMock();
82+
}
83+
private function getResponse()
84+
{
85+
return $this
86+
->getMockBuilder('Symfony\Component\HttpFoundation\Response')
87+
->disableOriginalConstructor()
88+
->getMock();
89+
}
90+
}

0 commit comments

Comments
 (0)
0