src/EventSubscriber/UserSystem/LoginSuccessSubscriber.php line 69

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
  4.  *
  5.  * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU Affero General Public License as published
  9.  * by the Free Software Foundation, either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU Affero General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Affero General Public License
  18.  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  19.  */
  20. declare(strict_types=1);
  21. /**
  22.  * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
  23.  *
  24.  * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
  25.  *
  26.  * This program is free software; you can redistribute it and/or
  27.  * modify it under the terms of the GNU General Public License
  28.  * as published by the Free Software Foundation; either version 2
  29.  * of the License, or (at your option) any later version.
  30.  *
  31.  * This program is distributed in the hope that it will be useful,
  32.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  34.  * GNU General Public License for more details.
  35.  *
  36.  * You should have received a copy of the GNU General Public License
  37.  * along with this program; if not, write to the Free Software
  38.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  39.  */
  40. namespace App\EventSubscriber\UserSystem;
  41. use App\Entity\LogSystem\UserLoginLogEntry;
  42. use App\Entity\UserSystem\User;
  43. use App\Services\LogSystem\EventLogger;
  44. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  45. use Symfony\Component\HttpFoundation\Session\Session;
  46. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  47. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  48. use Symfony\Component\Security\Http\SecurityEvents;
  49. use Symfony\Contracts\Translation\TranslatorInterface;
  50. /**
  51.  * This event listener shows an login successful flash to the user after login and write the login to event log.
  52.  */
  53. final class LoginSuccessSubscriber implements EventSubscriberInterface
  54. {
  55.     private $translator;
  56.     private $flashBag;
  57.     private $eventLogger;
  58.     private $gpdr_compliance;
  59.     public function __construct(TranslatorInterface $translatorSessionInterface $sessionEventLogger $eventLoggerbool $gpdr_compliance)
  60.     {
  61.         /** @var Session $session */
  62.         $this->translator $translator;
  63.         $this->flashBag $session->getFlashBag();
  64.         $this->eventLogger $eventLogger;
  65.         $this->gpdr_compliance $gpdr_compliance;
  66.     }
  67.     public function onLogin(InteractiveLoginEvent $event): void
  68.     {
  69.         $ip $event->getRequest()->getClientIp();
  70.         $log = new UserLoginLogEntry($ip$this->gpdr_compliance);
  71.         $user $event->getAuthenticationToken()->getUser();
  72.         if ($user instanceof User) {
  73.             $log->setTargetElement($user);
  74.         }
  75.         $this->eventLogger->logAndFlush($log);
  76.         $this->flashBag->add('notice'$this->translator->trans('flash.login_successful'));
  77.     }
  78.     /**
  79.      * Returns an array of event names this subscriber wants to listen to.
  80.      *
  81.      * The array keys are event names and the value can be:
  82.      *
  83.      *  * The method name to call (priority defaults to 0)
  84.      *  * An array composed of the method name to call and the priority
  85.      *  * An array of arrays composed of the method names to call and respective
  86.      *    priorities, or 0 if unset
  87.      *
  88.      * For instance:
  89.      *
  90.      *  * ['eventName' => 'methodName']
  91.      *  * ['eventName' => ['methodName', $priority]]
  92.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  93.      *
  94.      * @return array The event names to listen to
  95.      */
  96.     public static function getSubscribedEvents(): array
  97.     {
  98.         return [SecurityEvents::INTERACTIVE_LOGIN => 'onLogin'];
  99.     }
  100. }