获取模仿者用户名但获取代码弃用通知
Getting the impersonator username but getting code deprecated notice
我正在 twig
中为我的 symfony4 项目编写模板。我想访问模拟用户的用户对象。 不是 被模拟的用户。
我找到了解决方案,但是 PhpStorm 和 Symfony 工具栏都告诉我我的代码已被弃用:
{% 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 %}
虽然我的代码确实按预期(不是)工作,但以下两条错误消息出现在网络调试工具栏中:
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.
和
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.
我自己找到了解决办法!我查看了 https://symfony.com/doc/current/security/impersonating_user.html#finding-the-original-user 代码并将函数应用于 twig 中的 app.token 变量。
忘掉for循环吧!只需使用:
{{ app.token.getOriginalToken().getUser() }}
或简称:
{{ app.token.originalToken.user }}
我正在 twig
中为我的 symfony4 项目编写模板。我想访问模拟用户的用户对象。 不是 被模拟的用户。
我找到了解决方案,但是 PhpStorm 和 Symfony 工具栏都告诉我我的代码已被弃用:
{% 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 %}
虽然我的代码确实按预期(不是)工作,但以下两条错误消息出现在网络调试工具栏中:
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.
和
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.
我自己找到了解决办法!我查看了 https://symfony.com/doc/current/security/impersonating_user.html#finding-the-original-user 代码并将函数应用于 twig 中的 app.token 变量。
忘掉for循环吧!只需使用:
{{ app.token.getOriginalToken().getUser() }}
或简称:
{{ app.token.originalToken.user }}