nbgrp / onelogin-saml-bundle

OneLogin SAML Symfony Bundle
BSD 3-Clause "New" or "Revised" License
43 stars 13 forks source link

Events not firing (UserCreatedEvent, UserUpdatedEvent) #28

Closed slejnej closed 1 year ago

slejnej commented 1 year ago

Tried with Listener and Subscriber, but despite DeferredUserListener is calling dispatch nothing is kicking in.

# service.yaml
    App\EventListener\SamlUserCreatedListener:
        tags:
            - { name: kernel.event_listener, event: 'Nbgrp\OneloginSamlBundle\Event\UserCreatedEvent', method: onSamlUserCreated }

Subscriber:

class CustomEventSubscriber implements EventSubscriberInterface
{

  public function __construct(private LoggerInterface $logger)
  {
  }

  public static function getSubscribedEvents(): array
  {
    // return the subscribed events, their methods and priorities
    return [
      UserCreatedEvent::class => 'onCreated',
    ];
  }

  public function onCreated(UserCreatedEvent $event)
  {
    $this->logger->info($event->getUser()->getUserIdentifier());
  }
}

Not called listeners:

Nbgrp\OneloginSamlBundle\Event\UserCreatedEvent
--
0 | "App\EventListener\SamlUserCreatedListener::onSamlUserCreated(UserCreatedEvent $event)"
-1000 | "App\EventSubscriber\CustomEventSubscriber::onCreated(UserCreatedEvent $event)"
a-menshchikov commented 1 year ago

@slejnej hi! Thank you for your question.

It's important to remember that authentication events are security events. So you need to specify correct value for a dispatcher property of the listener configuration. For example, if you use this bundle in main firewall, you should specify security.event_dispatcher.main as a dispatcher for your listener.

slejnej commented 1 year ago

@a-menshchikov Thank you for your response.

Maybe some updates to the README with an example how to use that because tagging for the event and method is then not sufficient to create a listener, but it also needs dispatcher: security.event_dispatcher.main (or firewall used in) defined when wiring.

Subscriber to Nbgrp\OneloginSamlBundle\Event\UserCreatedEvent is never visible for this event, but same subscriber for CheckPassportEvent::class is listed.

The code:

use Nbgrp\OneloginSamlBundle\Event\UserCreatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\Event\CheckPassportEvent;

...

public static function getSubscribedEvents()
  {
    // return the subscribed events, their methods and priorities
    return [
      UserCreatedEvent::class => 'onUserCreated',
      CheckPassportEvent::class => ['onUserCreated', -1],
    ];
  }

debug output:

"Nbgrp\OneloginSamlBundle\Event\UserCreatedEvent" event
-------------------------------------------------------

 ------- ----------------------------------------------------------------------------- ---------- 
  Order   Callable                                                                      Priority  
 ------- ----------------------------------------------------------------------------- ---------- 
  #1      Nbgrp\OneloginSamlBundle\EventListener\User\UserCreatedListener::__invoke()   0         
 ------- ----------------------------------------------------------------------------- ---------- 

"Nbgrp\OneloginSamlBundle\Event\UserModifiedEvent" event
--------------------------------------------------------

"Symfony\Component\Security\Http\Event\CheckPassportEvent" event
----------------------------------------------------------------

 ------- ------------------------------------------------------------------------------------------- ---------- 
  Order   Callable                                                                                    Priority  
 ------- ------------------------------------------------------------------------------------------- ---------- 
  #1      Symfony\Component\Security\Http\EventListener\UserProviderListener::checkPassport()         2048      
  #2      Symfony\Component\Security\Http\EventListener\UserProviderListener::checkPassport()         1024      
  #3      Symfony\Component\Security\Http\EventListener\CsrfProtectionListener::checkPassport()       512       
  #4      Symfony\Component\Security\Http\EventListener\UserCheckerListener::preCheckCredentials()    256       
  #5      Nbgrp\OneloginSamlBundle\EventListener\User\DeferredUserListener::dispatchDeferredEvent()   0         
  #6      Symfony\Component\Security\Http\EventListener\CheckCredentialsListener::checkPassport()     0         
  #7      App\EventSubscriber\SecurityEventSubscriber::onUserCreated()                                -1        
 ------- ------------------------------------------------------------------------------------------- ---------- 
a-menshchikov commented 1 year ago

I don't know the way to specify a certain dispatcher when use a subscriber. But I have no idea why you need to use a subsciber in a Symfony 6 application while you can use the #[AsEventListener(dispatcher: 'security.event_dispatcher.main')] attribute. IMO this is the same approach as we've had earlier with event subscribers.