<?php
namespace JF\JuridicusBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use JF\JuridicusBundle\Entity\PrueferEmail;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use JF\JuridicusBundle\Entity\PrueferUser;
use JF\JuridicusBundle\Entity\User;
use JF\JuridicusBundle\Entity\MailTemplate;
use Swift_Mailer;
use Swift_Message;
use Psr\Log\LoggerInterface;
/**
* Controller managing the resetting of the password
*
* @Route("/pruefer/reset_password")
*/
class ResettingPrueferUserController extends AbstractController
{
private int $tokenTtl = 86400;
public function __construct(
private EntityManagerInterface $em,
private UserPasswordHasherInterface $passwordHasher,
private Swift_Mailer $mailer,
private LoggerInterface $logger ) {
}
/**
* Request reset user password: show form
*
* @Route("/request", name="pruefer_reset_password_request")
* @Template("@JFJuridicusBundle/PrueferBereich/request.html.twig")
*/
public function requestAction()
{
return array();
}
/**
* Request reset user password: submit form and send email
*
* @Route("/send_email", name="pruefer_reset_password_send_email")
* @Method({"POST"})
* @Template("@JFJuridicusBundle/PrueferBereich/request.html.twig")
*/
public function sendEmailAction(Request $request)
{
$username = $request->request->get('username');
$user = $this->em->getRepository(User::class)->findOneBy(['username' => $username]);
if (null === $user) {
// Back To Form
return array('invalid_username' => $username);
}
$prueferUser = $this->em->getRepository(PrueferUser::class)->findOneByEmail($username);
if (null === $prueferUser) {
// Back To Form
return array('invalid_username' => $username);
}
if ($user->isPasswordRequestNonExpired($this->tokenTtl)) {
return $this->render('@JFJuridicusBundle/PrueferBereich/password_already_requested.html.twig');
}
$token = $user->getConfirmationToken();
if (null === $token) {
$token = uniqid(md5(rand()));
$user->setConfirmationToken($token);
}
$user->setPasswordRequestedAt(new \DateTimeImmutable());
$this->em->persist($user);
$this->em->flush();
$link = $this->generateUrl('pruefer_reset_password_activate', array('token' => $token), UrlGeneratorInterface::ABSOLUTE_URL); //chagngedFrom true
// finde neuestes Template -> TODO
$template = $this->em->getRepository(MailTemplate::class)->findCurrentByTyp(PrueferEmail::PRUEFER_BEREICH_PASSWORT_RESET);
$replacements = array(
'vorname' => $prueferUser->getVorname(),
'nachname' => $prueferUser->getNachname(),
'email' => $prueferUser->getEmail(),
//'handynummer' => $prueferUser->getHandynummer(),
'link' => $link
);
$this->logger->info(sprintf('Link: %s ', $link));
$mailerBcc = $this->getParameter('mailer_bcc');
$mailUser = $this->getParameter('mailer_user');
$message = (new Swift_Message($template->getBetreff()))
->setFrom(array($mailUser => 'Juridicus'))
->setReplyTo('info@juridicus.de')
->setTo($prueferUser->getEmail())
->setBcc('info@juridicus.de')
->setBody($template->replace($replacements), 'text/html')
;
if ($this->mailer->send($message)) {
// Versand speichern
$this->email = new PrueferEmail();
$this->email
->setTyp(PrueferEmail::PRUEFER_BEREICH_PASSWORT_RESET)
->setPrueferUser($prueferUser)
;
$this->em->persist($this->email);
$this->container->get('session')->set('pruefer_reset_password_email_success', $prueferUser->getEmail());
return $this->redirect($this->generateUrl('pruefer_reset_password_email_success'));
} else {
$this->container->get('session')->set('pruefer_reset_password_email_success', $prueferUser->getEmail());
return $this->redirect($this->generateUrl('pruefer_reset_password_email_error'));
}
}
/**
* Reset user password and send sms
*
* @Route("/activate/{token}", name="pruefer_reset_password_activate")
* @param string $token
* @return \Symfony\Component\HttpFoundation\Response
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function resetAction($token)
{
//$sms_service = $this->container->get('jf_juridicus.sms');
/* @var $session Symfony\Component\HttpFoundation\Session */
$session = $this->container->get('session');
$user = $this->em->getRepository(User::class)->findOneBy(['confirmationToken' => $token]);
if (null === $user) {
throw $this->createNotFoundException('Der Resetcode existiert nicht.');
}
$prueferUser = $this->em->getRepository(PrueferUser::class)->findOneByUser($user);
if (null === $prueferUser) {
throw $this->createNotFoundException('Der Benutzer zum Resetcode existiert nicht.');
}
if (!$user->isPasswordRequestNonExpired($this->tokenTtl)) {
return $this->redirect($this->generateUrl('pruefer_reset_password_request'));
}
$password_plain = substr(uniqid(md5(rand())), 8, 8);
$hashedPassword = $this->passwordHasher->hashPassword(
$user,
$password_plain
);
$user->setConfirmationToken(null);
$user->setPlainPassword($password_plain);
$user->setPassword($hashedPassword);
$user->setPasswordRequestedAt(null);
$user->setEnabled(true);
$this->em->persist($user);
$this->em->flush();
$this->logger->error(sprintf('Password: %s ', $password_plain));
$this->em = $this->getDoctrine()->getManager();
// Senden des Passwords per SMS
$template = $this->em->getRepository(MailTemplate::class)->findCurrentByTyp(PrueferEmail::PRUEFER_BEREICH_PASSWORT_RESET_PASSWORT);
$replacements = array(
'vorname' => $prueferUser->getVorname(),
'nachname' => $prueferUser->getNachname(),
'email' => $prueferUser->getEmail(),
//'handynummer' => $prueferUser->getHandynummer(),
'password' => $password_plain
);
/* NEW send E-Mail with PW */
$mailerBcc = $this->getParameter('mailer_bcc');
$mailUser = $this->getParameter('mailer_user');
$message = (new Swift_Message($template->getBetreff()))
->setFrom(array($mailUser => 'Juridicus'))
->setReplyTo('info@juridicus.de')
->setTo($prueferUser->getEmail())
->setBcc('info@juridicus.de')
->setBody($template->replace($replacements), 'text/html')
;
if ($this->mailer->send($message)) {
// Versand speichern
$this->email = new PrueferEmail();
$this->email
->setTyp(PrueferEmail::PRUEFER_BEREICH_PASSWORT_RESET_PASSWORT)
->setPrueferUser($prueferUser)
;
$this->em->persist($this->email);
$this->em->flush();
$session->set('pruefer_reset_password_sms_success', $prueferUser->getEmail());
return $this->redirect($this->generateUrl('pruefer_reset_password_sms_success'));
} else {
$session->set('pruefer_reset_password_sms_error', $prueferUser->getEmail());
return $this->redirect($this->generateUrl('pruefer_reset_password_sms_error'));
}
}
/**
* Tell the user to check his email provider
*
* @Route("/email/success", name="pruefer_reset_password_email_success")
* @Template("@JFJuridicusBundle/PrueferBereich/resetting_email_success.html.twig")
*/
public function emailSuccessAction()
{
$session = $this->container->get('session');
$this->email = $session->get('pruefer_reset_password_email_success');
$session->remove('pruefer_reset_password_email_success');
if (empty($this->email)) {
// the user does not come from the sendEmail action
return $this->redirect($this->generateUrl('pruefer_reset_password_request'));
}
return array(
'email' => $this->email
);
}
/**
* Tell the user that email sending failed
*
* @Route("/email/error", name="pruefer_reset_password_email_error")
* @Template("@JFJuridicusBundle/PrueferBereich/resetting_email_error.html.twig")
*/
public function emailErrorAction()
{
$session = $this->container->get('session');
$this->email = $session->get('pruefer_reset_password_email_error');
$session->remove('pruefer_reset_password_email_error');
if (empty($this->email)) {
// the user does not come from the sendEmail action
return $this->redirect($this->generateUrl('pruefer_reset_password_request'));
}
return array(
'email' => $this->email
);
}
/**
* Tell the user to check his sms
*
* @Route("/sms/success", name="pruefer_reset_password_sms_success")
* @Template("@JFJuridicusBundle/PrueferBereich/resetting_sms_success.html.twig")
*/
public function smsSuccessAction()
{
$session = $this->container->get('session');
$this->email = $session->get('pruefer_reset_password_sms_success');
$session->remove('pruefer_reset_password_sms_success');
if (empty($this->email)) {
// the user does not come from the sendEmail action
return $this->redirect($this->generateUrl('pruefer_reset_password_request'));
}
return array(
'email' => $this->email
);
}
/**
* Tell the user that sms sending failed
*
* @Route("/sms/error", name="pruefer_reset_password_sms_error")
* @Template("@JFJuridicusBundle/PrueferBereich/resetting_sms_error.html.twig")
*/
public function smsErrorAction()
{
$session = $this->container->get('session');
$this->email = $session->get('pruefer_reset_password_sms_error');
$session->remove('pruefer_reset_password_sms_error');
if (empty($this->email)) {
// the user does not come from the sendSms action
return $this->redirect($this->generateUrl('pruefer_reset_password_request'));
}
return array(
'email' => $this->email
);
}
}