Method call after user login

I was wondering if it is possible to call the function after user login.

Here is the code I want to call:

$point = $this->container->get('process_points'); $point->ProcessPoints(1 , $this->container); 
+6
source share
1 answer

You can find the events that FOSUserBundle fires in the FOSUserEvents class . In particular, this is the one you are looking for:

 /** * The SECURITY_IMPLICIT_LOGIN event occurs when the user is logged in programmatically. * * This event allows you to access the response which will be sent. * The event listener method receives a FOS\UserBundle\Event\UserEvent instance. */ const SECURITY_IMPLICIT_LOGIN = 'fos_user.security.implicit_login'; 

The documentation for connecting to these events can be found on the Join the controller pages page. "In your case, you need to implement something like this:

 namespace Acme\UserBundle\EventListener; use FOS\UserBundle\FOSUserEvents; use FOS\UserBundle\Event\FormEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\Security\Http\SecurityEvents; use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; /** * Listener responsible to change the redirection at the end of the password resetting */ class LoginListener implements EventSubscriberInterface { private $container; public function __construct($container) { $this->container = $container; } /** * {@inheritDoc} */ public static function getSubscribedEvents() { return array( FOSUserEvents::SECURITY_IMPLICIT_LOGIN => 'onLogin', SecurityEvents::INTERACTIVE_LOGIN => 'onLogin', ); } public function onLogin($event) { // FYI // if ($event instanceof UserEvent) { // $user = $event->getUser(); // } // if ($event instanceof InteractiveLoginEvent) { // $user = $event->getAuthenticationToken()->getUser(); // } $point = $this->container->get('process_points'); $point->ProcessPoints(1 , $this->container); } } 

Then you must identify the listener as a service and enter the container. Alternatively, you can enter only the service that you need, and not the entire container.

 services: acme_user.login: class: Acme\UserBundle\EventListener\LoginListener arguments: [@container] tags: - { name: kernel.event_subscriber } 

There is also another method that involves overriding the controller , but, as indicated in the documentation, you need to duplicate their code so that it is not cleaned up exactly and is tied to a break if (or, rather, when) the FOSUserBundle changes.

+26
source

Source: https://habr.com/ru/post/947060/


All Articles