bug #14816 [TwigBridge] Make AppVariable check if security.context exists (ogizanagi)
This PR was merged into the 2.7 branch.
Discussion
----------
[TwigBridge] Make AppVariable check if security.context exists
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | -
If security isn't configured in the application, neither the `security.context` service, nor the `security.token_storage` service exists.
Therefore, if a third-party bundle relies on the `app.user` or `app.security` (deprecated) check in Twig templates, an exception was thrown about asking for an non-existing service:
```yml
security: false
```
```twig
{% if app.user %}
<div>
...
</div>
{% endif %}
```
```
You have requested a non-existent service "security.context"
```
Instead, this patch checks if the `security.context` actually exists before trying to use it, and returns null otherwise.
Note that the **GlobalVariables** class, used for twig app globals in previous versions, and still used for PHP templating, behaves the same way.
So this is basically a BC break.
Commits
-------
ea71174
[TwigBridge] Make AppVariable check if security.context exists
This commit is contained in:
commit
da02fceb26
@ -74,8 +74,10 @@ class AppVariable
|
|||||||
throw new \RuntimeException('The "app.security" variable is not available.');
|
throw new \RuntimeException('The "app.security" variable is not available.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($this->container->has('security.context')) {
|
||||||
return $this->container->get('security.context');
|
return $this->container->get('security.context');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current user.
|
* Returns the current user.
|
||||||
@ -89,6 +91,8 @@ class AppVariable
|
|||||||
if (null === $this->tokenStorage) {
|
if (null === $this->tokenStorage) {
|
||||||
if (null === $this->container) {
|
if (null === $this->container) {
|
||||||
throw new \RuntimeException('The "app.user" variable is not available.');
|
throw new \RuntimeException('The "app.user" variable is not available.');
|
||||||
|
} elseif (!$this->container->has('security.context')) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->tokenStorage = $this->container->get('security.context');
|
$this->tokenStorage = $this->container->get('security.context');
|
||||||
|
Reference in New Issue
Block a user