1

I have Googled and done what seemed obvious - but it's not working...I'm running Symfony 5.1.7

How to get the current logged User in a service

UPDATED:

use Symfony\Component\Security\Core\Security;

class SomeSubscriber implements EventSubscriber
{

    private $user = null;
    private $activity = null;

    public function __construct(Security $security)
    {
        dump($security->getUser());
        //$this->user = $tokenStorage->getUser();
    }
}

services.yaml:

App\EventListener\SomeSubscriber:
    tags:
        -
            name: 'doctrine.event_subscriber'
            priority: -1500
Alex.Barylski
  • 2,843
  • 4
  • 45
  • 68
  • It's a timing issue. Your subscriber is being created before the user processing is done. Just store $tokenStorage (or better yet $security) as an instance variable then do your $security->getUser() when you actually need the user. As long as your subscriber method is called after the user listener does it's thing then it should all work. – Cerad Oct 23 '20 at 19:23
  • I figured so...but I'm not sure how you mean store $security? Can I not change the priority of my subscriber to occur after authorization? – Alex.Barylski Oct 23 '20 at 19:26
  • $security refers to injecting Security $security as the partially correct answer below shows in place of TokenStorage. Using Security saves a tiny bit of code and is the recommended method for getting the user. And yes, use priority if necessary to insure your listener is fired after Symfony's user listener. – Cerad Oct 23 '20 at 19:31
  • I've tried Security and I've changed the services.yaml priority...still no dice (see original message for updated code). I've cleared cache, notta! Any ideas? – Alex.Barylski Oct 23 '20 at 19:36
  • Are you using the DoctrineBundle EventSubscriber or the Symfony EventDispatcher subscriber? – Cerad Oct 23 '20 at 19:43
  • Oh jeeze...I think??? (Doctrine\Common\EventSubscriber) – Alex.Barylski Oct 23 '20 at 19:44
  • This is what I followed: https://symfony.com/doc/current/doctrine/events.html#doctrine-lifecycle-subscribers – Alex.Barylski Oct 23 '20 at 19:45
  • 1
    The priority only impacts Kernel event listeners. Doctrine has it's own event dispatcher and listener. Apparently some Doctrine events are being triggered before the user is available. If you always need a user then just add code to ignore any events fired with the user being available. Or rethink your approach. Doctrine events can be very touchy. – Cerad Oct 23 '20 at 19:47

1 Answers1

2

You can inject the Security Component inside any service :

use Symfony\Component\Security\Core\Security;

class SomeSubscriber implements EventSubscriber
{
    private $security = null;
    private $user = null;
    
    // ...

    public function __construct(Security $security)
    {
        $this->security = $security;
        //$this->user = $this->security->getUser();
    }

}
agrandy
  • 31
  • 4
  • Wondering if it's because they are doctrine events I am listening to? – Alex.Barylski Oct 23 '20 at 19:24
  • To check that, can you get the user inside an other service, a controller for instance ? – agrandy Oct 23 '20 at 19:27
  • Yes I can get the user inside controllers no problems...I'm pretty sure its an timing issue as stated above, just not sure how to work around it, other than change priority of subscriber??? – Alex.Barylski Oct 23 '20 at 19:29