Talk To Us For FREE

We are in the Business of Support and that starts before you even become a client.
Please prove you are human by selecting the Tree.

How To Create An Authentication Listener In Symfony2

Categories : Article, Open Source Development, PHP Development, Symfony, Web Development

If you want to keep a track of the user’s failed login attempts so that the account can be locked or you want to set the last login date for a user on his or her successful login then Symfony2 will enable several events for authentication. The security.authentication.failure event will be dispatched on failed login and the security.interactive_login event will be dispatched on successful authentication. The Symfony2 will allow the Symfony developers to create an authentication listener class that will subscribe to those events so that the code can be executed when the events are dispatched.

// AuthenticationListener.php
 
namespace path\to\your\class;
 
use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
 
class AuthenticationListener
{
    /**
     * onAuthenticationFailure
     *
     * @author     Joe Sexton 
     * @param     AuthenticationFailureEvent $event
     */
    public function onAuthenticationFailure( AuthenticationFailureEvent $event )
    {
        // executes on failed login
    }
 
    /**
     * onAuthenticationSuccess
     *
     * @author     Joe Sexton 
     * @param     InteractiveLoginEvent $event
     */
    public function onAuthenticationSuccess( InteractiveLoginEvent $event )
    {
        // executes on successful login
    }
}

The class includes two methods. One that subscribes to an authentication failure event and one that subscribes to an interactive login event. The next step consists of subscribing the class methods to the events. This can be achieved in different ways first is in your bundle’s Resources/config/services.yml file like:

# Resources/config/services.yml
    
    # authentication failure event listener
    acme.security.authentication_failure_event_listener:
        class: path\to\your\class\AuthenticationListener
        tags:
            - { name: kernel.event_listener, event: security.authentication.failure, method: onAuthenticationFailure }
 
    # authentication success event listener
    acme.security.interactive_login_listener:
        class: path\to\your\class\AuthenticationListener
        tags:
            - { name: kernel.event_listener, event: security.interactive_login, method: onAuthenticationSuccess }

We have successfully subscribed the two methods to their respective events. The other way you can subscribe to the events is by adding a getSubscribedEvents method to the class such as:

// AuthenticationListener.php
 
namespace path\to\your\class;
 
use Symfony\Component\Security\Core\AuthenticationEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
 
class AuthenticationListener implements EventSubscriberInterface
{
    /**
     * getSubscribedEvents
     *
     * @author     Joe Sexton 
     * @return     array
     */
    public static function getSubscribedEvents()
    {
        return array(
            AuthenticationEvents::AUTHENTICATION_FAILURE => 'onAuthenticationFailure',
            AuthenticationEvents::AUTHENTICATION_SUCCESS => 'onAuthenticationSuccess',
        );
    }
 
    /**
     * onAuthenticationFailure
     *
     * @author     Joe Sexton 
     * @param     AuthenticationFailureEvent $event
     */
    public function onAuthenticationFailure( AuthenticationFailureEvent $event )
    {
        // executes on failed login
    }
 
    /**
     * onAuthenticationSuccess
     *
     * @author     Joe Sexton 
     * @param     InteractiveLoginEvent $event
     */
    public function onAuthenticationSuccess( InteractiveLoginEvent $event )
    {
        // executes on successful login
    }
}

The services file would appear a little different and will look like this:
# Resources/config/services.yml
    
    # authentication failure event listener
    acme.security.authentication_event_listener:
        class: path\to\your\class\AuthenticationListener
        tags:
            - { name: kernel.event_subscriber }

These are two different ways you can subscribe to Symfony’s login events. So, what keeps you waiting? Contact us today for Symfony Web Development…!

[Total: 1    Average: 5/5]