<?php
namespace App\Controller;
use App\Form\Model\EmailRegistrationFormModel;
use App\Repository\UserRepository;
use App\Repository\VendorRepository;
use App\Validator\CheckEmail;
use App\Validator\NPWPValidation;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints\NotBlank;
class LupaPassController extends AbstractController
{
private MailerInterface $mailer;
private ManagerRegistry $doctrine;
private UserPasswordHasherInterface $passwordHasher;
public function __construct(
MailerInterface $mailer,
ManagerRegistry $doctrine,
UserPasswordHasherInterface $passwordHasher
) {
$this->mailer = $mailer;
$this->doctrine = $doctrine;
$this->passwordHasher = $passwordHasher;
}
private function generatePass(): string{
$random=null;
$char='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$n=5;
for ($i=0;$i<=$n;$i++)
{
$index=rand(0,strlen($char)-1);
$random.=$char[$index];
}
return $random;
}
private function sendEmail($email, $random, $url){
$mail = (new TemplatedEmail())
->from('sysadmin@wicida.ac.id')
->to($email)
->subject('Generate Password')
->htmlTemplate('lupa_pass/email.html.twig')
->context([
'url2'=>$url.'/ubah/pass/reset/'.$random
]);
$this->mailer->send($mail);
return true;
}
#[Route('/lupa/pass', name: 'app_lupa_pass')]
public function formEmail(
Request $request,
UserRepository $userRepository,
ManagerRegistry $doctrine
):Response{
$entityManager = $doctrine->getManager();
$form = $this->createFormBuilder()
->add('email', EmailType::class,[
'constraints'=>[
new NotBlank(),
new CheckEmail()
]
])
->add('kirim', SubmitType::class, ['label' => 'Kirim Email'])
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// $form->getData() holds the submitted values
// but, the original `$task` variable has also been updated
$data = $form->getData();
// ... perform some action, such as saving the task to the database
$user = $userRepository->findOneBy([
'email'=>$form->get('email')->getData()
]);
$url = $request->getSchemeAndHttpHost();
$random = $this->generatePass();
$mailing=$this->sendEmail($form->get('email')->getData(), $random, $url);
$now = date_create('now');
$the_interval = date_interval_create_from_date_string('1 days');
date_add($now, $the_interval);
$user->setKodeLupaPass($random);
$user->setExpDate($now);
$entityManager->persist($user);
$entityManager->flush();
return $this->redirectToRoute('send_email_success');
}
return $this->render('lupa_pass/index.html.twig', [
'form' => $form->createView(),
]);
}
#[Route('/sendEmailSuccess', name: 'send_email_success')]
public function sendEmailSuccess() {
return $this->render('lupa_pass/success.html.twig', [
]);
}
}