Skip to content

[FrameworkBundle] Added GlobalVariables::getToken() #20773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

3.3.0
-----

* Added `GlobalVariables::getToken()`

3.2.0
-----

Expand Down
21 changes: 14 additions & 7 deletions src/Symfony/Bundle/FrameworkBundle/Templating/GlobalVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you removed the return type, you could IMO remove the whole docblock.

*
* @see TokenInterface::getUser()
*/
public function getUser()
{
if (!$token = $this->getToken()) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down