From 74f6db8a80f338bac627e2c7ef3c2879e48b4515 Mon Sep 17 00:00:00 2001 From: jleroy2023 Date: Wed, 20 Nov 2024 10:40:08 +0100 Subject: [PATCH] set admin user --- .env | 2 +- .idea/sortir.iml | 1 - config/packages/security.yaml | 1 + migrations/Version20241120093750.php | 33 +++++++++ src/Controller/AdminController.php | 70 +++++++++++++++++-- src/Entity/PasswordResetToken.php | 70 +++++++++++++++++++ .../PasswordResetTokenRepository.php | 43 ++++++++++++ templates/admin/index.html.twig | 19 +---- templates/admin/user.html.twig | 60 +++++++++++++++- 9 files changed, 273 insertions(+), 26 deletions(-) create mode 100644 migrations/Version20241120093750.php create mode 100644 src/Entity/PasswordResetToken.php create mode 100644 src/Repository/PasswordResetTokenRepository.php diff --git a/.env b/.env index 3f249f0..ad3c6b8 100644 --- a/.env +++ b/.env @@ -37,5 +37,5 @@ MESSENGER_TRANSPORT_DSN=doctrine://default?auto_setup=0 ###< symfony/messenger ### ###> symfony/mailer ### -MAILER_DSN=null://null +MAILER_DSN=smtp://no-reply@lidge.fr:t7qbPx6C1XDSWbO3XOFf@mail.lidge.fr:587 ###< symfony/mailer ### diff --git a/.idea/sortir.iml b/.idea/sortir.iml index 3d6ae95..c0db0d2 100644 --- a/.idea/sortir.iml +++ b/.idea/sortir.iml @@ -3,7 +3,6 @@ - diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 1058005..7eedffd 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -44,6 +44,7 @@ security: access_control: - { path: ^/login, roles: PUBLIC_ACCESS } - { path: ^/register, roles: PUBLIC_ACCESS } + - { path: ^/admin, roles: ROLE_ADMIN } - { path: ^/, roles: ROLE_USER } when@test: diff --git a/migrations/Version20241120093750.php b/migrations/Version20241120093750.php new file mode 100644 index 0000000..3e1d6b3 --- /dev/null +++ b/migrations/Version20241120093750.php @@ -0,0 +1,33 @@ +addSql('CREATE TABLE password_reset_token (id_password_reset_token CHAR(36) NOT NULL COMMENT \'(DC2Type:guid)\', token VARCHAR(255) NOT NULL, created_at DATETIME NOT NULL, email VARCHAR(255) NOT NULL, PRIMARY KEY(id_password_reset_token)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('ALTER TABLE participant DROP file_name'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('DROP TABLE password_reset_token'); + $this->addSql('ALTER TABLE participant ADD file_name VARCHAR(255) DEFAULT NULL'); + } +} diff --git a/src/Controller/AdminController.php b/src/Controller/AdminController.php index 5b75904..c25d77d 100644 --- a/src/Controller/AdminController.php +++ b/src/Controller/AdminController.php @@ -14,6 +14,8 @@ use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Mailer\MailerInterface; +use Symfony\Component\Mime\Email; use Symfony\Component\Routing\Attribute\Route; class AdminController extends AbstractController @@ -82,8 +84,68 @@ class AdminController extends AbstractController $this->addFlash('success', 'Utilisateur supprimé avec succès.'); return $this->redirectToRoute('app_adminUser'); // Redirigez vers la liste des utilisateurs } + #[Route('/admin/user/add', name: 'app_adminUserAdd', methods: ['POST'])] + public function userAdd(Request $request, EntityManagerInterface $entityManager, MailerInterface $mailer): Response + { + try { + // Récupérer les données envoyées par le formulaire + $nom = $request->request->get('nom'); + $prenom = $request->request->get('prenom'); + $pseudo = $request->request->get('pseudo'); + $telephone = $request->request->get('phone'); + $mail = $request->request->get('mail'); + + // Vérifier que les champs ne sont pas vides + if (!$mail || !$pseudo) { + $this->addFlash('error', 'Tous les champs sont requis.'); + return $this->redirectToRoute('app_adminUser'); + } + + // Vérifier que le mail est valide avec une regex + if (!filter_var($mail, FILTER_VALIDATE_EMAIL)) { + $this->addFlash('error', 'L\'adresse e-mail n\'est pas valide.'); + return $this->redirectToRoute('app_adminUser'); + } + + // Créer une nouvelle entité City et définir ses propriétés + $participant = new Participant(); + $participant->setNom($nom); + $participant->setPrenom($prenom); + $participant->setPseudo($pseudo); + $participant->setTelephone($telephone); + $participant->setEmail($mail); + $participant->setAdministrateur(false); + $participant->setActif(false); + $participant->setRoles(['ROLE_USER']); + $participant->setPassword(password_hash("aChanger44!", PASSWORD_BCRYPT)); + + // Enregistrer la ville dans la base de données + $entityManager->persist($participant); + $entityManager->flush(); + + // Envoyer un email de notification + $email = (new Email()) + ->from('contact@Sortir.com') + ->to($mail) + ->subject('Sortir.com | Bienvenue sur notre site !') + ->html(" +

Bonjour $pseudo,

+

Un administrateur du site vous à créé un compte sur Sortir.com !

+

Votre mot de passe temporaire est : aChanger44!

+

Toute l'équipe de Sortir vous souhaite la bienvenue !

+ "); + + $mailer->send($email); + + $this->addFlash('success', "Utilisateur ajouté ! Un email lui a été envoyé !"); + return $this->redirectToRoute('app_adminUser'); + } catch(\Exception $e) { + $this->addFlash('error', "Erreur : " . $e->getMessage()); + return $this->redirectToRoute('app_adminUser'); + } + } #[Route('/admin/user/import', name: 'participant_import', methods: ['POST'])] - public function import(Request $request, EntityManagerInterface $em): Response + public function import(Request $request, EntityManagerInterface $em): RedirectResponse { $file = $request->files->get('csv_file'); if ($file) { @@ -96,10 +158,10 @@ class AdminController extends AbstractController $participant->setPseudo($row[2]); $participant->setTelephone($row[3]); $participant->setEmail($row[4]); - $participant->setAdministrateur((bool)$row[5]); - $participant->setActif((bool)$row[6]); + $participant->setAdministrateur(false); + $participant->setActif(false); $participant->setRoles(explode('|', $row[7])); - $participant->setPassword(password_hash($row[8], PASSWORD_BCRYPT)); + $participant->setPassword(password_hash("aChanger44!", PASSWORD_BCRYPT)); $em->persist($participant); } $em->flush(); diff --git a/src/Entity/PasswordResetToken.php b/src/Entity/PasswordResetToken.php new file mode 100644 index 0000000..f7cbf18 --- /dev/null +++ b/src/Entity/PasswordResetToken.php @@ -0,0 +1,70 @@ +idPasswordResetToken; + } + + public function getToken(): ?string + { + return $this->token; + } + + public function setToken(string $token): self + { + $this->token = $token; + return $this; + } + + public function getCreatedAt(): ?\DateTimeInterface + { + return $this->createdAt; + } + + public function setCreatedAt(\DateTimeInterface $createdAt): self + { + $this->createdAt = $createdAt; + return $this; + } + + public function getEmail(): ?string + { + return $this->email; + } + + public function setEmail(string $email): self + { + $this->email = $email; + return $this; + } + + public function isExpired(): bool + { + $now = new \DateTime(); + return $this->createdAt->modify('+24 hours') < $now; + } + +} diff --git a/src/Repository/PasswordResetTokenRepository.php b/src/Repository/PasswordResetTokenRepository.php new file mode 100644 index 0000000..46f94e1 --- /dev/null +++ b/src/Repository/PasswordResetTokenRepository.php @@ -0,0 +1,43 @@ + + */ +class PasswordResetTokenRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, PasswordResetToken::class); + } + +// /** +// * @return PasswordResetToken[] Returns an array of PasswordResetToken objects +// */ +// public function findByExampleField($value): array +// { +// return $this->createQueryBuilder('p') +// ->andWhere('p.exampleField = :val') +// ->setParameter('val', $value) +// ->orderBy('p.id', 'ASC') +// ->setMaxResults(10) +// ->getQuery() +// ->getResult() +// ; +// } + +// public function findOneBySomeField($value): ?PasswordResetToken +// { +// return $this->createQueryBuilder('p') +// ->andWhere('p.exampleField = :val') +// ->setParameter('val', $value) +// ->getQuery() +// ->getOneOrNullResult() +// ; +// } +} diff --git a/templates/admin/index.html.twig b/templates/admin/index.html.twig index 35ebba0..d912f7d 100644 --- a/templates/admin/index.html.twig +++ b/templates/admin/index.html.twig @@ -5,26 +5,11 @@ {% block content %}
- - + {% include 'admin/sidebar.html.twig' %} - - -
+

Bienvenue sur le Panel d'Administration

Utilisez le menu pour accéder aux différentes sections.

- {% endblock %} diff --git a/templates/admin/user.html.twig b/templates/admin/user.html.twig index 1b1acb9..18a7643 100644 --- a/templates/admin/user.html.twig +++ b/templates/admin/user.html.twig @@ -29,9 +29,16 @@ Importer CSV - - Exporter CSV - + +
@@ -83,5 +90,52 @@ + + + + {% endblock %}