<?php
namespace App\Controller\Admin;
use App\Enum\ContainerType;
use App\Form\Admin\PasswordReminderChangeType;
use App\Form\Admin\PasswordReminderType;
use App\Repository\SitesRepository;
use App\Service\PasswordReminderService;
use Random\RandomException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
/**
* Class AuthController
* @package App\Controller\Admin
*/
class AuthController extends AbstractController
{
/**
* @Route("/admin/login", name="admin_login")
*
* @param AuthenticationUtils $authenticationUtils
* @param SitesRepository $sitesRepository
* @return Response
*/
public function login(AuthenticationUtils $authenticationUtils, SitesRepository $sitesRepository): Response
{
$error = $authenticationUtils->getLastAuthenticationError();
$username = $authenticationUtils->getLastUsername();
if (
$this->getUser() &&
$this->getUser()->getType() &&
(in_array($this->getUser()->getType(), ['ADMIN', 'OPERATION', 'CUSTOMER_REPRESENTATIVE', 'ACCOUNTING_DEPARTMENT']))
) {
return $this->redirectToRoute('admin_home');
}
return $this->render('admin/auth/login.html.twig', [
'lastUsername' => $username,
'error' => $error,
'sites' => $sitesRepository->findAll(),
]);
}
/**
* @Route("/admin/logout", name="admin_logout")
* @param AuthenticationUtils $authenticationUtils
* @return Response
*/
public function logout(AuthenticationUtils $authenticationUtils): Response
{
}
/**
* @Route("/admin/password_reminder", name="admin_password_reminder")
*
* @param Request $request
* @param PasswordReminderService $passwordReminder
* @param SitesRepository $sitesRepository
* @return Response
* @throws RandomException
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function passwordReminder(Request $request, PasswordReminderService $passwordReminder, SitesRepository $sitesRepository): Response
{
$status = false;
$form = $this->createForm(PasswordReminderType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$email = $form->get("email")->getData();
$status = $passwordReminder->handle($email, ContainerType::ADMIN);
}
return $this->render('admin/auth/password_reminder.html.twig', [
"form" => $form->createView(),
"status" => $status,
'sites' => $sitesRepository->findAll(),
]);
}
/**
* @Route("/admin/password_approval/{token}", name="admin_password_approval")
*
* @param Request $request
* @param PasswordReminderService $passwordReminder
* @param $token
* @param SitesRepository $sitesRepository
* @return Response
*/
public function passwordApproval(Request $request, PasswordReminderService $passwordReminder, $token, SitesRepository $sitesRepository): Response
{
if ($passwordReminder->hasToken($token)) {
$status = false;
$form = $this->createForm(PasswordReminderChangeType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$password = $form->get("password")->getData();
$status = $passwordReminder->approvalPasswordChange($token, $password, $this->getDoctrine());
}
return $this->render('admin/auth/password_approval.html.twig', [
"form" => $form->createView(),
"status" => $status,
'sites' => $sitesRepository->findAll(),
]);
} else {
return new Response("");
}
}
}