<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\RegistrationFormType;
use App\Security\EmailVerifier;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mime\Address;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
use App\Repository\UserRepository;
class RegistrationController extends AbstractController
{
private EmailVerifier $emailVerifier;
public function __construct(EmailVerifier $emailVerifier)
{
$this->emailVerifier = $emailVerifier;
}
#[Route('/register', name: 'app_register')]
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager): Response
{
if (!$this->getUser()) {return $this->redirectToRoute('app_login');} //check connexion
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->remove('firstName');
$form->remove('lastName');
$form->remove('phoneNumber');
$form->remove('plainPassword');
$form->remove('isActive');
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// genere activation code as password to send and it
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$passwordToSend= substr(str_shuffle($chars),0,7);
$user->setPassword($userPasswordHasher->hashPassword($user,$passwordToSend));
$user->setIsActive(true);
//dd($this->emailVerifier);
// dd($user);
$entityManager->persist($user);
$entityManager->flush();
// generate a signed url and email it to the user
$this->emailVerifier->sendEmailConfirmation('app_verify_email', $user,
(new TemplatedEmail())
->from(new Address('no-reply@meridec.ch', 'Tecnicaltechnical support'))
->to($user->getEmail())
->subject('Please Confirm your Email code : '.$passwordToSend.'')
//->htmlTemplate('registration/confirmation_email.html.twig',['passwordToSend'=>$passwordToSend])
->htmlTemplate('registration/confirmation_email.html.twig')
);
// do anything else you need here, like send an email
return $this->redirectToRoute('app_myhome');
}
return $this->render('registration/register.html.twig', [
'registrationForm' => $form->createView(),
]);
}
#[Route('/verify/email', name: 'app_verify_email')]
public function verifyUserEmail(Request $request, TranslatorInterface $translator): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
// validate email confirmation link, sets User::isVerified=true and persists
try {
$this->emailVerifier->handleEmailConfirmation($request, $this->getUser());
} catch (VerifyEmailExceptionInterface $exception) {
$this->addFlash('verify_email_error', $translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));
return $this->redirectToRoute('app_register');
}
// @TODO Change the redirect on success and handle or remove the flash message in your templates
$this->addFlash('success', 'Your email address has been verified.');
return $this->redirectToRoute('verify_infos_registration');
}
//""""""""""""""""
#[Route('/verify/infos-registration', name: 'verify_infos_registration')]
public function verifyInfosRegistration(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager): Response
{
//dd($this->getUser());
if (!$this->getUser()) {
return $this->redirectToRoute('app_login');
}
else if(!$this->getUser()->getIsActive()){
//check if userIsActive == true
$this->addFlash('error','Your account has been disabled ');
return $this->redirectToRoute("app_logout");
} else if ($this->getUser()->checkIfAllInformationsAreFullfilled()) {
//On vérifie que l'utilisateur a rempli toutes ces informations
return $this->redirectToRoute("app_myhome",[]);
//return $this->redirectToRoute("files_users_home_admin",[]);
}
// Coplete info user
$form=$this->createForm(RegistrationFormType::class,$this->getUser());
$form->remove('email');
$form->remove('plainPassword');
$form->remove('Roles');
$form->remove('isActive');
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
//$user->setPassword($userPasswordHasher->hashPassword($user,$passwordToSend));
/*$user->setPassword(
$userPasswordHasher->hashPassword(
$user,
$form->get('plainPassword')->getData()
);*/
$entityManager->persist($this->getUser());
$entityManager->flush();
$this->addFlash('success','Your profile has been modified ! ');
return $this->redirectToRoute('app_myhome');
}
return $this->render('registration/verify_infos_registration.html.twig', [
'form' => $form->createView(),
]);
}
#[Route('/display-users', name: 'app_display_users')]
public function displayUsers(UserRepository $userRepository): Response
{
if (!$this->getUser()) {return $this->redirectToRoute('app_login');} //check connexion
$users=$userRepository->findAll();
//dd($users);
return $this->render('registration/display_users.html.twig', [
'controller_name' => 'MyhomeController',
'users' => $users,
]);
}
#[Route("/edit-user-profile-{id}", name:"app_admin_edit_user_profile")]
public function editUserProfile(User $user,Request $request, EntityManagerInterface $entityManager)
{
if (!$this->getUser()) {return $this->redirectToRoute('app_login');} //check connexion
$form=$this->createForm(RegistrationFormType::class,$user);
$form->remove('plainPassword');
//$form->remove('rolesList');
//$form->remove('isActive');
// $form->remove('employee_bareme');
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
//dd($user);
$entityManager->persist($user);
$entityManager->flush();
$this->addFlash('success','Your profile has been modified with success');
return $this->redirectToRoute('app_profile');
}
return $this->render('registration/admin_edit_user_profile.html.twig', [
'form' => $form->createView(),
'user' => $user,
]);
}
}