Skip to content
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

DEV toolbar crashing when trying to get user from token storage in kernel.request listener #18056

Closed
clytemnestra opened this issue Mar 8, 2016 · 3 comments

Comments

@clytemnestra
Copy link

Using SF 2.8.3 and having the following class and its configuration:

class ThemeRequestListener
{

    private $sam;

    public function __construct(TokenStorage $sam)
    {

        $this->sam = $sam;
    }

    /**
     * @param GetResponseEvent $event
     */
    public function onKernelRequest(GetResponseEvent $event)
    {
        $a = $this->sam->getToken()->getUser();
    }

}
    liip_theme.theme_request_listener:
        class: Application\Liip\ThemeBundle\EventListener\ThemeRequestListener
        arguments: [ @security.token_storage ]
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: -1 }

My DEV environment always crashes with the toolbar asking me to be opened in a dialog. However, PROD works just fine, so the problem is with the Symfony toolbar

Error: Call to a member function getUser() on null

I've tried with a fresh new 2.8.3 project with no vendor bundle aside the standard ones, just this listener and getting the same error.

@jvasseur
Copy link
Contributor

jvasseur commented Mar 8, 2016

In a standard symfony installation, the wdt is behind a disabled firewall (https://github.com/symfony/symfony-standard/blob/master/app/config/security.yml#L14) so the security token is always null.

You shouldn't blindly call getUser on the token without checking that it isn't null.

@jakzal
Copy link
Contributor

jakzal commented Mar 8, 2016

Closing as you should indeed check if a token is returned. TokenStorageInterface::getToken() might return null. It doesn't happen on production as you most likely don't have any production routes with no authentication (toolbar should be disabled). Feel free to provide more information if you still think this is a bug and we'll re-open.

@jakzal jakzal closed this as completed Mar 8, 2016
@clytemnestra
Copy link
Author

Thanks for the help. Checking if null worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants