<?phpnamespace Oz\NvlPortalDisplayer\EventSubscriber;use DateTime;use Doctrine\ORM\EntityManagerInterface;use League\OAuth2\Client\Token\AccessToken;use Oz\ApiNvl\Model\User;use Oz\NvlPortalDisplayer\Entity\PortalUser;use Oz\NvlPortalDisplayer\Service\Helper\UserHelper;use Oz\NvlPortalDisplayer\Service\User\UserTransformator;use Oz\NvlPortalDisplayer\Model\UserRessourceOwner;use Oz\Toolboxe\Tools\JwtHelper;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\Security\Core\User\UserInterface;use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;/** * */class UpdateDisplayerUserSubscriber implements EventSubscriberInterface{ /** * @var EntityManagerInterface */ private $em; /** * @var UserHelper */ private $userHelper; /** * @param EntityManagerInterface $em * @param UserHelper $userHelper */ public function __construct(EntityManagerInterface $em, UserHelper $userHelper) { $this->em = $em; $this->userHelper = $userHelper; } /** * @return string[] */ public static function getSubscribedEvents(): array { return [ 'security.interactive_login' => ['onSecurityInteractiveLogin', 20], ]; } /** * @param InteractiveLoginEvent $event */ public function onSecurityInteractiveLogin(InteractiveLoginEvent $event): void { /** @var UserRessourceOwner $user */ $user = $event->getAuthenticationToken()->getUser(); if (method_exists($user, 'isLocalAdmin') && $user->isLocalAdmin() === true) { return; } if (!$user instanceof UserInterface) { return; }// if (!$user instanceof User) {// return;// } $this->updateUser($user); $this->userHelper->checkUserStructure($user); $this->em->flush(); } /** * Creation de l'user en base si il n'existe pas et update last login * * @param UserRessourceOwner $user */ private function updateUser(UserInterface $user): void { $now = new DateTime('NOW'); if (!$baseUser = $this->em->getRepository(PortalUser::class)->findOneBy(['userId' => $user->getId()])) { // creation de l'user $baseUser = (UserTransformator::getEntityUserFromUserRessourceOwner($user)) ->setCreationDate($now); if ( $user->getToken() instanceof AccessToken && method_exists($user, 'isSsoUser') && $user->isSsoUser() === true ) { $jwtDecoded = JwtHelper::decode($user->getToken()); $baseUser->setUserId($jwtDecoded['Id']); } } $baseUser->setLastLogin($now); $this->em->persist($baseUser); }}