src/Controller/RegistrationController.php line 160

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use App\Security\EmailVerifier;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Mime\Address;
  12. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  16. use App\Repository\UserRepository;
  17. class RegistrationController extends AbstractController
  18. {
  19.     private EmailVerifier $emailVerifier;
  20.     public function __construct(EmailVerifier $emailVerifier)
  21.     {
  22.         $this->emailVerifier $emailVerifier;
  23.     }
  24.     #[Route('/register'name'app_register')]
  25.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherEntityManagerInterface $entityManager): Response
  26.     {
  27.         if (!$this->getUser()) {return $this->redirectToRoute('app_login');} //check connexion
  28.         $user = new User();
  29.         $form $this->createForm(RegistrationFormType::class, $user);
  30.         $form->remove('firstName');
  31.         $form->remove('lastName');
  32.         $form->remove('phoneNumber');
  33.         $form->remove('plainPassword');
  34.         $form->remove('isActive');
  35.         $form->handleRequest($request);
  36.         
  37.         if ($form->isSubmitted() && $form->isValid()) {
  38.             // genere activation code as password to send and it
  39.             $chars "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  40.             $passwordToSend=  substr(str_shuffle($chars),0,7);
  41.             $user->setPassword($userPasswordHasher->hashPassword($user,$passwordToSend));
  42.             $user->setIsActive(true);
  43.             //dd($this->emailVerifier);
  44.            // dd($user);
  45.          $entityManager->persist($user);
  46.             $entityManager->flush();
  47.             // generate a signed url and email it to the user
  48.              
  49.              $this->emailVerifier->sendEmailConfirmation('app_verify_email'$user,
  50.                 (new TemplatedEmail())
  51.                     ->from(new Address('no-reply@meridec.ch''Tecnicaltechnical support'))
  52.                     ->to($user->getEmail())
  53.                     ->subject('Please Confirm your Email code : '.$passwordToSend.'')
  54.                     //->htmlTemplate('registration/confirmation_email.html.twig',['passwordToSend'=>$passwordToSend])
  55.                     ->htmlTemplate('registration/confirmation_email.html.twig')
  56.             );
  57.             // do anything else you need here, like send an email
  58.             return $this->redirectToRoute('app_myhome');
  59.         }
  60.         return $this->render('registration/register.html.twig', [
  61.             'registrationForm' => $form->createView(),
  62.         ]);
  63.     }
  64.     #[Route('/verify/email'name'app_verify_email')]
  65.     public function verifyUserEmail(Request $requestTranslatorInterface $translator): Response
  66.     {
  67.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  68.         // validate email confirmation link, sets User::isVerified=true and persists
  69.         try {
  70.             $this->emailVerifier->handleEmailConfirmation($request$this->getUser());
  71.         } catch (VerifyEmailExceptionInterface $exception) {
  72.             $this->addFlash('verify_email_error'$translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));
  73.             return $this->redirectToRoute('app_register');
  74.         }
  75.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  76.         $this->addFlash('success''Your email address has been verified.');
  77.         return $this->redirectToRoute('verify_infos_registration');
  78.     }
  79. //""""""""""""""""
  80.  #[Route('/verify/infos-registration'name'verify_infos_registration')]
  81.     public function verifyInfosRegistration(Request $requestUserPasswordHasherInterface $userPasswordHasherEntityManagerInterface $entityManager): Response
  82.     {        
  83.         
  84.         //dd($this->getUser());
  85.         if (!$this->getUser()) {
  86.          return $this->redirectToRoute('app_login');
  87.         }
  88.         else if(!$this->getUser()->getIsActive()){
  89.             //check if userIsActive == true
  90.             $this->addFlash('error','Your account has been disabled ');
  91.             return $this->redirectToRoute("app_logout");
  92.         } else if ($this->getUser()->checkIfAllInformationsAreFullfilled()) {
  93.             //On vĂ©rifie que l'utilisateur a rempli toutes ces informations
  94.             return $this->redirectToRoute("app_myhome",[]);
  95.            //return $this->redirectToRoute("files_users_home_admin",[]);
  96.         }
  97.         // Coplete info user
  98.         $form=$this->createForm(RegistrationFormType::class,$this->getUser());
  99.         $form->remove('email');
  100.         $form->remove('plainPassword');
  101.         $form->remove('Roles');
  102.         $form->remove('isActive');
  103.         $form->handleRequest($request);
  104.         if($form->isSubmitted() && $form->isValid())
  105.         {
  106.       
  107.             //$user->setPassword($userPasswordHasher->hashPassword($user,$passwordToSend));
  108.             /*$user->setPassword(
  109.                 $userPasswordHasher->hashPassword(
  110.                     $user,
  111.                     $form->get('plainPassword')->getData()
  112.                 );*/
  113.             $entityManager->persist($this->getUser());
  114.             $entityManager->flush();       
  115.             $this->addFlash('success','Your profile has been modified ! ');
  116.             return $this->redirectToRoute('app_myhome');
  117.         }
  118.  return $this->render('registration/verify_infos_registration.html.twig', [
  119.             'form' => $form->createView(),
  120.     ]);
  121. }
  122.      #[Route('/display-users'name'app_display_users')]
  123.     public function displayUsers(UserRepository $userRepository): Response
  124.     {
  125.         if (!$this->getUser()) {return $this->redirectToRoute('app_login');} //check connexion
  126.         $users=$userRepository->findAll();
  127.         //dd($users);
  128.         return $this->render('registration/display_users.html.twig', [
  129.             'controller_name' => 'MyhomeController',
  130.             'users' => $users,
  131.         ]);
  132.     }
  133.     
  134.      #[Route("/edit-user-profile-{id}"name:"app_admin_edit_user_profile")]
  135.      
  136.     public function editUserProfile(User $user,Request $requestEntityManagerInterface $entityManager)
  137.     {
  138.         
  139.         if (!$this->getUser()) {return $this->redirectToRoute('app_login');} //check connexion
  140.        
  141.         $form=$this->createForm(RegistrationFormType::class,$user);
  142.         $form->remove('plainPassword');
  143.         //$form->remove('rolesList');
  144.         //$form->remove('isActive');
  145.        // $form->remove('employee_bareme');
  146.         $form->handleRequest($request);
  147.         if($form->isSubmitted() && $form->isValid())
  148.         {
  149.             
  150. //dd($user);
  151.             $entityManager->persist($user);
  152.             $entityManager->flush();
  153.             $this->addFlash('success','Your profile has been modified with success');
  154.             return $this->redirectToRoute('app_profile');
  155.         }
  156.         return $this->render('registration/admin_edit_user_profile.html.twig', [
  157.             'form' => $form->createView(),
  158.             'user' => $user,
  159.         ]);
  160.     }
  161. }