<?php
namespace App\Controller;
use App\Entity\Customer;
use App\Repository\CollaboratorRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils, Request $request): Response
{
if ($user = $this->getUser()) {
if (in_array('ROLE_ADMIN', $user->getRoles())) {
return $this->redirectToRoute('admin_index');
}
/** @var Customer $user */
if ($user instanceof Customer) {
return $this->redirectToRoute('customer_dashboard', ['id' => $user->getId()]);
}
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$label = "Adresse Email";
$inputType = "email";
$titre = "";
$connexion = "user";
if ($request->get("connexion")) {
$label = "Identitifiant";
$inputType = "text";
$titre = "administrateur";
$connexion = "admin";
}
return $this->render(
'security/login.html.twig',
[
'titre' => $titre,
'label' => $label,
'inputType' => $inputType,
'last_username' => $lastUsername,
'error' => $error,
'connexion' => $connexion,
]
);
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
/**
* remplit le champ
* @Route("/getEmail", name="user_setEmail", options={"expose"=true})
* @Method("POST")
*/
public function setEmail(CollaboratorRepository $collaboratorRepository, Request $request): Response
{
if ($request->isXmlHttpRequest()) {
$inputLogin = $request->request->get('inputLogin');
$user = $collaboratorRepository->findOneBy(['identifiant' => $inputLogin]);
if (!$user) {
throw new \Exception("User not found", Response::HTTP_NOT_FOUND);
}
$data = $user->getEmail();
return new Response($data);
}
return new Response("Cette action n'est pas autorisée");
}
}