Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
symfony4:authentification [2020/04/03 15:27] marclebrun [Templates du contrôleur] |
— (Version actuelle) | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | ====== Authentification (NEW) ====== | ||
- | |||
- | Source : **Lior Chamla** [[https://www.youtube.com/watch?v=_GjHWa9hQic|SYMFONY 4/4 : 1H POUR COMPRENDRE L'AUTHENTIFICATION !]] | ||
- | |||
- | ===== Configuration de la sécurité ===== | ||
- | |||
- | Éditer le fichier **config/packages/security.yaml** | ||
- | |||
- | * Configurer un **encoder** pour l'entité **App\Entity\User** | ||
- | * Configurer un **provider** | ||
- | * Configurer un ou plusieurs **firewalls** | ||
- | |||
- | <code yaml> | ||
- | security: | ||
- | encoders: | ||
- | App\Entity\User: | ||
- | algorithm: bcrypt | ||
- | providers: | ||
- | in_database: | ||
- | entity: | ||
- | class: App\Entity\User | ||
- | property: email | ||
- | firewalls: | ||
- | dev: | ||
- | pattern: ^/(_(profiler|wdt)|css|images\js)/ | ||
- | security: false | ||
- | main: | ||
- | anonymous: true | ||
- | provider: in_database | ||
- | form_login: | ||
- | login_path: login | ||
- | check_path: login | ||
- | </code> | ||
- | |||
- | ===== Création de l'entité USER ===== | ||
- | |||
- | <code bash> | ||
- | php bin/console make:entity User | ||
- | </code> | ||
- | |||
- | Fichiers créés : | ||
- | |||
- | | src/Entity/**User**.php | | ||
- | | src/Repository/**UserRepository**.php | | ||
- | |||
- | Ajout des propriétés : | ||
- | |||
- | ^ Nom ^ Type ^ Longueur ^ Nullable ^ | ||
- | | email | string | 255 | Non | | ||
- | | username | string | 255 | Non | | ||
- | | password | string | 255 | Non | | ||
- | |||
- | Créer la migration : | ||
- | |||
- | <code bash> | ||
- | php bin/console make:migration | ||
- | </code> | ||
- | |||
- | Appliquer la migration : | ||
- | |||
- | <code bash> | ||
- | php bin/console doctrine:migrations:migrate | ||
- | </code> | ||
- | |||
- | ==== Ajouter une propriété "confirm_password" à l'entité ==== | ||
- | |||
- | On a besoin d'une propriété ''confirm_password'' utilisée dans le | ||
- | formulaire d'inscription, mais celle-ci ne doit pas être liée à un | ||
- | champ de la base de données. | ||
- | On l'ajoute manuellement, ainsi que les fonctions getter et setter. | ||
- | |||
- | <code php User.php> | ||
- | Class User | ||
- | { | ||
- | // ... | ||
- | | ||
- | private $confirm_password; | ||
- | | ||
- | // ... | ||
- | | ||
- | public function getConfirmPassword(): ?string | ||
- | { | ||
- | return $this->confirm_password; | ||
- | } | ||
- | |||
- | public function setConfirmPassword(string $confirm_password): self | ||
- | { | ||
- | $this->confirm_password = $confirm_password; | ||
- | |||
- | return $this; | ||
- | } | ||
- | |||
- | } | ||
- | </code> | ||
- | |||
- | |||
- | ==== Contraintes et UserInterface ==== | ||
- | |||
- | * Contrainte d'unicité de l'entité | ||
- | * Contraintes de validation des champs | ||
- | * Implémenter l'interface **UserInterface** | ||
- | |||
- | <code php User.php> | ||
- | use Doctrine\ORM\Mapping as ORM; | ||
- | use Symfony\Component\Validator\Constraints as Assert; | ||
- | use Symfony\Component\Security\Core\User\UserInterface; | ||
- | use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; | ||
- | |||
- | /** | ||
- | * @ORM\Entity(repositoryClass="App\Repository\UserRepository") | ||
- | * @UniqueEntity( | ||
- | * fields = {"email"}, | ||
- | * message = "L'email indiqué est déjà utilisé" | ||
- | * ) | ||
- | */ | ||
- | Class User implements UserInterface | ||
- | { | ||
- | /** | ||
- | * @ORM\Id() | ||
- | * @ORM\GeneratedValue() | ||
- | * @ORM\Column(type="integer") | ||
- | */ | ||
- | private $id; | ||
- | |||
- | /** | ||
- | * @ORM\Column(type="string", length=255) | ||
- | * @Assert\Email() | ||
- | */ | ||
- | private $email; | ||
- | |||
- | /** | ||
- | * @ORM\Column(type="string", length=255) | ||
- | */ | ||
- | private $username; | ||
- | |||
- | /** | ||
- | * @ORM\Column(type="string", length=255) | ||
- | * @Assert\Length( | ||
- | * min="8", | ||
- | * minMessage="Votre mot de passe doit comporter au minimum 8 caractères !" | ||
- | * ) | ||
- | */ | ||
- | private $password; | ||
- | | ||
- | /** | ||
- | * @Assert\Equalto( | ||
- | * propertyPath="password", | ||
- | * message="Vous n'avez pas tapé deux fois le même mot de passe !" | ||
- | * ) | ||
- | */ | ||
- | private $confirm_password; | ||
- | |||
- | // Getters et setters pour les champs | ||
- | //*********************************** | ||
- | // ... | ||
- | | ||
- | // Implémentation des fonctions de UserInterface | ||
- | //********************************************** | ||
- | | ||
- | public function eraseCredentials() { | ||
- | } | ||
- | | ||
- | public function getSalt() { | ||
- | } | ||
- | | ||
- | public function getRoles() { | ||
- | return ['ROLE_USER']; | ||
- | } | ||
- | } | ||
- | </code> | ||
- | |||
- | ===== Formulaire d'inscription ===== | ||
- | |||
- | <code bash> | ||
- | php bin/console make:form RegistrationType | ||
- | </code> | ||
- | |||
- | Fichiers créés : | ||
- | |||
- | | src/Form/**RegistrationType**.php | | ||
- | |||
- | Répondre aux questions : | ||
- | |||
- | | Nom de l'entité | **User** | | ||
- | |||
- | Les champs de l'entité **User** sont déjà ajoutés dans la classe du formulaire. | ||
- | |||
- | <code php RegistrationType.php> | ||
- | use Symfony\Component\Form\Extension\Core\Type\PasswordType; | ||
- | |||
- | class RegistrationType extends AbstractType | ||
- | { | ||
- | public function buildForm(FormBuilderInterface $builder, array $options) | ||
- | { | ||
- | $builder | ||
- | ->add('email') | ||
- | ->add('username') | ||
- | ->add('password', PasswordType::class) | ||
- | ->add('confirm_password', PasswordType::class) | ||
- | ; | ||
- | } | ||
- | } | ||
- | </code> | ||
- | |||
- | ===== Contrôleur ===== | ||
- | |||
- | <code bash> | ||
- | php bin/console make:controller | ||
- | </code> | ||
- | |||
- | <code> | ||
- | Choose a name for your controller class: | ||
- | > SecurityController | ||
- | </code> | ||
- | |||
- | Fichiers créés : | ||
- | |||
- | | src/Controller/**SecurityController**.php | | ||
- | | templates/security/index.html.twig | | ||
- | |||
- | <code php SecurityController.php> | ||
- | use App\Entity\User; | ||
- | use App\Form\RegistrationType; | ||
- | use Docrine\ORM\EntityManagerInterface; | ||
- | use Symfony\Component\HttpFoundation\Request; | ||
- | use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; | ||
- | |||
- | class SecurityController extends Controller | ||
- | { | ||
- | /** | ||
- | * @Route("/register", name="register") | ||
- | */ | ||
- | public function register( | ||
- | Request $request, | ||
- | EntityManagerInterface $manager, | ||
- | UserPasswordEncoderInterface $encoder | ||
- | ) { | ||
- | $user = new User(); | ||
- | | ||
- | $form = $this->createForm(RegistrationType::class, $user); | ||
- | $form->handleRequest($request); | ||
- | | ||
- | if($form->isSubmitted() && $form->isValid()) { | ||
- | | ||
- | $hash = $encoder->encodePassword($user, $user->getPassword()); | ||
- | $user->setPassword($hash); | ||
- | | ||
- | $manager->persist($user); | ||
- | $manager->flush(); | ||
- | | ||
- | return $this->redirectToRoute('login'); | ||
- | } | ||
- | | ||
- | return $this->render("security/register.html.twig", [ | ||
- | 'form' => $form->createView() | ||
- | ]); | ||
- | } | ||
- | | ||
- | /** | ||
- | * @Route("/connexion", name="login") | ||
- | */ | ||
- | public function login() { | ||
- | return $this->render("security/login.html.twig"); | ||
- | } | ||
- | | ||
- | } | ||
- | </code> | ||
- | |||
- | ==== Templates du contrôleur ==== | ||
- | |||
- | === Register === | ||
- | |||
- | <code twig register.html.twig> | ||
- | {% extends 'base.html.twig' %} | ||
- | |||
- | {% block title %}Inscription{% endblock %} | ||
- | |||
- | {% block body %} | ||
- | <h1>Inscription</h1> | ||
- | |||
- | {{ form_start(form) }} | ||
- | |||
- | {{ form_row(form.username, { | ||
- | 'label': "Nom d'utilisateur", | ||
- | 'attr': { | ||
- | 'placeholder': "Nom d'utilisateur" | ||
- | } | ||
- | }) }} | ||
- | |||
- | {{ form_row(form.email, { | ||
- | 'label': "Adresse email", | ||
- | 'attr': { | ||
- | 'placeholder': "Adresse email" | ||
- | } | ||
- | }) }} | ||
- | |||
- | {{ form_row(form.password, { | ||
- | 'label': "Mot de passe", | ||
- | 'attr': { | ||
- | 'placeholder': "Mot de passe" | ||
- | } | ||
- | }) }} | ||
- | |||
- | {{ form_row(form.confirm_password, { | ||
- | 'label': "Confirmation du mot de passe", | ||
- | 'attr': { | ||
- | 'placeholder': "Confirmation du mot de passe" | ||
- | } | ||
- | }) }} | ||
- | |||
- | <button | ||
- | type="submit" | ||
- | class="btn btn-success">Inscription</button> | ||
- | | ||
- | {{ form_end(form) }} | ||
- | {% endblock %} | ||
- | </code> | ||
- | |||
- | === Login === | ||
- | |||
- | <code twig login.html.twig> | ||
- | {% extends 'base.html.twig' %} | ||
- | |||
- | {% block body %} | ||
- | <h1>Connexion</h1> | ||
- | | ||
- | <form action="{{ path(login) }}" method="post"> | ||
- | <div class="form-group"> | ||
- | <input | ||
- | type="text" | ||
- | name="_username" | ||
- | class="form-control" | ||
- | placeholder="Adresse email" | ||
- | required> | ||
- | </div> | ||
- | <div class="form-group"> | ||
- | <input | ||
- | type="password" | ||
- | name="_password" | ||
- | class="form-control" | ||
- | placeholder="Mot de passe" | ||
- | required> | ||
- | </div> | ||
- | | ||
- | <div class="form-group"> | ||
- | <button type="submit" class="btn btn-success">Connexion</button> | ||
- | </div> | ||
- | </form> | ||
- | | ||
- | {% endblock %} | ||
- | </code> | ||