8000 feature #19991 [TwigBridge] Added access to token from twig AppVariab… · symfony/symfony@cbda41d · GitHub
[go: up one dir, main page]

Skip to content

Commit cbda41d

Browse files
committed
feature #19991 [TwigBridge] Added access to token from twig AppVariable (HeahDude)
This PR was merged into the 3.2-dev branch. Discussion ---------- [TwigBridge] Added access to token from twig AppVariable | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | ~ | License | MIT | Doc PR | TODO In Symfony 2.x we could access the token from `app.security` but now we can only get the user even if it comes from the token storage. This makes mandatory to create a custom twig extension to access it and thus harder to update to symfony 3.x when you need this simple getter in a template where custom tokens are involved (e.g using a ConnectToken from SensioLabs Connect API). I hope this little feature will be part of 3.2 :) Commits ------- efd3e2d Added access to token from twig AppVariable
2 parents 043ccd5 + efd3e2d commit cbda41d

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

src/Symfony/Bridge/Twig/AppVariable.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\HttpFoundation\RequestStack;
1616
use Symfony\Component\HttpFoundation\Session\Session;
1717
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
18+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1819

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

52+
/**
53+
* Returns the current token.
54+
*
55+
* @return TokenInterface|null
56+
*
57+
* @throws \RuntimeException When the TokenStorage is not available
58+
*/
59+
public function getToken()
60+
{
61+
if (null === $tokenStorage = $this->tokenStorage) {
62+
throw new \RuntimeException('The "app.token" variable is not available.');
63+
}
64+
65+
return $tokenStorage->getToken();
66+
}
67+
5168
/**
5269
* Returns the current user.
5370
*
@@ -57,9 +74,7 @@ public function setDebug($debug)
5774
*/
5875
public function getUser()
5976
{
60-
if (null !== $this->tokenStorage) {
61-
$tokenStorage = $this->tokenStorage;
62-
} else {
77+
if (null === $tokenStorage = $this->tokenStorage) {
6378
throw new \RuntimeException('The "app.user" variable is not available.');
6479
}
6580

src/Symfony/Bridge/Twig/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
3.2.0
55
-----
66

7+
* added `AppVariable::getToken()`
78
* Deprecated the possibility to inject the Form Twig Renderer into the form
89
extension. Inject it on TwigRendererEngine instead.
910

src/Symfony/Bridge/Twig/Tests/AppVariableTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ public function testGetRequest()
6767
$this->assertEquals($request, $this->appVariable->getRequest());
6868
}
6969

70+
public function testGetToken()
71+
{
72+
$tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
73+
$this->appVariable->setTokenStorage($tokenStorage);
74+
75+
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
76+
$tokenStorage->method('getToken')->willReturn($token);
77+
78+
$this->assertEquals($token, $this->appVariable->getToken());
79+
}
80+
7081
public function testGetUser()
7182
{
7283
$this->setTokenStorage($user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'));
@@ -81,6 +92,14 @@ public function testGetUserWithUsernameAsTokenUser()
8192
$this->assertNull($this->appVariable->getUser());
8293
}
8394

95+
public function testGetTokenWithNoToken()
96+
{
97+
$tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
98+
$this->appVariable->setTokenStorage($tokenStorage);
99+
100+
$this->assertNull($this->appVariable->getToken());
101+
}
102+
84103
public function testGetUserWithNoToken()
85104
{
86105
$tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
@@ -105,6 +124,14 @@ public function testDebugNotSet()
105124
$this->appVariable->getDebug();
106125
}
107126

127+
/**
128+
* @expectedException \RuntimeException
129+
*/
130+
public function testGetTokenWithTokenStorageNotSet()
131+
{
132+
$this->appVariable->getToken();
133+
}
134+
108135
/**
109136
* @expectedException \RuntimeException
110137
*/

0 commit comments

Comments
 (0)
0