Added access to token from twig AppVariable
This commit is contained in:
parent
93c0e8adc2
commit
efd3e2dda1
@ -15,6 +15,7 @@ use Symfony\Component\HttpFoundation\Request;
|
|||||||
use Symfony\Component\HttpFoundation\RequestStack;
|
use Symfony\Component\HttpFoundation\RequestStack;
|
||||||
use Symfony\Component\HttpFoundation\Session\Session;
|
use Symfony\Component\HttpFoundation\Session\Session;
|
||||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
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.
|
* Exposes some Symfony parameters and services as an "app" global variable.
|
||||||
@ -48,6 +49,22 @@ class AppVariable
|
|||||||
$this->debug = (bool) $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.
|
* Returns the current user.
|
||||||
*
|
*
|
||||||
@ -57,9 +74,7 @@ class AppVariable
|
|||||||
*/
|
*/
|
||||||
public function getUser()
|
public function getUser()
|
||||||
{
|
{
|
||||||
if (null !== $this->tokenStorage) {
|
if (null === $tokenStorage = $this->tokenStorage) {
|
||||||
$tokenStorage = $this->tokenStorage;
|
|
||||||
} else {
|
|
||||||
throw new \RuntimeException('The "app.user" variable is not available.');
|
throw new \RuntimeException('The "app.user" variable is not available.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
CHANGELOG
|
CHANGELOG
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
3.2.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* added `AppVariable::getToken()`
|
||||||
|
|
||||||
2.7.0
|
2.7.0
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
@ -67,6 +67,17 @@ class AppVariableTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals($request, $this->appVariable->getRequest());
|
$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()
|
public function testGetUser()
|
||||||
{
|
{
|
||||||
$this->setTokenStorage($user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'));
|
$this->setTokenStorage($user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'));
|
||||||
@ -81,6 +92,14 @@ class AppVariableTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertNull($this->appVariable->getUser());
|
$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()
|
public function testGetUserWithNoToken()
|
||||||
{
|
{
|
||||||
$tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
|
$tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
|
||||||
@ -105,6 +124,14 @@ class AppVariableTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->appVariable->getDebug();
|
$this->appVariable->getDebug();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \RuntimeException
|
||||||
|
*/
|
||||||
|
public function testGetTokenWithTokenStorageNotSet()
|
||||||
|
{
|
||||||
|
$this->appVariable->getToken();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \RuntimeException
|
* @expectedException \RuntimeException
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user