How to get userid from eventlistener which are called from AJAX

whitebear picture whitebear · Jun 10, 2013 · Viewed 13.3k times · Source

I am using symfony2 and FOSUserBundle.

Normally,I can get user data from Controller

$user = $this->get('security.context')->getToken()->getUser();

or

$user = $this->container->get('security.context')->getToken()->getUser(); 

But now,I want to get the userdata from eventlistner which are called from Ajax.

Is it possible? or not?

my source is here.

    namespace Acme\MemberBundle\EventListener;

    use ADesigns\CalendarBundle\Event\CalendarEvent;
    use ADesigns\CalendarBundle\Entity\EventEntity;
    use Doctrine\ORM\EntityManager;

    class CalendarEventListener
    {
        private $entityManager;

        public function __construct(EntityManager $entityManager)
        {
            $this->entityManager = $entityManager;
        }

        public function loadEvents(CalendarEvent $calendarEvent)
        {
            $startDate = $calendarEvent->getStartDatetime();
            $endDate = $calendarEvent->getEndDatetime();


           $user = $this->container->get('security.context')->getToken()->getUser();//it doesnt work

            // load events using your custom logic here,
            // for instance, retrieving events from a repository

            $companyEvents = $this->entityManager->getRepository('UserBundle:MutorSche')
                    ->createQueryBuilder('company_events')
                    ->where('company_events.event_datetime BETWEEN :startDate and :endDate')
                    ->setParameter('startDate', $startDate->format('Y-m-d H:i:s'))
                    ->setParameter('endDate', $endDate->format('Y-m-d H:i:s'))
                    ->getQuery()->getResults();



            foreach($companyEvents as $companyEvent) {

                // create an event with a start/end time, or an all day event
                if ($companyEvent->getAllDayEvent() === false) {
                    $eventEntity = new EventEntity($companyEvent->getTitle(), $companyEvent->getStartDatetime(), $companyEvent->getEndDatetime());
                } else {
                    $eventEntity = new EventEntity($companyEvent->getTitle(), $companyEvent->getStartDatetime(), null, true);
                }

                //optional calendar event settings
                $eventEntity->setAllDay(true); // default is false, set to true if this is an all day event
                $eventEntity->setBgColor('#FF0000'); //set the background color of the event's label
                $eventEntity->setFgColor('#FFFFFF'); //set the foreground color of the event's label
                $eventEntity->setUrl('http://www.google.com'); // url to send user to when event label is clicked
                $eventEntity->setCssClass('my-custom-class'); // a custom class you may want to apply to event labels

                //finally, add the event to the CalendarEvent for displaying on the calendar
                $calendarEvent->addEvent($eventEntity);
            }
        }
    }

according to @nifr's advice.

I changed a bit in my source cord like

class CalendarEventListener
{
    private $entityManager;
    private $container;//add

    public function __construct(
            EntityManager $entityManager,
            ContainerInterface $container)//add
    {
        $this->entityManager = $entityManager;
        $this->container = $container;//add
    }

then I want to pass the second argument to eventlistner. I am using services.xml to make listener. How can I add second argument on this?

 <?xml version="1.0" encoding="UTF-8" ?>
      <container xmlns="http://symfony.com/schema/dic/services">

        <services>
            <service id="acme.memberbundle.calendar_listener" class="Acme\MemberBundle\EventListener\CalendarEventListener">
                <argument type="service" id="doctrine.orm.entity_manager" />
                <tag name="kernel.event_listener" event="calendar.load_events" method="loadEvents" />
            </service>

        </services>
      </container>

Answer

Anyone picture Anyone · Sep 24, 2014

As of Symfony 2.6 this issue should be fixed. A pull request has just been accepted into the master. Your problem is described in here. https://github.com/symfony/symfony/pull/11690

As of Symfony 2.6, you can inject the security.token_storage into your listener. This service will contain the token as used by the SecurityContext in <=2.5. In 3.0 this service will replace the SecurityContext::getToken() altogether. You can see a basic change list here: http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements#deprecated-the-security-context-service

Example usage in 2.6:

Your configuration:

services:
    my.listener:
        class: EntityListener
        arguments:
            - "@security.token_storage"
        tags:
            - { name: doctrine.event_listener, event: prePersist }


Your Listener

use Doctrine\ORM\Event\LifecycleEventArgs;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class EntityListener
{
    private $token_storage;

    public function __construct(TokenStorageInterface $token_storage)
    {
        $this->token_storage = $token_storage;
    }

    public function prePersist(LifeCycleEventArgs $args)
    {
        $entity = $args->getEntity();
        $entity->setCreatedBy($this->token_storage->getToken()->getUsername());
    }
}