<?php
namespace App\Controller\Support;
use App\Entity\ApiKey;
use App\Entity\Configuration;
use App\Entity\UserAccount;
use App\Form\Support\ChangePasswordFormType;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use LogicException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class SecurityController extends AbstractController
{
/**
* @param AuthenticationUtils $authenticationUtils
* @param EntityManagerInterface $em
* @param TranslatorInterface $trans
*
* @return Response
*/
public function login(AuthenticationUtils $authenticationUtils, EntityManagerInterface $em, TranslatorInterface $trans): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('support_index');
}
$errorMessage = null;
$error = $authenticationUtils->getLastAuthenticationError();
if ($error) {
if (get_class($error) === "Symfony\Component\Security\Core\Exception\BadCredentialsException") {
try {
$phone = $em->getRepository(Configuration::class)->get('support_postman_phone');
$errorMessage = new CustomUserMessageAuthenticationException(
$trans->trans('Špatná kombinace jména a hesla. V případě problému volejte na podporu na čísle: %phone%', ['%phone%' => $phone], 'security')
);
} catch (Exception $e) {
$errorMessage = new CustomUserMessageAuthenticationException(
$trans->trans('Špatná kombinace jména a hesla.', [], 'security')
);
}
} else {
try {
$phone = $em->getRepository(Configuration::class)->get('support_postman_phone');
$errorMessage = new CustomUserMessageAuthenticationException(
$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')
);
} catch (Exception $e) {
$errorMessage = new CustomUserMessageAuthenticationException(
$trans->trans('Došlo k technické chybě. Zkuste za chvíli akci zopakovat.', [], 'security')
);
}
}
}
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('Support/login.html.twig', ['last_username' => $lastUsername, 'error' => $errorMessage]);
}
public function logout(): void
{
throw new LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
public function logoutPage(): Response
{
return $this->render('Support/logout.html.twig');
}
/**
* Zobrazí a zpracuje formulář pro změnu hesla právě přihlášeného uživatele.
* @param Request $request
* @param EntityManagerInterface $em
* @param TranslatorInterface $translator
* @param UserPasswordHasherInterface $passwordHasher
* @return Response
* @throws Exception
*/
public function passwordChange(Request $request, EntityManagerInterface $em, TranslatorInterface $translator, UserPasswordHasherInterface $passwordHasher): Response
{
$changePassword = $this->createForm(ChangePasswordFormType::class, null, ['label' => "Změna hesla", 'translation_domain' => 'support',]);
$changePassword->handleRequest($request);
if ($changePassword->isSubmitted() && $changePassword->isValid()) {
$data = $changePassword->getData();
$oldPwd = (string) $data['oldPassword'];
$newPwd = $changePassword->get('plainPassword')->getData();
if ($this->getUser() instanceof ApiKey) {
throw new Exception("Nelze měnit heslo API klíči.");
}
/** @var UserAccount $user */
$user = $this->getUser();
$checkPass = $passwordHasher->isPasswordValid($user, $oldPwd);
if ($checkPass === true) {
$user->setPassword(
$passwordHasher->hashPassword(
$user,
$newPwd
)
);
$entityManager = $em;
$entityManager->persist($user);
$entityManager->flush();
$this->addFlash('success', $translator->trans('Vaše heslo bylo změněno. ', [], 'support'));
} else {
$this->addFlash('error', $translator->trans('Zadal jsi staré heslo špatně!', [], 'support'));
return $this->render(
'Support\change_password.html.twig',
['form' => $changePassword->createView()]
);
}
} else {
return $this->render(
'Support\change_password.html.twig',
['form' => $changePassword->createView()]
);
}
return $this->redirectToRoute('support_index');
}
}