Symfony2 - Inject the currently logged in user into a listener

隐身守侯 提交于 2020-01-01 08:58:07

问题


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".

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/17890433/symfony2-inject-the-currently-logged-in-user-into-a-listener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!