Fosuserbundle: Allow to disable LastLoginListener

Created on 20 May 2019  路  5Comments  路  Source: FriendsOfSymfony/FOSUserBundle

Why?

user.last_login gets updated every api request.

How?
Add a new parameter to allow disabling the listener

Most helpful comment

You can change fos_user.security.interactive_login_listener.class class to your own implementation:

parameters:
    fos_user.security.interactive_login_listener.class: ApiBundle\EventListener\LastLoginListener

And then implement own logic (like "do nothing"):

<?php

namespace ApiBundle\EventListener;

use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use FOS\UserBundle\EventListener\LastLoginListener as BaseLastLoginListener;

class LastLoginListener extends BaseLastLoginListener
{
    /**
     * @param InteractiveLoginEvent $event
     */
    public function onSecurityInteractiveLogin(InteractiveLoginEvent $event) {}
}

All 5 comments

agreed -- it's an expensive query (because it's an update, not simply a read like getting the user). Especially in API calls.

One way to disable it is to override the method in User.php

    // hack to disable lastLogin update 
    public function setLastLogin(\DateTime $time = null) {}

You can try to create a compiler pass which removes the service / event from the container
https://symfony.com/doc/current/service_container/compiler_passes.html

You can change fos_user.security.interactive_login_listener.class class to your own implementation:

parameters:
    fos_user.security.interactive_login_listener.class: ApiBundle\EventListener\LastLoginListener

And then implement own logic (like "do nothing"):

<?php

namespace ApiBundle\EventListener;

use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use FOS\UserBundle\EventListener\LastLoginListener as BaseLastLoginListener;

class LastLoginListener extends BaseLastLoginListener
{
    /**
     * @param InteractiveLoginEvent $event
     */
    public function onSecurityInteractiveLogin(InteractiveLoginEvent $event) {}
}

@asamek thanks. works like a charm!

Was this page helpful?
0 / 5 - 0 ratings