<?php
namespace App\Controller;
use App\Form\User\ChangePasswordType;
use App\Form\User\ForgotPasswordType;
use App\Repository\Tracking\TimeTrackRepository;
use App\Repository\UserRepository;
use App\Service\MailService;
use App\Service\TrackingService;
use Doctrine\ORM\EntityManagerInterface;
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\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Contracts\Translation\TranslatorInterface;
class SecurityController extends AbstractController
{
private $entityManager;
private $userRepository;
private $mailService;
private $translator;
private $trackingService;
public function __construct(EntityManagerInterface $entityManager,
UserRepository $userRepository,
MailService $mailService,
TranslatorInterface $translator,
TrackingService $trackingService)
{
$this->entityManager = $entityManager;
$this->userRepository = $userRepository;
$this->mailService = $mailService;
$this->translator = $translator;
$this->trackingService = $trackingService;
}
/**
* @Route("/prihlaseni/", name="app_login")
* @param AuthenticationUtils $authenticationUtils
* @return Response
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('order_index');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
/**
* @Route("/logout/", name="app_logout")
*/
public function logout()
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
/**
* @Route("/odhlasit/", name="app_before_logout")
*/
public function beforeLogout()
{
$this->trackingService->stopActiveTrack($this->getUser());
$this->entityManager->flush();
return $this->redirectToRoute('app_logout');
}
/**
* @Route("/zapomenute-heslo/", name="app_forgotten_password")
* @param Request $request
* @param UserPasswordEncoderInterface $passwordEncoder
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
*/
public function forgotPassword(Request $request)
{
$form = $this->createForm(ForgotPasswordType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$email = $form->get('email')->getData();
$user = $this->userRepository->findOneBy(['email' => $email]);
if (!$user) {
$this->addFlash('danger', $this->translator->trans('security.email_not_found'));
return $this->redirectToRoute('app_forgotten_password');
}
$hash = bin2hex(random_bytes(32));
$user->setResetPasswordHash($hash)
->setRequestPasswordDate(new \DateTime());
$this->entityManager->persist($user);
$this->entityManager->flush();
$this->mailService->sendPasswordResetLink($user->getEmail(), $hash);
$this->addFlash('success', $this->translator->trans('security.reset_link_sent'));
return $this->redirectToRoute('app_forgotten_password');
}
return $this->render('security/forgot_password.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/obnovit-heslo/", name="app_reset_password")
* @param Request $request
* @param UserPasswordEncoderInterface $passwordEncoder
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
*/
public function resetPassword(Request $request, UserPasswordEncoderInterface $passwordEncoder)
{
$errorMessage = '';
$hasError = false;
$hash = $request->query->get('hash');
$user = $this->userRepository->findOneBy(['resetPasswordHash' => $request->query->get('hash')]);
if (!is_null($hash) && $user) {
$yesterday = new \DateTime();
$yesterday = $yesterday->modify('-1days');
if ($user->getRequestPasswordDate() > $yesterday) {
$form = $this->createForm(ChangePasswordType::class, null, [
'showCurrentPassword' => false,
]);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$encodedPassword = $passwordEncoder->encodePassword($user, $form->get('password')->getData());
$user->setPassword($encodedPassword);
$this->entityManager->persist($user);
$this->entityManager->flush();
$this->addFlash('success', $this->translator->trans('security.password_changed'));
return $this->redirectToRoute('app_login');
}
}
} else {
$errorMessage = $this->translator->trans('security.link_expired');
$hasError = true;
}
} else {
$errorMessage = $this->translator->trans('security.wrong_link');
$hasError = true;
}
return $this->render('security/reset_password.html.twig', [
'form' => isset($form) ? $form->createView() : null,
'errorMessage' => $errorMessage,
'hasError' => $hasError,
]);
}
}