0
votes

I try to set some data after the login successfull of a user in my Symfony2 project and for that I need to pass the argument Request $request. But I get the follow error:

Catchable Fatal Error: Argument 2 passed to Utilisateurs\UtilisateursBundle\Listener\LoginListener::onSecurityInteractiveLogin() must be an instance of Symfony\Component\HttpFoundation\Request, string given

Here are my codes:

services.yml

services:
    login_listener:
        class: 'Utilisateurs\UtilisateursBundle\Listener\LoginListener'
        arguments: ['@security.context', '@doctrine']
        tags:
            - { name: 'kernel.event_listener', event: 'security.interactive_login' }
        scope: request

LoginListener:

<?php

namespace Utilisateurs\UtilisateursBundle\Listener;

use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Core\SecurityContext;

use Symfony\Component\HttpFoundation\Request as Request;
use Doctrine\Bundle\DoctrineBundle\Registry as Doctrine; // for Symfony 2.1.0+

/**
 * Custom login listener.
 */
class LoginListener
{
    /** @var \Symfony\Component\Security\Core\SecurityContext */
    private $securityContext;

    /** @var \Doctrine\ORM\EntityManager */
    private $em;



    /**
     * Constructor
     * 
     * @param SecurityContext $securityContext
     * @param Doctrine        $doctrine
     */
    public function __construct(SecurityContext $securityContext, Doctrine $doctrine)
    {
        $this->securityContext = $securityContext;
        $this->em              = $doctrine->getEntityManager();
    }

    /**
     * Do the magic.
     * 
     * @param InteractiveLoginEvent $event
     */
    public function onSecurityInteractiveLogin(InteractiveLoginEvent $event, Request $request)
    {
        if ($this->securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
            // user has just logged in
        }

        if ($this->securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
            // user has logged in using remember_me cookie
        }

                    $session = $request->getSession();
                    $content = $session->get('result');
                    $idQuery = $session->get('query_id');
                    $authorId = $this->getUser()->getId();

                    $biblio = new Biblio;
                    $biblio->setAuthorId($authorId);
                    $biblio->setContent($content);
                    $biblio->setQueryId($idQuery);
                    $biblio->setDate(new \DateTime());
                    $em = $this->getDoctrine()->getManager();
                    $em->persist($biblio);
                    $em->flush();

                    $session->clear();

                    $response = $this->forward('BiblishareBundle:Profile:show');

                    return $response;

        // do some other magic here
        $user = $event->getAuthenticationToken()->getUser();

        // ...
    }
}

I'm using the FOSUserBundle. Thank you for your help

1
It works now, just one more question: how can I redirect via a Url into the listener? thanksnico_lrx

1 Answers

0
votes

Try $request = $event->getRequest();

I'm not sure that you get the Request in the method onSecurityInteractiveLogin

Anyway http://api.symfony.com/2.0/Symfony/Component/Security/Http/Event/InteractiveLoginEvent.html