src/Czech/Controller/SecurityController.php line 21

Open in your IDE?
  1. <?php
  2. namespace Czech\Controller;
  3. use App\Exception\Central\Client\ClientInactiveException;
  4. use App\Exception\Central\User\UserInactiveException;
  5. use App\Exception\Central\User\UserNotFoundException;
  6. use App\Service\Central\User\UserPasswordManager;
  7. use Czech\Form\Type\ForgotPasswordType;
  8. use Czech\Model\ForgotPasswordModel;
  9. use Czech\Service\MailerManager;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  13. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. class SecurityController extends AbstractController
  16. {
  17.     public function login(AuthenticationUtils $authenticationUtils)
  18.     {
  19.         if ($this->getUser()) {
  20.             return $this->redirectToRoute('czech_homepage');
  21.         }
  22.         $error $authenticationUtils->getLastAuthenticationError();
  23.         $lastUsername $authenticationUtils->getLastUsername();
  24.         return $this->render('czech/security/login.html.twig', [
  25.             'last_username' => $lastUsername,
  26.             'error' => $error,
  27.         ]);
  28.     }
  29.     public function logout()
  30.     {
  31.     }
  32.     public function forgotPassword(
  33.         Request             $request,
  34.         UserPasswordManager $userPasswordManager,
  35.         FlashBagInterface   $flashBag,
  36.         TranslatorInterface $translator,
  37.         MailerManager       $mailerManager
  38.     )
  39.     {
  40.         if ($this->getUser()) {
  41.             return $this->redirectToRoute('czech_homepage');
  42.         }
  43.         $forgotPassword = new ForgotPasswordModel();
  44.         $form $this->createForm(ForgotPasswordType::class, $forgotPassword);
  45.         $form->handleRequest($request);
  46.         if ($form->isSubmitted() && $form->isValid()) {
  47.             try {
  48.                 $user $userPasswordManager->createResetPasswordToken($forgotPassword->email);
  49.                 $mailerManager->sendResetPassword($user);
  50.                 $flashBag->add('success'$translator->trans('email.forgot_password.send'));
  51.             } catch (UserNotFoundException $userNotFoundException) {
  52.                 $flashBag->add('error'$translator->trans('user.email_not_exist', [], 'validators'));
  53.             } catch (ClientInactiveException $userClientInactiveException) {
  54.                 $flashBag->add('error'$translator->trans('client.inactive', [], 'validators'));
  55.             } catch (UserInactiveException $userClientInactiveException) {
  56.                 $flashBag->add('error'$translator->trans('user.inactive', [], 'validators'));
  57.             }
  58.         }
  59.         return $this->render('czech/security/forgot_password.html.twig', [
  60.             'form' => $form->createView()
  61.         ]);
  62.     }
  63.     public function resetPassword(
  64.         Request             $request,
  65.         UserPasswordManager $userPasswordManager,
  66.         MailerManager       $mailerManager,
  67.         FlashBagInterface   $flashBag,
  68.         TranslatorInterface $translator
  69.     )
  70.     {
  71.         $resetPasswordToken $request->get('token');
  72.         try {
  73.             $user $userPasswordManager->generateNewPassword($resetPasswordToken);
  74.             $mailerManager->sendNewPassword($user);
  75.             $flashBag->add('success'$translator->trans('user.new_password_generated'));
  76.         } catch (\Exception $exception) {
  77.             $flashBag->add('error'$translator->trans('user.reset_password_failed'));
  78.         }
  79.         return $this->redirectToRoute('czech_login');
  80.     }
  81. }