Skip to content

Commit

Permalink
feature #19991 [TwigBridge] Added access to token from twig AppVariab…
Browse files Browse the repository at this point in the history
…le (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
  • Loading branch information
fabpot committed Oct 5, 2016
2 parents 043ccd5 + efd3e2d commit cbda41d
Show file tree
Hide file tree
Showing 3 changed files with 46 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
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
3.2.0
-----

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

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 cbda41d

Please sign in to comment.