<?php
namespace App\Controller;
use App\Form\ResetPasswordType;
use App\Form\ResetType;
use App\Service\Mail;
use App\Service\MailFailedException;
use Doctrine\Persistence\ManagerRegistry;
use Psr\Log\LoggerInterface;
use App\Entity\MailOutBox;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Routing\Annotation\Route;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="login")
*/
public function loginAction(Request $request, AuthenticationUtils $authUtils): Response
{
// get the login error if there is one
$error = $authUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authUtils->getLastUsername();
return $this->render('security/login.html.twig', array(
'last_username' => $lastUsername,
'error' => $error,
));
}
/**
* @Route("/resetPassword", name="resetPassword")
* @param Request $request
* @param AuthenticationUtils $authUtils
* @param TranslatorInterface $translator
* @param LoggerInterface $logger
* @return RedirectResponse|Response
*/
public function resetPasswordEmailAction(
Request $request,
AuthenticationUtils $authUtils,
TranslatorInterface $translator,
LoggerInterface $logger,
ValidatorInterface $validator)
{
$em = $this->getDoctrine()->getManager();
$form = $this->createFormBuilder()
->add('mail', EmailType::class,[
'label' => false,
'attr' => [
'placeholder' => 'user.email'
]
])
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted())
{
try {
$user = $em->getRepository(User::class)->findOneBy(['email' => $form->getData()['mail']]);
if ($user != null)
{
if ($user->getUserStatus()->getId() == 2)
{
$user->setToken(md5($user->getId().$user->getFirstnamelastname().time()));
$em->persist($user);
$em->flush();
$this->addFlash('success', $translator->trans('login.resetPasswordSuccess'));
} else {
$this->addFlash('error', $translator->trans('login.accountNotActivated'));
}
$template = $em->getRepository(\App\Entity\MailTemplates::class)->findOneBy(['id' => 2]);
$mailOut = new MailOutBox();
$mailOut->setRecipient($user->getEmail());
$mailOut->setSubject($template->getSubject());
$mailOut->setBody($template->getBody());
$em->persist($mailOut);
$em->flush();
return $this->redirectToRoute('homepage');
} else {
$this->addFlash('success', $translator->trans('login.noAccount'));
return $this->redirectToRoute('homepage');
}
} catch (\Exception $exception) {
$this->addFlash('error', $translator->trans('supplier.create_error'.$exception->getMessage()));
$logger->error('Error while create Admin Supplier', ['e' => $exception]);
}
}
return $this->render('security/resetPassword.html.twig', array(
'form' => $form->createView(),
));
}
}