[SecurityBundle] replaced deprecated SecurityContextInterface dependency by new TokenStorageInterface instance in SecurityDataCollector. Also added unit tests suite for SecurityDataCollector class.
This commit is contained in:
parent
5f861347b2
commit
337eb57c48
@ -11,8 +11,9 @@
|
||||
|
||||
namespace Symfony\Bundle\SecurityBundle\DataCollector;
|
||||
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
|
||||
use Symfony\Component\Security\Core\SecurityContextInterface;
|
||||
use Symfony\Component\Security\Core\Role\RoleInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
|
||||
@ -24,12 +25,18 @@ use Symfony\Component\HttpKernel\DataCollector\DataCollector;
|
||||
*/
|
||||
class SecurityDataCollector extends DataCollector
|
||||
{
|
||||
private $context;
|
||||
private $tokenStorage;
|
||||
private $roleHierarchy;
|
||||
|
||||
public function __construct(SecurityContextInterface $context = null, RoleHierarchyInterface $roleHierarchy = null)
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param TokenStorageInterface|null $tokenStorage
|
||||
* @param RoleHierarchyInterface|null $roleHierarchy
|
||||
*/
|
||||
public function __construct(TokenStorageInterface $tokenStorage = null, RoleHierarchyInterface $roleHierarchy = null)
|
||||
{
|
||||
$this->context = $context;
|
||||
$this->tokenStorage = $tokenStorage;
|
||||
$this->roleHierarchy = $roleHierarchy;
|
||||
}
|
||||
|
||||
@ -38,7 +45,7 @@ class SecurityDataCollector extends DataCollector
|
||||
*/
|
||||
public function collect(Request $request, Response $response, \Exception $exception = null)
|
||||
{
|
||||
if (null === $this->context) {
|
||||
if (null === $this->tokenStorage) {
|
||||
$this->data = array(
|
||||
'enabled' => false,
|
||||
'authenticated' => false,
|
||||
@ -48,7 +55,7 @@ class SecurityDataCollector extends DataCollector
|
||||
'inherited_roles' => array(),
|
||||
'supports_role_hierarchy' => null !== $this->roleHierarchy,
|
||||
);
|
||||
} elseif (null === $token = $this->context->getToken()) {
|
||||
} elseif (null === $token = $this->tokenStorage->getToken()) {
|
||||
$this->data = array(
|
||||
'enabled' => true,
|
||||
'authenticated' => false,
|
||||
@ -74,8 +81,8 @@ class SecurityDataCollector extends DataCollector
|
||||
'authenticated' => $token->isAuthenticated(),
|
||||
'token_class' => get_class($token),
|
||||
'user' => $token->getUsername(),
|
||||
'roles' => array_map(function ($role) { return $role->getRole();}, $assignedRoles),
|
||||
'inherited_roles' => array_map(function ($role) { return $role->getRole();}, $inheritedRoles),
|
||||
'roles' => array_map(function (RoleInterface $role) { return $role->getRole();}, $assignedRoles),
|
||||
'inherited_roles' => array_map(function (RoleInterface $role) { return $role->getRole();}, $inheritedRoles),
|
||||
'supports_role_hierarchy' => null !== $this->roleHierarchy,
|
||||
);
|
||||
}
|
||||
|
@ -11,7 +11,7 @@
|
||||
<services>
|
||||
<service id="data_collector.security" class="%data_collector.security.class%" public="false">
|
||||
<tag name="data_collector" template="@Security/Collector/security.html.twig" id="security" />
|
||||
<argument type="service" id="security.context" on-invalid="ignore" />
|
||||
<argument type="service" id="security.token_storage" on-invalid="ignore" />
|
||||
<argument type="service" id="security.role_hierarchy" />
|
||||
</service>
|
||||
</services>
|
||||
|
@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Bundle\SecurityBundle\Tests\DataCollector;
|
||||
|
||||
use Symfony\Bundle\SecurityBundle\DataCollector\SecurityDataCollector;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Symfony\Component\Security\Core\Role\RoleHierarchy;
|
||||
|
||||
class SecurityDataCollectorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testCollectWhenSecurityIsDisabled()
|
||||
{
|
||||
$collector = new SecurityDataCollector();
|
||||
$collector->collect($this->getRequest(), $this->getResponse());
|
||||
|
||||
$this->assertSame('security', $collector->getName());
|
||||
$this->assertFalse($collector->isEnabled());
|
||||
$this->assertFalse($collector->isAuthenticated());
|
||||
$this->assertNull($collector->getTokenClass());
|
||||
$this->assertFalse($collector->supportsRoleHierarchy());
|
||||
$this->assertCount(0, $collector->getRoles());
|
||||
$this->assertCount(0, $collector->getInheritedRoles());
|
||||
$this->assertEmpty($collector->getUser());
|
||||
}
|
||||
|
||||
/** @dataProvider provideTokenStorage */
|
||||
public function testCollectWhenAuthenticationTokenIsNull($tokenStorage)
|
||||
{
|
||||
$collector = new SecurityDataCollector($tokenStorage, $this->getRoleHierarchy());
|
||||
$collector->collect($this->getRequest(), $this->getResponse());
|
||||
|
||||
$this->assertTrue($collector->isEnabled());
|
||||
$this->assertFalse($collector->isAuthenticated());
|
||||
$this->assertNull($collector->getTokenClass());
|
||||
$this->assertTrue($collector->supportsRoleHierarchy());
|
||||
$this->assertCount(0, $collector->getRoles());
|
||||
$this->assertCount(0, $collector->getInheritedRoles());
|
||||
$this->assertEmpty($collector->getUser());
|
||||
}
|
||||
|
||||
public function provideTokenStorage()
|
||||
{
|
||||
return array(
|
||||
array(new TokenStorage()),
|
||||
array($this->getMock('Symfony\Component\Security\Core\SecurityContextInterface')),
|
||||
);
|
||||
}
|
||||
|
||||
/** @dataProvider provideRoles */
|
||||
public function testCollectAuthenticationTokenAndRoles(array $roles, array $normalizedRoles, array $inheritedRoles)
|
||||
{
|
||||
$tokenStorage = new TokenStorage();
|
||||
$tokenStorage->setToken(new UsernamePasswordToken('hhamon', 'P4$$w0rD', 'provider', $roles));
|
||||
|
||||
$collector = new SecurityDataCollector($tokenStorage, $this->getRoleHierarchy());
|
||||
$collector->collect($this->getRequest(), $this->getResponse());
|
||||
|
||||
$this->assertTrue($collector->isEnabled());
|
||||
$this->assertTrue($collector->isAuthenticated());
|
||||
$this->assertSame('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $collector->getTokenClass());
|
||||
$this->assertTrue($collector->supportsRoleHierarchy());
|
||||
$this->assertSame($normalizedRoles, $collector->getRoles());
|
||||
$this->assertSame($inheritedRoles, $collector->getInheritedRoles());
|
||||
$this->assertSame('hhamon', $collector->getUser());
|
||||
}
|
||||
|
||||
public function provideRoles()
|
||||
{
|
||||
return array(
|
||||
// Basic roles
|
||||
array(
|
||||
array('ROLE_USER'),
|
||||
array('ROLE_USER'),
|
||||
array(),
|
||||
),
|
||||
array(
|
||||
array(new Role('ROLE_USER')),
|
||||
array('ROLE_USER'),
|
||||
array(),
|
||||
),
|
||||
// Inherited roles
|
||||
array(
|
||||
array('ROLE_ADMIN'),
|
||||
array('ROLE_ADMIN'),
|
||||
array('ROLE_USER', 'ROLE_ALLOWED_TO_SWITCH'),
|
||||
),
|
||||
array(
|
||||
array(new Role('ROLE_ADMIN')),
|
||||
array('ROLE_ADMIN'),
|
||||
array('ROLE_USER', 'ROLE_ALLOWED_TO_SWITCH'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
private function getRoleHierarchy()
|
||||
{
|
||||
return new RoleHierarchy(array(
|
||||
'ROLE_ADMIN' => array('ROLE_USER', 'ROLE_ALLOWED_TO_SWITCH'),
|
||||
));
|
||||
}
|
||||
|
||||
private function getRequest()
|
||||
{
|
||||
return $this
|
||||
->getMockBuilder('Symfony\Component\HttpFoundation\Request')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
}
|
||||
private function getResponse()
|
||||
{
|
||||
return $this
|
||||
->getMockBuilder('Symfony\Component\HttpFoundation\Response')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user