Skip to content

[TwigBridge] Added access to token from twig AppVariable #19991

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
Oct 5, 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
21 changes: 18 additions & 3 deletions src/Symfony/Bridge/Twig/AppVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

/**
* Exposes some Symfony parameters and services as an "app" global variable.
Expand Down Expand Up @@ -48,6 +49,22 @@ public function setDebug($debug)
$this->debug = (bool) $debug;
}

/**
* Returns the current token.
*
* @return TokenInterface|null
*
* @throws \RuntimeException When the TokenStorage is not available
*/
public function getToken()
{
Copy link
Member

Choose a reason for hiding this comment

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

not sure this change is needed, and it forces the error message to be less specific

Copy link
Contributor

Choose a reason for hiding this comment

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

It's moved to the getToken() method.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I see what you mean now. Indeed the message will not be specific to the user anymore (will be about token). I'd indeed keep the check and throw an exception like before.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fair enough

if (null === $tokenStorage = $this->tokenStorage) {
throw new \RuntimeException('The "app.token" variable is not available.');
}

return $tokenStorage->getToken();
}

/**
* Returns the current user.
*
Expand All @@ -57,9 +74,7 @@ public function setDebug($debug)
*/
public function getUser()
{
if (null !== $this->tokenStorage) {
$tokenStorage = $this->tokenStorage;
} else {
if (null === $tokenStorage = $this->tokenStorage) {
throw new \RuntimeException('The "app.user" variable is not available.');
}

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

3.2.0
-----

* added `AppVariable::getToken()`

2.7.0
-----

Expand Down
27 changes: 27 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/AppVariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ public function testGetRequest()
$this->assertEquals($request, $this->appVariable->getRequest());
}

public function testGetToken()
{
$tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
$this->appVariable->setTokenStorage($tokenStorage);

$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
$tokenStorage->method('getToken')->willReturn($token);

$this->assertEquals($token, $this->appVariable->getToken());
}

public function testGetUser()
{
$this->setTokenStorage($user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'));
Expand All @@ -81,6 +92,14 @@ public function testGetUserWithUsernameAsTokenUser()
$this->assertNull($this->appVariable->getUser());
}

public function testGetTokenWithNoToken()
{
$tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
$this->appVariable->setTokenStorage($tokenStorage);

$this->assertNull($this->appVariable->getToken());
}

public function testGetUserWithNoToken()
{
$tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
Expand All @@ -105,6 +124,14 @@ public function testDebugNotSet()
$this->appVariable->getDebug();
}

/**
* @expectedException \RuntimeException
*/
public function testGetTokenWithTokenStorageNotSet()
{
$this->appVariable->getToken();
}

/**
* @expectedException \RuntimeException
*/
Expand Down