feat: creation de l'entité USER de façon sécurisée (cf mon cours) + installation de Tailwind sur le projet + première ébauche d'une page de connexion

This commit is contained in:
2026-03-16 10:03:57 +01:00
parent 1454b9bfc8
commit 38b1293a27
30 changed files with 8159 additions and 263 deletions

View File

@@ -8,7 +8,7 @@ use Symfony\Component\Routing\Attribute\Route;
class HomeController extends AbstractController
{
#[Route('/hello')]
#[Route('/hello', name: 'app_home', methods: ['GET'])]
public function hello(): Response
{
return $this->render('home/hello.html.twig', [

View File

@@ -0,0 +1,39 @@
<?php
namespace App\Controller;
use LogicException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
#[Route(path: '/login', name: 'app_login', methods: ['GET','POST'])]
public function login(AuthenticationUtils $authenticationUtils): Response
{
// si on a un utilisateur déjà connecté, alors on le redirige sur la page d'accueil
if ($this->getUser()) {
return $this->redirectToRoute('app_home');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
]);
}
#[Route(path: '/logout', name: 'app_logout', methods: ['POST'])]
public function logout(): void
{
throw new LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}

View File

@@ -14,6 +14,9 @@ use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class UserController extends AbstractController
{
// TODO : UserPasswordHasherInterface
// voir : https://symfony.com/doc/current/security/passwords.html#hashing-the-password
/**
* Permet de vérifier si un utilisateur existe dans le ldap.
*
@@ -27,7 +30,7 @@ class UserController extends AbstractController
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
#[Route('/user/{email}')]
#[Route('/user/{email}', name: 'app_user', methods: ['GET'])]
public function index(string $email, KazApiService $apiClient): Response
{
$exist = $apiClient->getUserData($email);

268
src/Entity/User.php Normal file
View File

@@ -0,0 +1,268 @@
<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Uid\Uuid;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: '`user`')]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_EMAIL', fields: ['email'])]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')]
#[ORM\Column(type: 'uuid', unique: true)]
private ?Uuid $id;
#[ORM\Column(length: 180, unique: true)]
private ?string $email = null;
/**
* @var list<string> The user roles
*/
#[ORM\Column]
private array $roles = [];
/**
* @var ?string The hashed password
*/
#[ORM\Column]
private ?string $password = null;
#[ORM\Column(length: 255)]
private ?string $emailQuota = null;
#[ORM\Column(length: 255)]
private ?string $emailDeSecours = null;
#[ORM\Column(length: 255)]
private ?string $identifiantKaz = null;
#[ORM\Column(length: 255)]
private ?string $quota = null;
#[ORM\Column]
private ?bool $hasNextcloudAccess = null;
#[ORM\Column(length: 255)]
private ?string $nextcloudQuota = null;
#[ORM\Column]
private ?bool $hasMobilizon = null;
#[ORM\Column]
private ?bool $hasAgoraAccess = null;
#[ORM\Column(length: 255)]
private ?string $lastname = null;
#[ORM\Column(length: 255)]
private ?string $firstname = null;
public function getId(): ?Uuid
{
return $this->id;
}
public function setId(Uuid $id): static
{
$this->id = $id;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): static
{
$this->email = $email;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): 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);
}
/**
* @param list<string> $roles
*/
public function setRoles(array $roles): static
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): static
{
$this->password = $password;
return $this;
}
/**
* Ensure the session doesn't contain actual password hashes by CRC32C-hashing them, as supported since Symfony 7.3.
*/
public function __serialize(): array
{
$data = (array) $this;
$data["\0".self::class."\0password"] = hash('crc32c', $this->password);
return $data;
}
public function getEmailQuota(): ?string
{
return $this->emailQuota;
}
public function setEmailQuota(string $emailQuota): static
{
$this->emailQuota = $emailQuota;
return $this;
}
public function getEmailDeSecours(): ?string
{
return $this->emailDeSecours;
}
public function setEmailDeSecours(string $emailDeSecours): static
{
$this->emailDeSecours = $emailDeSecours;
return $this;
}
public function getIdentifiantKaz(): ?string
{
return $this->identifiantKaz;
}
public function setIdentifiantKaz(string $identifiantKaz): static
{
$this->identifiantKaz = $identifiantKaz;
return $this;
}
public function getQuota(): ?string
{
return $this->quota;
}
public function setQuota(string $quota): static
{
$this->quota = $quota;
return $this;
}
public function hasNextcloudAccess(): ?bool
{
return $this->hasNextcloudAccess;
}
public function setHasNextcloudAccess(bool $hasNextcloudAccess): static
{
$this->hasNextcloudAccess = $hasNextcloudAccess;
return $this;
}
public function getNextcloudQuota(): ?string
{
return $this->nextcloudQuota;
}
public function setNextcloudQuota(string $nextcloudQuota): static
{
$this->nextcloudQuota = $nextcloudQuota;
return $this;
}
public function hasMobilizon(): ?bool
{
return $this->hasMobilizon;
}
public function setHasMobilizon(bool $hasMobilizon): static
{
$this->hasMobilizon = $hasMobilizon;
return $this;
}
public function hasAgoraAccess(): ?bool
{
return $this->hasAgoraAccess;
}
public function setHasAgoraAccess(bool $hasAgoraAccess): static
{
$this->hasAgoraAccess = $hasAgoraAccess;
return $this;
}
public function getLastname(): ?string
{
return $this->lastname;
}
public function setLastname(string $lastname): static
{
$this->lastname = $lastname;
return $this;
}
public function getFirstname(): ?string
{
return $this->firstname;
}
public function setFirstname(string $firstname): static
{
$this->firstname = $firstname;
return $this;
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace App\Repository;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
/**
* @extends ServiceEntityRepository<User>
*/
class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, User::class);
}
/**
* Used to upgrade (rehash) the user's password automatically over time.
*/
public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
{
if (!$user instanceof User) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $user::class));
}
$user->setPassword($newHashedPassword);
$this->getEntityManager()->persist($user);
$this->getEntityManager()->flush();
}
// /**
// * @return User[] Returns an array of User objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('u')
// ->andWhere('u.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('u.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?User
// {
// return $this->createQueryBuilder('u')
// ->andWhere('u.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}