set admin user
This commit is contained in:
@@ -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("
|
||||
<h1>Bonjour $pseudo,</h1>
|
||||
<p>Un administrateur du site vous à créé un compte sur <a href='https://localhost:8080' target='_blank'>Sortir.com</a> !</p>
|
||||
<p>Votre mot de passe temporaire est : <strong>aChanger44!</strong></p>
|
||||
<p>Toute l'équipe de Sortir vous souhaite la bienvenue !</p>
|
||||
");
|
||||
|
||||
$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();
|
||||
|
||||
70
src/Entity/PasswordResetToken.php
Normal file
70
src/Entity/PasswordResetToken.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Repository\PasswordResetTokenRepository;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity(repositoryClass: PasswordResetTokenRepository::class)]
|
||||
class PasswordResetToken
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\Column(type: 'guid', unique: true)]
|
||||
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
|
||||
#[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')]
|
||||
private ?string $idPasswordResetToken = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255)]
|
||||
private ?string $token = null;
|
||||
|
||||
#[ORM\Column(type: 'datetime')]
|
||||
private ?\DateTimeInterface $createdAt = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255)]
|
||||
private ?string $email = null;
|
||||
|
||||
public function getIdPasswordResetToken(): ?string
|
||||
{
|
||||
return $this->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;
|
||||
}
|
||||
|
||||
}
|
||||
43
src/Repository/PasswordResetTokenRepository.php
Normal file
43
src/Repository/PasswordResetTokenRepository.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\PasswordResetToken;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<PasswordResetToken>
|
||||
*/
|
||||
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()
|
||||
// ;
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user