src/Controller/LupaPassController.php line 67

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\Model\EmailRegistrationFormModel;
  4. use App\Repository\UserRepository;
  5. use App\Repository\VendorRepository;
  6. use App\Validator\CheckEmail;
  7. use App\Validator\NPWPValidation;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  12. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Mailer\MailerInterface;
  16. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Component\Validator\Constraints\NotBlank;
  19. class LupaPassController extends AbstractController
  20. {
  21.     private MailerInterface $mailer;
  22.     private ManagerRegistry $doctrine;
  23.     private UserPasswordHasherInterface $passwordHasher;
  24.     public function __construct(
  25.         MailerInterface $mailer,
  26.         ManagerRegistry $doctrine,
  27.         UserPasswordHasherInterface $passwordHasher
  28.     ) {
  29.         $this->mailer $mailer;
  30.         $this->doctrine $doctrine;
  31.         $this->passwordHasher $passwordHasher;
  32.     }
  33.     private function generatePass(): string{
  34.         $random=null;
  35.         $char='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  36.         $n=5;
  37.         for ($i=0;$i<=$n;$i++)
  38.         {
  39.             $index=rand(0,strlen($char)-1);
  40.             $random.=$char[$index];
  41.         }
  42.         return $random;
  43.     }
  44.     private function sendEmail($email$random$url){
  45.         $mail = (new TemplatedEmail())
  46.             ->from('sysadmin@wicida.ac.id')
  47.             ->to($email)
  48.             ->subject('Generate Password')
  49.             ->htmlTemplate('lupa_pass/email.html.twig')
  50.             ->context([
  51.                 'url2'=>$url.'/ubah/pass/reset/'.$random
  52.             ]);
  53.         $this->mailer->send($mail);
  54.         return true;
  55.     }
  56.     #[Route('/lupa/pass'name'app_lupa_pass')]
  57.     public function formEmail(
  58.         Request $request,
  59.         UserRepository $userRepository,
  60.         ManagerRegistry $doctrine
  61.     ):Response{
  62.         $entityManager $doctrine->getManager();
  63.         $form $this->createFormBuilder()
  64.             ->add('email'EmailType::class,[
  65.                 'constraints'=>[
  66.                     new NotBlank(),
  67.                     new CheckEmail()
  68.                 ]
  69.             ])
  70.             ->add('kirim'SubmitType::class, ['label' => 'Kirim Email'])
  71.             ->getForm();
  72.         $form->handleRequest($request);
  73.         if ($form->isSubmitted() && $form->isValid()) {
  74.             // $form->getData() holds the submitted values
  75.             // but, the original `$task` variable has also been updated
  76.             $data $form->getData();
  77.             // ... perform some action, such as saving the task to the database
  78.            $user $userRepository->findOneBy([
  79.                'email'=>$form->get('email')->getData()
  80.             ]);
  81.             $url $request->getSchemeAndHttpHost();
  82.             $random $this->generatePass();
  83.             $mailing=$this->sendEmail($form->get('email')->getData(), $random$url);
  84.             $now date_create('now');
  85.             $the_interval date_interval_create_from_date_string('1 days');
  86.             date_add($now$the_interval);
  87.             $user->setKodeLupaPass($random);
  88.            $user->setExpDate($now);
  89.             $entityManager->persist($user);
  90.             $entityManager->flush();
  91.             return $this->redirectToRoute('send_email_success');
  92.         }
  93.         return $this->render('lupa_pass/index.html.twig', [
  94.             'form' => $form->createView(),
  95.         ]);
  96.     }
  97.     #[Route('/sendEmailSuccess'name'send_email_success')]
  98.     public function sendEmailSuccess() {
  99.         return $this->render('lupa_pass/success.html.twig', [
  100.         ]);
  101.     }
  102. }