8000 minor #13214 [FrameworkBundle] Use security.token_storage service in … · symfony/symfony@be0c312 · GitHub
[go: up one dir, main page]

Skip to content

Commit be0c312

Browse files
committed
minor #13214 [FrameworkBundle] Use security.token_storage service in Controller::getUser() (xelaris)
This PR was squashed before being merged into the 2.6 branch (closes #13214). Discussion ---------- [FrameworkBundle] Use security.token_storage service in Controller::getUser() | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Replace deprecated `security.context` with `security.token_storage` service. Commits ------- f46ce9c [FrameworkBundle] Use security.token_storage service in Controller::getUser()
2 parents eabc9b8 + f46ce9c commit be0c312

File tree

2 files changed

+98
-4
lines changed

2 files changed

+98
-4
lines changed

src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ public function getDoctrine()
293293
}
294294

295295
/**
296-
* Get a user from the Security Context.
296+
* Get a user from the Security Token Storage.
297297
*
298298
* @return mixed
299299
*
@@ -303,15 +303,16 @@ public function getDoctrine()
303303
*/
304304
public function getUser()
305305
{
306-
if (!$this->container->has('security.context')) {
306+
if (!$this->container->has('security.token_storage')) {
307307
throw new \LogicException('The SecurityBundle is not registered in your application.');
308308
}
309309

310-
if (null === $token = $this->container->get('security.context')->getToken()) {
310+
if (null === $token = $this->container->get('security.token_storage')->getToken()) {
311311
return;
312312
}
313313

314314
if (!is_object($user = $token->getUser())) {
315+
// e.g. anonymous authentication
315316
return;
316317
}
317318

src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@
1313

1414
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1515
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
16+
use Symfony\Component\DependencyInjection\ContainerInterface;
1617
use Symfony\Component\HttpFoundation\Request;
1718
use Symfony\Component\HttpFoundation\RequestStack;
1819
use Symfony\Component\HttpFoundation\Response;
20+
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
21+
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
22+
use Symfony\Component\Security\Core\User\User;
1923

2024
class ControllerTest extends TestCase
2125
{
@@ -37,10 +41,99 @@ public function testForward()
3741
$container->expects($this->at(0))->method('get')->will($this->returnValue($requestStack));
3842
$container->expects($this->at(1))->method('get')->will($this->returnValue($kernel));
3943

40-
$controller = new Controller();
44+
$controller = new TestController();
4145
$controller->setContainer($container);
4246

4347
$response = $controller->forward('a_controller');
4448
$this->assertEquals('xml--fr', $response->getContent());
4549
}
50+
51+
public function testGetUser()
52+
{
53+
$user = new User('user', 'pass');
54+
$token = new UsernamePasswordToken($user, 'pass', 'default', array('ROLE_USER'));
55+
56+
$controller = new TestController();
57+
$controller->setContainer($this->getContainerWithTokenStorage($token));
58+
59+
$this->assertSame($controller->getUser(), $user);
60+
}
61+
62+
public function testGetUserAnonymousUserConvertedToNull()
63+
{
64+
$token = new AnonymousToken('default', 'anon.');
65+
66+
$controller = new TestController();
67+
$controller->setContainer($this->getContainerWithTokenStorage($token));
68+
69+
$this->assertNull($controller->getUser());
70+
}
71+
72+
public function testGetUserWithEmptyTokenStorage()
73+
{
74+
$controller = new TestController();
75+
$controller->setContainer($this->getContainerWithTokenStorage(null));
76+
77+
$this->assertNull($controller->getUser());
78+
}
79+
80+
/**
81+
* @expectedException \LogicException
82+
* @expectedExceptionMessage The SecurityBundle is not registered in your application.
83+
*/
84+
public function testGetUserWithEmptyContainer()
85+
{
86+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
87+
$container
88+
->expects($this->once())
89+
->method('has')
90+
->with('security.token_storage')
91+
->will($this->returnValue(false));
92+
93+
$controller = new TestController();
94+
$controller->setContainer($container);
95+
96+
$controller->getUser();
97+
}
98+
99+
/**
100+
* @param $token
101+
* @return ContainerInterface
102+
*/
103+
private function getContainerWithTokenStorage($token = null)
104+
{
105+
$tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage');
106+
$tokenStorage
107+
->expects($this->once())
108+
->method('getToken')
109+
->will($this->returnValue($token));
110+
111+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
112+
$container
113+
->expects($this->once())
114+
->method('has')
115+
->with('security.token_storage')
116+
->will($this->returnValue(true));
117+
118+
$container
119+
->expects($this->once())
120+
->method('get')
121+
->with('security.token_storage')
122+
->will($this->returnValue($tokenStorage));
123+
124+
return $container;
125+
}
126+
}
127+
128+
class TestController extends Controller
129+
{
130+
public function forward($controller, array $path = array(), array $query = array())
131+
{
132+
return parent::forward($controller, $path, $query);
133+
}
134+
135+
public function getUser()
136+
{
137+
return parent::getUser();
138+
}
46139
}

0 commit comments

Comments
 (0)
0