<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\AskRecoveryType;
use App\Form\RecoverType;
use App\Service\UserService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class ManagerSecurityController extends AbstractController
{
const DEFAULT_ROUTE = 'manager_home';
public function nolocale(): RedirectResponse
{
return $this->redirectToRoute('manager_login', ['_locale' => 'en']);
}
public function login(AuthenticationUtils $authUtils): Response
{
if ($this->getUser() instanceof User) {
return $this->redirectToRoute(self::DEFAULT_ROUTE);
}
$error = $authUtils->getLastAuthenticationError();
$lastUsername = $authUtils->getLastUsername();
return $this->render('security/manager/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
]);
}
public function askRecovery(Request $request, UserService $util): Response
{
if ($this->getUser()) {
return $this->redirectToRoute(self::DEFAULT_ROUTE);
}
$form = $this->createForm(AskRecoveryType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$user = $util->getUserByEmail($data['email']);
if ($user !== null) {
$util->sendRecovery($user);
}
return $this->render('security/recover_sent.html.twig');
}
return $this->render('security/ask_recovery.html.twig', [
'form' => $form->createView(),
'error' => false,
]);
}
public function recover(Request $request, string $token, UserService $util): Response
{
if ($this->getUser()) {
return $this->redirectToRoute(self::DEFAULT_ROUTE);
}
if ($token === null) {
return $this->render('security/recover_invalid_token.html.twig');
}
$user = $util->getRecoverableUser($token);
if ($user === null) {
return $this->render('security/recover_invalid_token.html.twig');
}
$form = $this->createForm(RecoverType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$util->changePassword($user, $data['password']);
return $this->redirectToRoute(self::DEFAULT_ROUTE);
}
return $this->render('security/recover.html.twig', [
'form' => $form->createView(),
]);
}
public function createPassword(Request $request, string $token, UserService $util): Response
{
if ($this->getUser()) {
return $this->redirectToRoute(self::DEFAULT_ROUTE);
}
if ($token === null) {
return $this->render('security/create_password_invalid_token.html.twig');
}
$user = $util->getRecoverableUser($token);
if ($user === null) {
return $this->render('security/create_password_invalid_token.html.twig');
}
$form = $this->createForm(RecoverType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$util->changePassword($user, $data['password']);
return $this->redirectToRoute(self::DEFAULT_ROUTE);
}
return $this->render('security/create_password.html.twig', [
'form' => $form->createView(),
]);
}
}