8000 [SecurityBundle] replaced deprecated SecurityContextInterface depende… · symfony/symfony@337eb57 · GitHub
[go: up one dir, main page]

Skip to content

Commit 337eb57

Browse files
author
Hugo Hamon
committed
[SecurityBundle] replaced deprecated SecurityContextInterface dependency by new TokenStorageInterface instance in SecurityDataCollector. Also added unit tests suite for SecurityDataCollector class.
1 parent 5f86134 commit 337eb57

File tree

3 files changed

+134
-9
lines changed

3 files changed

+134
-9
lines changed

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
namespace Symfony\Bundle\SecurityBundle\DataCollector;
1313

14+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1415
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
15-
use Symfony\Component\Security\Core\SecurityContextInterface;
16+
use Symfony\Component\Security\Core\Role\RoleInterface;
1617
use Symfony\Component\HttpFoundation\Request;
1718
use Symfony\Component\HttpFoundation\Response;
1819
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
@@ -24,12 +25,18 @@
2425
*/
2526
class SecurityDataCollector extends DataCollector
2627
{
27-
private $context;
28+
private $tokenStorage;
2829
private $roleHierarchy;
2930

30-
public function __construct(SecurityContextInterface $context = null, RoleHierarchyInterface $roleHierarchy = null)
31+
/**
32+
* Constructor.
33+
*
34+
* @param TokenStorageInterface|null $tokenStorage
35+
* @param RoleHierarchyInterface|null $roleHierarchy
36+
*/
37+
public function __construct(TokenStorageInterface $tokenStorage = null, RoleHierarchyInterface $roleHierarchy = null)
3138
{
32-
$this->context = $context;
39+
$this->tokenStorage = $tokenStorage;
3340
$this->roleHierarchy = $roleHierarchy;
3441
}
3542

@@ -38,7 +45,7 @@ public function __construct(SecurityContextInterface $context = null, RoleHierar
3845
*/
3946
public function collect(Request $request, Response $response, \Exception $exception = null)
4047
{
41-
if (null === $this->context) {
48+
if (null === $this->tokenStorage) {
4249
$this->data = array(
4350
'enabled' => false,
4451
'authenticated' => false,
@@ -48,7 +55,7 @@ public function collect(Request $request, Response $response, \Exception $except
4855
'inherited_roles' => array(),
4956
'supports_role_hierarchy' => null !== $this->roleHierarchy,
5057
);
51-
} elseif (null === $token = $this->context->getToken()) {
58+
} elseif (null === $token = $this->tokenStorage->getToken()) {
5259
$this->data = array(
5360
'enabled' => true,
5461
'authenticated' => false,
@@ -74,8 +81,8 @@ public function collect(Request $request, Response $response, \Exception $except
7481
'authenticated' => $token->isAuthenticated(),
7582
'token_class' => get_class($token),
7683
'user' => $token->getUsername(),
77-
'roles' => array_map(function ($role) { return $role->getRole();}, $assignedRoles),
78-
'inherited_roles' => array_map(function ($role) { return $role->getRole();}, $inheritedRoles),
84+
'roles' => array_map(function (RoleInterface $role) { return $role->getRole();}, $assignedRoles),
85+
'inherited_roles' => array_map(function (RoleInterface $role) { return $role->getRole();}, $inheritedRoles),
7986
'supports_role_hierarchy' => null !== $this->roleHierarchy,
8087
);
8188
}

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
<argument type="service" id="security.role_hierarchy" />
1616
</service>
1717
</services>
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
use Symfony\Component\Security\Core\Role\RoleHierarchy;
10+
11+
class SecurityDataCollectorTest extends \PHPUnit_Framework_TestCase
12+
{
13+
public function testCollectWhenSecurityIsDisabled()
14+
{
15+
$collector = new SecurityDataCollector();
16+
$collector->collect($this->getRequest(), $this->getResponse());
17+
18+
$this->assertSame('security', $collector->getName());
19+
$this->assertFalse($collector->isEnabled());
20+
$this->assertFalse($collector->isAuthenticated());
21+
$this->assertNull($collector->getTokenClass());
22+
$this->assertFalse($collector->supportsRoleHierarchy());
23+
$this->assertCount(0, $collector->getRoles());
24+
$this->assertCount(0, $collector->getInheritedRoles());
25+
$this->assertEmpty($collector->getUser());
26+
}
27+
28+
/** @dataProvider provideTokenStorage */
29+
public function testCollectWhenAuthenticationTokenIsNull($tokenStorage)
30+
{
31+
$collector = new SecurityDataCollector($tokenStorage, $this->getRoleHierarchy());
32+
$collector->collect($this->getRequest(), $this->getResponse());
33+
34+
$this->assertTrue($collector->isEnabled());
35+
$this->assertFalse($collector->isAuthenticated());
36+
$this->assertNull($collector->getTokenClass());
37+
$this->assertTrue($collector->supportsRoleHierarchy());
38+
$this->assertCount(0, $collector->getRoles());
39+
$this->assertCount(0, $collector->getInheritedRoles());
40+
$this->assertEmpty($collector->getUser());
41+
}
42+
43+
public function provideTokenStorage()
44+
{
45+
return array(
46+
array(new TokenStorage()),
47+
array($this->getMock('Symfony\Component\Security\Core\SecurityContextInterface')),
48+
);
49+
}
50+
51+
/** @dataProvider provideRoles */
52+
public function testCollectAuthenticationTokenAndRoles(array $roles, array $normalizedRoles, array $inheritedRoles)
53+
{
54+
$tokenStorage = new TokenStorage();
55+
$tokenStorage->setToken(new UsernamePasswordToken('hhamon', 'P4$$w0rD', 'provider', $roles));
56+
57+
$collector = new SecurityDataCollector($tokenStorage, $this->getRoleHierarchy());
58+
$collector->collect($this->getRequest(), $this->getResponse());
59+
60+
$this->assertTrue($collector->isEnabled());
61+
$this->assertTrue($collector->isAuthenticated());
62+
$this->assertSame('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $collector->getTokenClass());
63+
$this->assertTrue($collector->supportsRoleHierarchy());
64+
$this->assertSame($normalizedRoles, $collector->getRoles());
65+
$this->assertSame($inheritedRoles, $collector->getInheritedRoles());
66+
$this->assertSame('hhamon', $collector->getUser());
67+
}
68+
69+
public function provideRoles()
70+
{
71+
return array(
72+
// Basic roles
73+
array(
74+
array('ROLE_USER'),
75+
array('ROLE_USER'),
76+
array(),
77+
),
78+
array(
79+
array(new Role('ROLE_USER')),
80+
array('ROLE_USER'),
81+
array(),
82+
),
83+
// Inherited roles
84+
array(
85+
array('ROLE_ADMIN'),
86+
array('ROLE_ADMIN'),
87+
array('ROLE_USER', 'ROLE_ALLOWED_TO_SWITCH'),
88+
),
89+
array(
90+
array(new Role('ROLE_ADMIN')),
91+
array('ROLE_ADMIN'),
92+
array('ROLE_USER', 'ROLE_ALLOWED_TO_SWITCH'),
93+
),
94+
);
95+
}
96+
97+
private function getRoleHierarchy()
98+
{
99+
return new RoleHierarchy(array(
100+
'ROLE_ADMIN' => array('ROLE_USER', 'ROLE_ALLOWED_TO_SWITCH'),
101+
));
102+
}
103+
104+
private function getRequest()
105+
{
106+
return $this
107+
->getMockBuilder('Symfony\Component\HttpFoundation\Request')
108+
->disableOriginalConstructor()
109+
->getMock();
110+
}
111+
private function getResponse()
112+
{
113+
return $this
114+
->getMockBuilder('Symfony\Component\HttpFoundation\Response')
115+
->disableOriginalConstructor()
116+
->getMock();
117+
}
118+
}

0 commit comments

Comments
 (0)
0