src/Domain/User/Symfony/EventSubscriber/Kernel/SwitchUserSubscriber.php line 111

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the MADIS - RGPD Management application.
  4.  *
  5.  * @copyright Copyright (c) 2018-2019 Soluris - Solutions Numériques Territoriales Innovantes
  6.  * @author ANODE <contact@agence-anode.fr>
  7.  *
  8.  * This program is free software: you can redistribute it and/or modify
  9.  * it under the terms of the GNU Affero General Public License as published by
  10.  * the Free Software Foundation, either version 3 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU Affero General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Affero General Public License
  19.  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  20.  */
  21. declare(strict_types=1);
  22. namespace App\Domain\User\Symfony\EventSubscriber\Kernel;
  23. use App\Domain\Reporting\Dictionary\LogJournalActionDictionary;
  24. use App\Domain\Reporting\Dictionary\LogJournalSubjectDictionary;
  25. use App\Domain\Reporting\Model\LogJournal;
  26. use App\Domain\User\Dictionary\UserRoleDictionary;
  27. use App\Domain\User\Model\Collectivity;
  28. use App\Domain\User\Model\User;
  29. use Doctrine\ORM\EntityManagerInterface;
  30. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  31. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  32. use Symfony\Component\Security\Core\Security;
  33. use Symfony\Component\Security\Http\Event\SwitchUserEvent;
  34. use Symfony\Component\Security\Http\SecurityEvents;
  35. class SwitchUserSubscriber implements EventSubscriberInterface
  36. {
  37.     /**
  38.      * @var Security
  39.      */
  40.     private $security;
  41.     /**
  42.      * @var EntityManagerInterface
  43.      */
  44.     private $entityManager;
  45.     /**
  46.      * @var \App\Domain\User\Repository\User
  47.      */
  48.     private $userRepository;
  49.     public function __construct(
  50.         Security $security,
  51.         EntityManagerInterface $em,
  52.         \App\Domain\User\Repository\User $userRepository
  53.     ) {
  54.         $this->security       $security;
  55.         $this->entityManager  $em;
  56.         $this->userRepository $userRepository;
  57.     }
  58.     public static function getSubscribedEvents()
  59.     {
  60.         return [
  61.             SecurityEvents::SWITCH_USER => 'onSwitchUser',
  62.         ];
  63.     }
  64.     private function supports(SwitchUserEvent $event)
  65.     {
  66.         $request             $event->getRequest();
  67.         $switchUserAttribute $request->get('_switch_user');
  68.         if ('_exit' === $switchUserAttribute || $this->security->isGranted('ROLE_ADMIN')) {
  69.             return true;
  70.         }
  71.         /** @var User $switchUser */
  72.         $switchUser $this->userRepository->findOneOrNullByEmail($switchUserAttribute);
  73.         if (\is_null($switchUser)) {
  74.             return false;
  75.         }
  76.         $deniedRoles = [UserRoleDictionary::ROLE_REFERENTUserRoleDictionary::ROLE_ADMIN];
  77.         if (\in_array($switchUser->getRoles()[0], $deniedRoles)) {
  78.             return false;
  79.         }
  80.         /** @var User $connectedUser */
  81.         $connectedUser  $this->security->getUser();
  82.         $collectivities \array_filter(
  83.             \iterable_to_array($connectedUser->getCollectivitesReferees()),
  84.             function (Collectivity $collectivity) use ($switchUser) {
  85.                 return $collectivity === $switchUser->getCollectivity();
  86.             }
  87.         );
  88.         if (empty($collectivities)) {
  89.             return false;
  90.         }
  91.         return true;
  92.     }
  93.     public function onSwitchUser(SwitchUserEvent $event)
  94.     {
  95.         if (!$this->supports($event)) {
  96.             throw new AccessDeniedException();
  97.         }
  98.         $request    $event->getRequest();
  99.         $switchUser $request->get('_switch_user');
  100.         /** @var User $user */
  101.         $user   $this->security->getUser();
  102.         $action LogJournalActionDictionary::SWITCH_USER_ON;
  103.         /** @var User $targetUser */
  104.         $targetUser         $event->getTargetUser();
  105.         $switchUserFullName $targetUser->getFullName();
  106.         if ('_exit' === $switchUser) {
  107.             $action             LogJournalActionDictionary::SWITCH_USER_OFF;
  108.             $switchUserFullName $user->getFullName();
  109.             /** @var User $user */
  110.             $user $event->getTargetUser();
  111.         }
  112.         $log = new LogJournal(
  113.             $user->getCollectivity(),
  114.             $user->getFullName(),
  115.             $user->getEmail(),
  116.             $action,
  117.             LogJournalSubjectDictionary::USER_USER,
  118.             $user->getId()->toString(),
  119.             $switchUserFullName
  120.         );
  121.         $this->entityManager->persist($log);
  122.         $this->entityManager->flush();
  123.     }
  124. }