diff --git a/migrations/Version20250709141934.php b/migrations/Version20250709141934.php new file mode 100644 index 0000000..e710b77 --- /dev/null +++ b/migrations/Version20250709141934.php @@ -0,0 +1,33 @@ +addSql('ALTER TABLE "user" ADD last_connection DATE DEFAULT NULL'); + $this->addSql('COMMENT ON COLUMN "user".last_connection IS \'(DC2Type:date_immutable)\''); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE SCHEMA public'); + $this->addSql('ALTER TABLE "user" DROP last_connection'); + } +} diff --git a/migrations/Version20250710070735.php b/migrations/Version20250710070735.php new file mode 100644 index 0000000..210e9c2 --- /dev/null +++ b/migrations/Version20250710070735.php @@ -0,0 +1,34 @@ +addSql('ALTER TABLE "user" ALTER last_connection TYPE DATE'); + $this->addSql('COMMENT ON COLUMN "user".last_connection IS NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE SCHEMA public'); + $this->addSql('ALTER TABLE "user" ALTER last_connection TYPE DATE'); + $this->addSql('COMMENT ON COLUMN "user".last_connection IS \'(DC2Type:date_immutable)\''); + } +} diff --git a/migrations/Version20250710071344.php b/migrations/Version20250710071344.php new file mode 100644 index 0000000..6530beb --- /dev/null +++ b/migrations/Version20250710071344.php @@ -0,0 +1,32 @@ +addSql('ALTER TABLE "user" ALTER last_connection TYPE TIMESTAMP(0) WITHOUT TIME ZONE'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE SCHEMA public'); + $this->addSql('ALTER TABLE "user" ALTER last_connection TYPE DATE'); + } +} diff --git a/src/Entity/User.php b/src/Entity/User.php index ff06fae..5b742ba 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -5,6 +5,7 @@ namespace App\Entity; use App\Repository\UserRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; +use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; use Symfony\Component\Security\Core\User\UserInterface; @@ -55,6 +56,9 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface #[ORM\Column(options: ['default' => false])] private ?bool $isDeleted = null; + #[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)] + private ?\DateTime $lastConnection = null; + public function __construct() { @@ -227,4 +231,17 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface return (string) $this->getId(); } + public function getLastConnection(): ?\DateTime + { + return $this->lastConnection; + } + + public function setLastConnection(?\DateTime $lastConnection): static + { + $this->lastConnection = $lastConnection; + + return $this; + } + + } diff --git a/src/EventSubscriber/LoginSubscriber.php b/src/EventSubscriber/LoginSubscriber.php new file mode 100644 index 0000000..66ab103 --- /dev/null +++ b/src/EventSubscriber/LoginSubscriber.php @@ -0,0 +1,37 @@ +entityManager = $entityManager; + } + + public static function getSubscribedEvents() + { + return [ + LoginSuccessEvent::class => 'onLoginSuccess', + ]; + } + + public function onLoginSuccess(LoginSuccessEvent $event): void + { + $user = $event->getUser(); + if($user) { + $user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $user->getUserIdentifier()]); + $user->setLastConnection(new \DateTime('now', new \DateTimeZone('Europe/Paris'))); + $this->entityManager->persist($user); + $this->entityManager->flush(); + } + } +}