<?php
namespace App\Controller;
use Datetime;
use App\Entity\User;
use App\Entity\UserRecovery;
use App\Form\Type\ForgotPasswordType;
use App\Form\Type\ChangePasswordType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
class AccountRecoveryController extends AbstractController
{
protected $recoveryHash = 'SadkÅÄÖajsh213asdokz';
/**
* @Route("/forgot-password", name="app_forgot_password")
*/
public function forgot(Request $request, MailerInterface $mailer) {
$form = $this->createForm(ForgotPasswordType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository('App\Entity\User')->findOneByEmail($data['email']);
if($user) {
$recovery = new UserRecovery();
$recovery->setUser($user);
$recovery->setRecovery(md5($this->recoveryHash . $user->getId()));
$recovery->setCreated(new Datetime());
$em->persist($recovery);
$em->flush();
/* $message = (new \Swift_Message('Återställ lösenord'))
->setFrom('info@sebohushalmstad.se', 'Sebohushalmstad.se')
->setTo($user->getEmail())
->setBody(
// templates/emails/account-recovery.html.twig
$this->renderView('emails/account-recovery.html.twig', [
'user' => $user,
'recovery' => $recovery
]),
'text/html'
); */
$message = (new Email())
->from('info@sebohushalmstad.se')
->to($user->getEmail())
->subject('Återställ lösenord')
->text('Återställ lösenord')
->html(
// templates/emails/account-recovery.html.twig
$this->renderView('emails/account-recovery.html.twig', [
'user' => $user,
'recovery' => $recovery
]),
);
$mailer->send($message);
$this->addFlash('success', 'En länk för att återställa ditt lösenord har skickats till din e-postadress');
} else {
// User not found
}
return $this->redirectToRoute('app_forgot_password');
}
return $this->render('forgot-password.html.twig', [
'form' => $form->createView()
]);
}
/**
* @Route("/account-recovery/{hash}", name="app_account_recovery")
*/
public function recovery($hash, Request $request, UserPasswordEncoderInterface $passwordEncoder): Response
{
$em = $this->getDoctrine()->getManager();
$userRecovery = $em->getRepository('App\Entity\UserRecovery')->findOneByRecovery($hash, [
'created' => 'DESC'
]);
$user = $userRecovery->getUser();
$form = $this->createForm(ChangePasswordType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// encode the plain password
$user->setPassword(
$passwordEncoder->encodePassword(
$user,
$form->get('plainPassword')->getData()
)
);
$em->flush();
$this->addFlash('success', 'Ditt lösenord är nu uppdaterat');
return $this->redirectToRoute('app_login');
}
return $this->render('account-recovery.html.twig', [
'userRecovery' => $userRecovery,
'form' => $form->createView()
]);
}
}