Add new twig bridge function to generate impersonation path #50030
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Before this PR
So we already have impersonation features in Symfony (https://symfony.com/doc/current/security/impersonating_user.html) and we have two twig helper functions
impersonation_exit_url
andimpersonation_exit_path
which both work with the configuration parameter for the switch user.If the developer changes the switch parameter (
_switch_user
), then these helper functions will dynamically update the_switch_user=_exit
type urls/paths.However, to switch TO a user, hand crafted urls with
?_switch_user=MYIDENTIFIER
likehttp://example.com/somewhere?_switch_user=thomas
need to be hand crafted currently.The problem
if we now go and change
_switch_user
to be something else, like_want_to_be_this_user
in the Symfony configuration (Because the boss told us to do that), then all our exit path/urls will dynamically update, but our hard coded ?_switch_user=MYIDENTIFIER` will stop working.The solution this PR provides
The solution this PR provides is to provide a new Twig Helper function for the impersonation path only, taking into account the configured value in Symfony config of the parameter (default is still
_switch_user
but can be anything like_want_to_be_this_user
as per the docs)This new twig function can be used as such:
This would output
?_want_to_be_this_user=mike
or if the default parameter still used would be?_switch_user=mike
The PR repurposes the existing code to generate the paths and is backward compatible.