I'm writing a template in twig
for my symfony4 project. I want to access the user object of the impersonator user. NOT the user that is being impersonated.
I found a solution, but both PhpStorm and the Symfony toolbar tell me that my code is deprecated:
{% if is_granted("ROLE_PREVIOUS_ADMIN") %}
{% for role in app.token.roles %}
{% if role.role == "ROLE_PREVIOUS_ADMIN" %}
{{ role.source.user.username }}
{% endif %}
{% endfor %}
{% endif %}
Though my code does work as (not) expected, the following two error messages appear in the web debug toolbar:
10:16:10 php User Deprecated: The Symfony\Component\Security\Core\Authentication\Token\AbstractToken::getRoles() method is deprecated since Symfony 4.3. Use the getRoleNames() method instead.
and
10:16:10 php User Deprecated: The "Symfony\Component\Security\Core\Role\SwitchUserRole" class is deprecated since version 4.3 and will be removed in 5.0. Use strings as roles instead.
{% for role in app.token.getRoleNames() %}
,twig
will auto-translateapp.token.roles
toapp.token.getRoles()
– DarkBee