Skip to content

Commit

Permalink
Added access to token from twig AppVariable
Browse files Browse the repository at this point in the history
  • Loading branch information
HeahDude committed Sep 22, 2016
1 parent 93c0e8a commit efd3e2d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/Symfony/Bridge/Twig/AppVariable.php
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()
{
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
@@ -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
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

0 comments on commit efd3e2d

Please sign in to comment.