1

I'm trying to count failed and successful logins for my users. For that, I simply want to increase the respective counter in the datebase whenever an authentication attempt succeeds or fails. But I want to keep the default behavior without reinventing it.

So I followed this post: Symfony2 hold behavior when extending DefaultAuthenticationSuccessHandler

But apparently I cannot add any parameters to the constructor of my subclass of DefaultAuthenticationSuccessHandler or Symfony complains that the argument types are wrong.

How can I inject my user management service as a constructor parameter??

EDIT: Actually, the problem seems to be a little bit different! I have the following line in my services.yml:

services:
 security.authentication.success_handler:
  class: %security.authentication.success_handler.class%
  arguments: [@my_stuff.my_user_management_service, @security.http_utils, {}]

But the second argument passed to the constructor is an array containing the options like "login_path". But it's supposed to be an instance of HttpUtils. I'm confused...

Community
  • 1
  • 1
user2323470
  • 183
  • 9

2 Answers2

0

I figured it out by myself: the order of the parameters is important. I had to move my_stuff.my_user_management_service to the end of the parameter array like so:

arguments: [@security.http_utils, {}, @my_stuff.my_user_management_service]

I don't really understand why, though. There is something wrong with the parameter injection. Maybe someone has some insight??

user2323470
  • 183
  • 9
0

As far I can see, there is much simpler NOT to override DefaultAuthenticationSuccessHandler but to use Event Listener:

class Auth2FaEmailListener extends AbstractObjectEventSubscriber
{
    use EnabledTrait, TargetPathTrait;

    public static function getSubscribedEvents(): array
    {
        return [
            TwoFactorAuthenticationEvents::SUCCESS => 'onSuccess',
        ];
    }


    public function onSuccess(TwoFactorAuthenticationEvent $event)
    {
        if (!$this->isEnabled()) {
            return;
        }
        $request = $event->getRequest();
        
        // do what you need
    }
}

Remember to register Listener service:

// in SomeBundle/Resources/config/services.yml 
SomeBundle\EventSubscriber\:
        resource: '../../EventSubscriber'
        tags:
            - { name: kernel.event_subscriber }

Feel free to see the list of available events:

Eugene Kaurov
  • 2,356
  • 28
  • 39