src/Controller/SecurityController.php line 39

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace PPAdmin\Controller;
  3. use PlnaPenezenka\PPSDKBundle\Doctrine\Entity\Administrator;
  4. use PlnaPenezenka\PPSDKBundle\Doctrine\Repository\AdministratorsRepository;
  5. use PlnaPenezenka\PPSDKBundle\Service\AdministratorsManager;
  6. use PPAdmin\EmailNotifications\AdminPasswordResetNotifier;
  7. use PPAdmin\Form\AdminPasswordSetupForm;
  8. use PPAdmin\Model\AdminPasswordResetException;
  9. use PPAdmin\Security\AdminPasswordResetHandler;
  10. use Psr\Log\LoggerInterface;
  11. use RuntimeException;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  15. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  16. use Symfony\Component\Form\FormError;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  23. use Symfony\Component\Validator\Constraints as Assert;
  24. use Symfony\Contracts\Translation\TranslatorInterface;
  25. class SecurityController extends AbstractController
  26. {
  27.     #[Route('/login'name'login'methods: ['GET''POST'])]
  28.     function login(AuthenticationUtils $auth_utils): Response
  29.     {
  30.         return $this->render('security/login.html.twig', [
  31.             'last_username' => $auth_utils->getLastUsername(),
  32.             'error' => $auth_utils->getLastAuthenticationError()
  33.         ]);
  34.     }
  35.     #[Route('/password-reset-request'name'password-reset-request'methods: ['GET''POST'])]
  36.     function passwordResetRequest(
  37.         Request $request,
  38.         AdministratorsRepository $admin_repo,
  39.         AdminPasswordResetNotifier $password_reset_notifier,
  40.         LoggerInterface $logger,
  41.         TranslatorInterface $translator
  42.     ): Response
  43.     {
  44.         $form $this->createFormBuilder()
  45.             ->add('email'EmailType::class, [
  46.                 'label' => 'password-reset-request.form.email.label',
  47.                 'attr' => [
  48.                     'placeholder' => 'password-reset-request.form.email.placeholder'
  49.                 ],
  50.                 'help' => 'password-reset-request.form.email.help',
  51.                 'constraints' => [
  52.                     new Assert\NotBlank(),
  53.                     new Assert\Email(),
  54.                 ]
  55.             ])
  56.             ->add('submit'SubmitType::class, [
  57.                 'label' => 'password-reset-request.form.submit.label',
  58.                 'attr' => [
  59.                     'class' => 'btn btn-primary btn-block'
  60.                 ]
  61.             ])
  62.             ->getForm();
  63.         $email null;
  64.         $sent false;
  65.         $form->handleRequest($request);
  66.         if($form->isSubmitted() && $form->isValid()){
  67.             $email $form->get('email')->getData();
  68.             $admin $admin_repo->findByEmail($email);
  69.             if($admin && $admin->isActivated()){
  70.                 try {
  71.                     $password_reset_notifier->sendPasswordSetupEmail($admin);
  72.                     $sent true;
  73.                 } catch(\Exception $e){
  74.                     $logger->error(__METHOD__ ": Failed to send password reset email", [
  75.                         'exception_class' => get_class($e),
  76.                         'exception_message' => $e->getMessage()
  77.                     ]);
  78.                     $form->addError(new FormError($translator->trans('password-reset-request.form.delivery-error')));
  79.                 }
  80.             } else {
  81.                 $sent true;
  82.             }
  83.         }
  84.         return $this->render('security/password-reset-request.html.twig', [
  85.             'form' => $form->createView(),
  86.             'email' => $email,
  87.             'sent' => $sent
  88.         ]);
  89.     }
  90.     #[Route('/password-setup/{token}'name'password-setup'methods: ['GET''POST'])]
  91.     function passwordReset(
  92.         string $token,
  93.         AdminPasswordResetHandler $reset_handler,
  94.         AdministratorsManager $admin_manager,
  95.         Request $request
  96.     ): Response
  97.     {
  98.         try {
  99.             $decoded $reset_handler->decodeToken($token);
  100.         } catch(AdminPasswordResetException $e){
  101.             throw new BadRequestHttpException($e->getMessage(), $e$e->getCode());
  102.         }
  103.         $form $this->createForm(AdminPasswordSetupForm::class);
  104.         $form->handleRequest($request);
  105.         $changed false;
  106.         if($form->isSubmitted() && $form->isValid()){
  107.             $password $form->get('new_password')->getData();
  108.             $admin_manager->changeAdminPassword($decoded->admin$password);
  109.             $changed true;
  110.         }
  111.         return $this->render('security/password-setup.html.twig', [
  112.             'form' => $form->createView(),
  113.             'changed' => $changed
  114.         ]);
  115.     }
  116.     #[Route('/logout'name'logout')]
  117.     function logout()
  118.     {
  119.         throw new RuntimeException("You should not get here");
  120.     }
  121.     #[Route('/keep-alive'name'keep-alive')]
  122.     #[IsGranted('ROLE_ADMIN')]
  123.     function keepAlive(Request $request): JsonResponse
  124.     {
  125.         /** @var Administrator $user */
  126.         $user $this->getUser();
  127.         $session $request->getSession();
  128.         $session->set('keep-alive'time());
  129.         $session->getMetadataBag()->stampNew();
  130.         return $this->json([
  131.             'user_id' => $user->id,
  132.             'keep-alive' => $session->get('keep-alive')
  133.         ]);
  134.     }
  135. }