Skip to content

[TwigBridge] Make AppVariable check if security.context exists #14816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2015
Merged

[TwigBridge] Make AppVariable check if security.context exists #14816

merged 1 commit into from
Jun 11, 2015

Conversation

ogizanagi
Copy link
Contributor

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:

security: false
{% 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.

If security isn't configured in the application, the `security.context` service, nor the `security.token_storage` service exists.
Therefore, if a third-party bundle relies on the app.user check in Twig templates, an exception was thrown about asking for an non-existing service.
Instead, this change check if the `security.context` actually exists before trying to use it, and return null otherwise.
@@ -89,6 +91,8 @@ public function getUser()
if (null === $this->tokenStorage) {
if (null === $this->container) {
throw new \RuntimeException('The "app.user" variable is not available.');
} elseif (!$this->container->has('security.context')) {
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an exception saying it is not available (as done in the previous case) may make more sense

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I know, but it will make the check in templates unusable if third-party bundles relies on it to activate templates sections when the security is configured.

GlobalVariables class, previously used for twig app globals in previous versions did this very same check and returned null when the service didn't exist.
So, this is basically a BC break. :/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then we should indeed return null

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I'm indeed wondering why we should throw a RuntimeException in the above case where the container isn't defined. When does this particular case might happen ? Does it make sense to throw this exception instead of returning null?

Obviously, if this PR is merged as it is, we'll have some kind of inconsistency. It feels odd, and the same might be applicable to request and session globals...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about deprecating that behaviour with Symfony 2.8 and throw the exception starting with 3.0?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know. It was an easy way to enable or hide sections in templates, depending on the application configuration without much effort. Apps will require to check themselves if security is configured and inject the result in every template where they need it, or create their own global.
When does returning null in such case became wrong ? I can't find anything mentioning the reasons in #13354

@fabpot
Copy link
Member

fabpot commented Jun 11, 2015

👍

@fabpot
Copy link
Member

fabpot commented Jun 11, 2015

Thank you @ogizanagi.

@fabpot fabpot merged commit ea71174 into symfony:2.7 Jun 11, 2015
fabpot added a commit that referenced this pull request Jun 11, 2015
…ists (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
@ogizanagi ogizanagi deleted the twig/app_globals_security branch June 11, 2015 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants