vendor/sulu/sulu/src/Sulu/Bundle/SecurityBundle/EventListener/LastLoginListener.php line 51

  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\SecurityBundle\EventListener;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Sulu\Component\Security\Authentication\UserInterface as SuluUserInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  16. use Symfony\Component\Security\Http\SecurityEvents;
  17. /**
  18.  * Listener to set the last login field.
  19.  */
  20. class LastLoginListener implements EventSubscriberInterface
  21. {
  22.     public function __construct(protected EntityManagerInterface $entityManager)
  23.     {
  24.     }
  25.     /**
  26.      * Subscribe the login events.
  27.      *
  28.      * @return array
  29.      */
  30.     public static function getSubscribedEvents()
  31.     {
  32.         return [
  33.             SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
  34.         ];
  35.     }
  36.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
  37.     {
  38.         $user $event->getAuthenticationToken()->getUser();
  39.         $this->updateLastLogin($user);
  40.     }
  41.     /**
  42.      * Update the users last login.
  43.      *
  44.      * @param UserInterface $user
  45.      */
  46.     protected function updateLastLogin($user)
  47.     {
  48.         if ($user instanceof SuluUserInterface) {
  49.             $user->setLastLogin(new \DateTime());
  50.             $this->entityManager->flush();
  51.         }
  52.     }
  53. }