Professional Documents
Culture Documents
# https://symfony.com/doc/current/security/impersonating_user.html
# switch_user: true
src/controller/SecurityController.php
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
namespace App\Entity;
use Assert\Email;
use Assert\NotBlank;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\component\Uid\Uuid;
use
Symfony\Component\Security\Core\Utilisateur\PasswordAuthenticatedUtilisateurInter
face;
use App\Repository\UtilisateurRepository;
use App\Entity\UtilisateurInterface;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity('email', 'Cet email existe déjà au sein de l\'application.')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue('CUSTOM')]
#[ORM\Column(type: 'uuid', unique: true)]
#[ORM\CustomIdGenerator('doctrine.uuid_generator')]
private ?string $id = null;
/**
* @var string The hashed password
*/
#[ORM\Column]
private ?string $password = null;
#[ORM\Column(type: 'boolean')]
private $isVerified = false;
#[ORM\Column(type: 'datetime_immutable')]
#[Assert\NotNull()]
private \DateTimeImmutable $createdAt;
#[ORM\Column(type: 'datetime_immutable')]
#[Assert\NotNull()]
private \DateTimeImmutable $updatedAt;
#[ORM\PrePersist]
public function prePersist(): void
{
$this->avatar = 'https://api.dicebear.com/7.x/lorelei-neutral/svg' . $this->email
. '.svg';
}
#[ORM\PreUpdate]
public function preUpdate(): void
{
$this->avatar = 'https://api.dicebear.com/7.x/lorelei-neutral/svg' . $this->email
. '.svg';
$this->updatedAt = new \DateTimeImmutable();
}
return $this;
}
return $this;
}
return $this;
}
// public function getUserAdminName(): ?string
// {
// return $this->userAdminName;
// }
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->email;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string) $this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
return $this;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt(): ?string
{
return null;
}
/**
* @see UserInterface
*/
public function eraseCredentials(): void
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
return $this;
}
/**
* @return Collection<int, Video>
*/
public function getVideos(): Collection
{
return $this->videos;
}
return $this;
}
return $this;
}
/**
* @return Collection<int, Marker>
*/
public function getMarkers(): Collection
{
return $this->markers;
}
return $this;
}
/**
* @return Collection<int, ArticlesBlog>
*/
public function getArticlesBlogs(): Collection
{
return $this->articlesBlogs;
}
return $this;
}
return $this;
}
return $this;
}
}
registrationFormType a modifier
<?php
namespace App\Form;
use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\TextType; // N'oublie pas
d'ajouter cette ligne
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
les templates
template/security/
login.html.twig
{% extends 'base.html.twig' %}
{% block stylesheets %}
<link rel="stylesheet" href="{{ asset('css/styles.css') }}">
{% endblock %}
admin_login.html.twig
{% extends 'base.html.twig' %}
{% block stylesheets %}
<link rel="stylesheet" href="{{ asset('css/admin-styles.css') }}">
{% endblock %}
{% block body %}
<div class="admin-login-container">
<form action="" method="POST">
<h2>Admin - Connexion</h2>
<div class="admin-form-group">
<label for="username">Nom d'utilisateur</label>
<input type="text" id="username" name="_username" required />
</div>
<div class="admin-form-group">
<label for="password">Mot de passe</label>
<input type="password" id="password" name="_password" required />
</div>
<div class="admin-form-group">
<button type="submit" class="admin-button">Se connecter</button>
</div>
</form>
</div>
{% endblock %}
le css j'ai ajouter deux fichier juste mais ca tu peut le changer ( public/css/
styles.css et admin-styles.csss
styles.css
body {
font-family: 'Arial, sans-serif';
background-color: #f0f0f0;
}
.login-container {
width: 300px;
margin: 100px auto;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.login-container h2 {
margin-bottom: 20px;
text-align: center;
font-size: 24px;
color: #333;
}
.login-container .form-group {
margin-bottom: 15px;
}
/* */
.register-container {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f4f4f4;
}
.register-form {
width: 300px;
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.form-group {
margin-bottom: 1rem;
}
.form-group label {
display: inline-block;
width: 100%;
text-align: center;
margin-bottom: 8px;
font-weight: bold;
}
.form-group input {
display: block;
width: 100%;
padding: 10px;
margin: 0 auto;
border: 1px solid #ccc;
border-radius: 4px;
}
button[type="submit"] {
display: block;
margin: 0 auto;
padding: 10px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
admin-styles.csss
.admin-login-container {
width: 400px;
margin: auto;
border: 1px solid #ccc;
border-radius: 10px;
padding: 20px;
background-color: #f9f9f9;
}
.admin-form-group {
margin-bottom: 15px;
}
.admin-form-group label {
font-weight: bold;
display: block;
margin-bottom: 5px;
}
.admin-form-group input {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
.admin-button {
background-color: #007bff;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}
.admin-button:hover {
background-color: #0056b3;
}
template/registration/register.html.twig
{% extends 'base.html.twig' %}
{% block stylesheets %}
<link rel="stylesheet" href="{{ asset('css/styles.css') }}">
{% endblock %}
{% block body %}
<div class="register-container">
<form class="register-form" method="POST">
<h2>Inscription</h2>
{% for flash_error in app.flashes('verify_email_error') %}
<div class="alert alert-danger" role="alert">{{ flash_error }}</div>
{% endfor %}
{{ form_errors(registrationForm) }}
{{ form_start(registrationForm) }}
<div class="form-group">
{{ form_label(registrationForm.email) }}
{{ form_widget(registrationForm.email) }}
</div>
<div class="form-group">
{{ form_label(registrationForm.plainPassword) }}
{{ form_widget(registrationForm.plainPassword) }}
</div>
<div class="form-group">
{{ form_label(registrationForm.firstName, 'Prénom') }}
{{ form_widget(registrationForm.firstName) }}
</div>
<div class="form-group">
{{ form_label(registrationForm.lastName, 'Nom') }}
{{ form_widget(registrationForm.lastName) }}
</div>
<div class="form-group">
{{ form_label(registrationForm.agreeTerms, "J'accepte les conditions
d'utilisation") }}
{{ form_widget(registrationForm.agreeTerms) }}
</div>
<div class="form-group">
<button type="submit">S'inscrire</button>
</div>
{{ form_end(registrationForm) }}
</form>
</div>
{% endblock %}
namespace App\DataFixtures;
use App\Entity\User;
use Doctrine\Persistence\ObjectManager;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
$manager->flush();
}
}