8

I am trying to inject the currently logged in user into a Listener. My goal is to write a current \DateTime() to the 'last_active' column of my 'demo_user' table every time the user does any action (both of this this methods do not work for me) .

app/config/config.yml

# ...
services:
    demo.listener:
        class: Demo\UserBundle\EventListener\ActivityWatcher.php
        arguments: ['@service_container']
        tags:
            - { name: doctrine.event_listener, event: postLoad }

src/Demo/UserBundle/EventListener/ActivityWatcher.php

namespace Demo\UserBundle\EventListener;

use Doctrine\ORM\Event\LifecycleEventArgs;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Demo\UserBundle\Entity\DemoUser;

class ActivityWatcher
{

    protected $container;

    public function __construct(ContainerInterface $container)
    {   
        $this->container = $container;
    }


    public function postLoad(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();
        $entityManager = $args->getEntityManager();

        if ($entity instanceof DemoUser) {
            $token = $this->container->get('security.context')->getToken();
            $user = $token->getUser();

            if($entity->getId() == $user->getId()) {
                $entity->setLastActivity( new \DateTime() );
                $entityManager->persist($entity);
                $entityManager->flush();
            }
        }
    }
}

But the $token is always empty... UPDATE: Didn't mention, that I'm logged in and authenticated when this happens

FatalErrorException: Error: Call to a member function getUser()
on a non-object ...

Any ideas? Arrgh, thanks, Jan

UPDATE:

I also tried only to inject the security.context:

arguments: ["@security.context"]

but got a

ServiceCircularReferenceException: Circular reference detected for "doctrine.orm.default_entity_manager", path: "doctrine.orm.default_entity_manager -> doctrine.dbal.default_connection -> wwk.listener -> security.context -> security.authentication.manager -> security.user.provider.concrete.administrators".
Community
  • 1
  • 1
jxp315
  • 108
  • 1
  • 6
  • Not an ellegant solution but: http://stackoverflow.com/questions/7561013/injecting-securitycontext-services-into-a-listener-class-in-symfony2-causes-circ – Jovan Perovic Jul 26 '13 at 21:17
  • I think I'm exactly trying to do this at the moment, but without any success – jxp315 Jul 26 '13 at 21:19
  • The link provided by @JovanPerovic is fine. The correct answer is this one: http://stackoverflow.com/questions/7561013/injecting-securitycontext-into-a-listener-prepersist-or-preupdate-in-symfony2-to#26011863 – webDEVILopers Feb 20 '15 at 12:18

2 Answers2

2

for symfony 2.4 you should be able to inject expression, so instead of @service_container use @=service('security.context').getToken().getUser() to get user directly

zizoujab
  • 7,603
  • 8
  • 41
  • 72
kshishkin
  • 525
  • 5
  • 7
  • 2
    Hi, I tryed this but I have the same problem : *Circular reference detected for service "doctrine.orm.default_entity_manager"*. It looks like calling `@security.context` while using a `doctrine.event_listener̀` still raise the error. – MARTIN Damien Jan 29 '14 at 09:16
  • this worked perfectly for me. Thank you Symfony genie – Colin Rickels Oct 11 '17 at 18:58
0

The solution to the circular reference problem when injecting the current user is implementing a user callable.

You can use the UserCallable class provided by KnpDoctrineBehaviors.

At first you need to create a service from the user callable class and inject the container into it.

You can find a YML example on how to do this in the config provided by KnpDoctrineBehaviors.

Now inject your UserCallable service into your listener and retrieve the user from the user-callable as you can see in the source of Blameable listener.

Nicolai Fröhlich
  • 51,330
  • 11
  • 126
  • 130