diff --git a/config/packages/security.yaml b/config/packages/security.yaml index c7b078d..3dbc184 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -4,11 +4,8 @@ security: Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto' # https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider providers: - # used to reload user from session & other features (e.g. switch_user) app_user_provider: - entity: - class: App\Entity\Participant - property: email + id: App\Security\UserProvider # used to reload user from session & other features (e.g. switch_user) # used to reload user from session & other features (e.g. switch_user) firewalls: @@ -26,6 +23,10 @@ security: path: app_logout target: app_login # where to redirect after logout + remember_me: + secret: '%kernel.secret%' + lifetime: 604800 # 1 week in seconds + #always_remember_me: true # activate different ways to authenticate # https://symfony.com/doc/current/security.html#the-firewall diff --git a/config/services.yaml b/config/services.yaml index 2d6a76f..196b0d1 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -7,6 +7,9 @@ parameters: services: # default configuration for services in *this* file + App\Security\UserProvider: + arguments: + $entityManager: '@doctrine.orm.entity_manager' _defaults: autowire: true # Automatically injects dependencies in your services. autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. diff --git a/src/Entity/Participant.php b/src/Entity/Participant.php index 9b58bba..33fb490 100644 --- a/src/Entity/Participant.php +++ b/src/Entity/Participant.php @@ -12,6 +12,7 @@ use Symfony\Component\Uid\Uuid; #[ORM\Entity(repositoryClass: ParticipantRepository::class)] #[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_EMAIL', fields: ['email'])] +#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_PSEUDO', fields: ['pseudo'])] class Participant implements UserInterface, PasswordAuthenticatedUserInterface { #[ORM\Id] @@ -26,6 +27,9 @@ class Participant implements UserInterface, PasswordAuthenticatedUserInterface #[ORM\Column(length: 255)] private ?string $prenom = null; + #[ORM\Column(length: 255, nullable: false)] + private ?string $pseudo = null; + #[ORM\Column(length: 255, nullable: true)] private ?string $telephone = null; @@ -76,6 +80,17 @@ class Participant implements UserInterface, PasswordAuthenticatedUserInterface return $this; } + public function getPseudo(): ?string + { + return $this->pseudo; + } + + public function setPseudo(string $pseudo): self + { + $this->pseudo = $pseudo; + return $this; + } + public function getPrenom(): ?string { return $this->prenom; diff --git a/src/Form/RegistrationFormType.php b/src/Form/RegistrationFormType.php index c9ac6c7..40bff5d 100644 --- a/src/Form/RegistrationFormType.php +++ b/src/Form/RegistrationFormType.php @@ -39,6 +39,14 @@ class RegistrationFormType extends AbstractType 'placeholder' => 'Prénom', ], ]) + ->add('pseudo', TextType::class, [ + 'label' => 'Pseudo', + 'label_attr' => ['class' => 'text-gray-700 font-bold'], + 'attr' => [ + 'class' => 'w-full mb-4 px-4 py-2 border-2 border-gray-300 rounded-lg focus:outline-none focus:border-blue-500', + 'placeholder' => 'Pseudo', + ], + ]) ->add('nom', TextType::class, [ 'label' => 'Nom', 'label_attr' => ['class' => 'text-gray-700 font-bold'], diff --git a/src/Security/UserProvider.php b/src/Security/UserProvider.php new file mode 100644 index 0000000..cca49ba --- /dev/null +++ b/src/Security/UserProvider.php @@ -0,0 +1,45 @@ +entityManager = $entityManager; + } + + public function loadUserByIdentifier(string $username): UserInterface + { + // Cherche par email ou pseudo + $user = $this->entityManager->getRepository(Participant::class) + ->findOneBy(['email' => $username]) // Cherche par email + ?? $this->entityManager->getRepository(Participant::class) + ->findOneBy(['pseudo' => $username]); // Ou par pseudo + + if (!$user) { + throw new UsernameNotFoundException('Utilisateur non trouvé'); + } + + return $user; + } + + public function refreshUser(UserInterface $user): UserInterface + { + // Permet de rafraîchir les données d'un utilisateur (pas toujours nécessaire) + return $user; + } + + public function supportsClass(string $class): bool + { + return Participant::class === $class; + } +} diff --git a/templates/auth/login.html.twig b/templates/auth/login.html.twig index efd1fc4..bb57b42 100644 --- a/templates/auth/login.html.twig +++ b/templates/auth/login.html.twig @@ -13,11 +13,14 @@