Symfony2: After successful login event, perform set of actions
Asked Answered
V

2

29

I need to perform a set of actions after a user successfully logs in. This includes loading data from the database and storing it in the session.

What is the best approach to implementing this?

Valerievalerio answered 24/6, 2012 at 19:0 Comment(1)
How to do it using a Service: #8308550Bred
C
54

You can add a listener to the security.interactive_login event.

attach your listener like so. In this example I also pass the security context and session as dependencies.

Note: SecurityContext is deprecated as of Symfony 2.6. Please refer to http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements

parameters:
   # ...

   account.security_listener.class: Company\AccountBundle\Listener\SecurityListener

services:
   # ...

   account.security_listener:
        class: %account.security_listener.class%
        arguments: ['@security.context', '@session']
        tags:
            - { name: kernel.event_listener, event: security.interactive_login, method: onSecurityInteractiveLogin }

and in your listener you can store whatever you want on the session. In this case I set the users timezone.

<?php

namespace Company\AccountBundle\Listener;

use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;

class SecurityListener
{

   public function __construct(SecurityContextInterface $security, Session $session)
   {
      $this->security = $security;
      $this->session = $session;
   }

   public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
   {
        $timezone = $this->security->getToken()->getUser()->getTimezone();
        if (empty($timezone)) {
            $timezone = 'UTC';
        }
        $this->session->set('timezone', $timezone);
   }

}
Cachou answered 24/6, 2012 at 19:25 Comment(8)
This looks like it would do the trick. Are there any dependencies I need to declare in the class?Valerievalerio
After digging a bit more, seems like there are dependencies that need to be specified. Found this article that shows a more complete code implementation: metod.si/login-event-listener-in-symfony2Valerievalerio
There aren't any more dependencies... the only difference with that example is the scope of the listener.Cachou
You don't need to inject SecurityContext nor Session, you can grab it from InteractiveLoginEvent, using $event->getAuthenticationToken() and $event->getRequest()->getSession().Poppy
Most of the people using YML not XML. And it's a hell of a headache to convert it!Usurp
Plus, you didn't provided namesapces to use. Plus, the scope 'request' will throw an error every time.Usurp
is it also suitable for the user impersonalization ?Ashe
Does this work even on different session handlers ? What if the project store the sessions in Redis ?Schiffman
Q
8

You can even fetch the user instance from the event itself, no need to inject the token storage!

public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
    $event->getAuthenticationToken()->getUser()
}
Quartic answered 17/6, 2016 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.