From 099b8481f785f70a33ed05f3841c02b150c8b2ea Mon Sep 17 00:00:00 2001 From: Jules Pietri Date: Mon, 5 Dec 2016 23:50:48 +0100 Subject: [PATCH] Added GlobalVariables::getToken() --- .../Bundle/FrameworkBundle/CHANGELOG.md | 5 ++++ .../Templating/GlobalVariables.php | 21 ++++++++++----- .../Tests/Templating/GlobalVariablesTest.php | 26 +++++++++++++++++++ 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index efee66b41cecf..b9e88311ece9e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +3.3.0 +----- + + * Added `GlobalVariables::getToken()` + 3.2.0 ----- diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/GlobalVariables.php b/src/Symfony/Bundle/FrameworkBundle/Templating/GlobalVariables.php index 606e886415c95..660a9375cf332 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/GlobalVariables.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/GlobalVariables.php @@ -14,6 +14,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; /** * GlobalVariables is the entry point for Symfony global variables in PHP templates. @@ -33,21 +34,27 @@ public function __construct(ContainerInterface $container) } /** - * Returns the current user. - * - * @return mixed + * Returns the current token. * - * @see TokenInterface::getUser() + * @return TokenInterface|null */ - public function getUser() + public function getToken() { if (!$this->container->has('security.token_storage')) { return; } - $tokenStorage = $this->container->get('security.token_storage'); + return $this->container->get('security.token_storage')->getToken(); + } - if (!$token = $tokenStorage->getToken()) { + /** + * Returns the current user. + * + * @see TokenInterface::getUser() + */ + public function getUser() + { + if (!$token = $this->getToken()) { return; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/GlobalVariablesTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/GlobalVariablesTest.php index 5c00c62e7ff9d..d5ff1b309ddae 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/GlobalVariablesTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/GlobalVariablesTest.php @@ -26,6 +26,32 @@ protected function setUp() $this->globals = new GlobalVariables($this->container); } + public function testGetTokenNoTokenStorage() + { + $this->assertNull($this->globals->getToken()); + } + + public function testGetTokenNoToken() + { + $tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'); + $this->container->set('security.token_storage', $tokenStorage); + $this->assertNull($this->globals->getToken()); + } + + public function testGetToken() + { + $tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'); + + $this->container->set('security.token_storage', $tokenStorage); + + $tokenStorage + ->expects($this->once()) + ->method('getToken') + ->will($this->returnValue('token')); + + $this->assertSame('token', $this->globals->getToken()); + } + public function testGetUserNoTokenStorage() { $this->assertNull($this->globals->getUser());