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

Open in your IDE?
  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.     /**
  23.      * @var EntityManagerInterface
  24.      */
  25.     protected $entityManager;
  26.     /**
  27.      * LastLoginListener constructor.
  28.      */
  29.     public function __construct(EntityManagerInterface $entityManager)
  30.     {
  31.         $this->entityManager $entityManager;
  32.     }
  33.     /**
  34.      * Subscribe the login events.
  35.      *
  36.      * @return array
  37.      */
  38.     public static function getSubscribedEvents()
  39.     {
  40.         return [
  41.             SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
  42.         ];
  43.     }
  44.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
  45.     {
  46.         $user $event->getAuthenticationToken()->getUser();
  47.         $this->updateLastLogin($user);
  48.     }
  49.     /**
  50.      * Update the users last login.
  51.      *
  52.      * @param UserInterface $user
  53.      */
  54.     protected function updateLastLogin($user)
  55.     {
  56.         if ($user instanceof SuluUserInterface) {
  57.             $user->setLastLogin(new \DateTime());
  58.             $this->entityManager->flush();
  59.         }
  60.     }
  61. }