0

Is it possible to get the user in a RouteProviderInterface implementation?

My RouteProviderInterface implementation loads a number of new Routes, nothing special. But I want to customize the Routes based on a user setting, if a user is logged in.

If I inject the TokenStorage, the user is not loaded and null:

public function __construct(TokenStorage $tokenStorage) {

    $this->user = $tokenStorage->getToken()->getUser(); // is null

}

Is there another way to get the user?

Some edit based on comments:

  • I am trying this with a authenticated user. I also dump the user in the actual controller being used and the user does exists there
  • All firewalls have "stateless: true" in the config
BigJ
  • 1,990
  • 2
  • 29
  • 47
  • Try getting the user from the getRouteCollections method. Might be available by then. Just guess though. – Cerad Dec 13 '16 at 14:18
  • 1
    tokenStorage is the right way. Ensure that the token is beeing saved in session. Authentification needs to be stateless. See "configuration" here http://symfony.com/doc/current/security/api_key_authentication.html – Rawburner Dec 13 '16 at 14:22
  • I am agree with @AlexanderKeil , the best way is to injecting the security.tocken_storage service. If the actual user isn't authenticated then it will return null so try to dump an authenticated user in construct method – Mohamed Ben HEnda Dec 13 '16 at 14:51
  • Thnx for your responses. I am trying this with an authenticated user, and the "stateless: true" is set on all firewalls. – BigJ Dec 13 '16 at 14:59
  • 1
    i think this is not possible. routing happens before security, because security and the firewalls are configured based on the route and thus come later. see also this old discussion: https://github.com/symfony/symfony/issues/3121 – dbu Dec 13 '16 at 15:07
  • Thnx @dbu, this should be the answer. – BigJ Dec 13 '16 at 17:30

0 Answers0