src/Controller/Support/SecurityController.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Support;
  3. use App\Entity\ApiKey;
  4. use App\Entity\Configuration;
  5. use App\Entity\UserAccount;
  6. use App\Form\Support\ChangePasswordFormType;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Exception;
  9. use LogicException;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  14. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  17. class SecurityController extends AbstractController
  18. {
  19.     /**
  20.      * @param AuthenticationUtils $authenticationUtils
  21.      * @param EntityManagerInterface $em
  22.      * @param TranslatorInterface $trans
  23.      *
  24.      * @return Response
  25.      */
  26.     public function login(AuthenticationUtils $authenticationUtilsEntityManagerInterface $emTranslatorInterface $trans): Response
  27.     {
  28.         if ($this->getUser()) {
  29.             return $this->redirectToRoute('support_index');
  30.         }
  31.         $errorMessage null;
  32.         $error $authenticationUtils->getLastAuthenticationError();
  33.         if ($error) {
  34.             if (get_class($error) === "Symfony\Component\Security\Core\Exception\BadCredentialsException") {
  35.                 try {
  36.                     $phone $em->getRepository(Configuration::class)->get('support_postman_phone');
  37.                     $errorMessage = new CustomUserMessageAuthenticationException(
  38.                         $trans->trans('Špatná kombinace jména a hesla. V případě problému volejte na podporu na čísle: %phone%', ['%phone%' => $phone], 'security')
  39.                     );
  40.                 } catch (Exception $e) {
  41.                     $errorMessage = new CustomUserMessageAuthenticationException(
  42.                         $trans->trans('Špatná kombinace jména a hesla.', [], 'security')
  43.                     );
  44.                 }
  45.             } else {
  46.                 try {
  47.                     $phone $em->getRepository(Configuration::class)->get('support_postman_phone');
  48.                     $errorMessage = new CustomUserMessageAuthenticationException(
  49.                         $trans->trans('Došlo k technické chybě. Zkuste za chvíli akci zopakovat. V případě přetrvání problému obraťte se na podporu na čísle: %phone%', ['%phone%' => $phone], 'security')
  50.                     );
  51.                 } catch (Exception $e) {
  52.                     $errorMessage = new CustomUserMessageAuthenticationException(
  53.                         $trans->trans('Došlo k technické chybě. Zkuste za chvíli akci zopakovat.', [], 'security')
  54.                     );
  55.                 }
  56.             }
  57.         }
  58.         // last username entered by the user
  59.         $lastUsername $authenticationUtils->getLastUsername();
  60.         return $this->render('Support/login.html.twig', ['last_username' => $lastUsername'error' => $errorMessage]);
  61.     }
  62.     public function logout(): void
  63.     {
  64.         throw new LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  65.     }
  66.     public function logoutPage(): Response
  67.     {
  68.         return $this->render('Support/logout.html.twig');
  69.     }
  70.     /**
  71.      * Zobrazí a zpracuje formulář pro změnu hesla právě přihlášeného uživatele.
  72.      * @param Request $request
  73.      * @param EntityManagerInterface $em
  74.      * @param TranslatorInterface $translator
  75.      * @param UserPasswordHasherInterface $passwordHasher
  76.      * @return Response
  77.      * @throws Exception
  78.      */
  79.     public function passwordChange(Request $requestEntityManagerInterface $emTranslatorInterface $translatorUserPasswordHasherInterface $passwordHasher): Response
  80.     {
  81.         $changePassword $this->createForm(ChangePasswordFormType::class, null, ['label' => "Změna hesla"'translation_domain' => 'support',]);
  82.         $changePassword->handleRequest($request);
  83.         if ($changePassword->isSubmitted() && $changePassword->isValid()) {
  84.             $data $changePassword->getData();
  85.             $oldPwd = (string) $data['oldPassword'];
  86.             $newPwd $changePassword->get('plainPassword')->getData();
  87.             if ($this->getUser() instanceof ApiKey) {
  88.                 throw new Exception("Nelze měnit heslo API klíči.");
  89.             }
  90.             /** @var UserAccount $user */
  91.             $user  $this->getUser();
  92.             $checkPass $passwordHasher->isPasswordValid($user$oldPwd);
  93.             if ($checkPass === true) {
  94.                 $user->setPassword(
  95.                     $passwordHasher->hashPassword(
  96.                         $user,
  97.                         $newPwd
  98.                     )
  99.                 );
  100.                 $entityManager $em;
  101.                 $entityManager->persist($user);
  102.                 $entityManager->flush();
  103.                 $this->addFlash('success'$translator->trans('Vaše heslo bylo změněno. ', [], 'support'));
  104.             } else {
  105.                 $this->addFlash('error'$translator->trans('Zadal jsi staré heslo špatně!', [], 'support'));
  106.                 return $this->render(
  107.                     'Support\change_password.html.twig',
  108.                     ['form' => $changePassword->createView()]
  109.                 );
  110.             }
  111.         } else {
  112.             return $this->render(
  113.                 'Support\change_password.html.twig',
  114.                 ['form' => $changePassword->createView()]
  115.             );
  116.         }
  117.         return $this->redirectToRoute('support_index');
  118.     }
  119. }