src/Controller/SecurityController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Customer;
  4. use App\Repository\CollaboratorRepository;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  11. class SecurityController extends AbstractController
  12. {
  13. /**
  14. * @Route("/login", name="app_login")
  15. */
  16. public function login(AuthenticationUtils $authenticationUtils, Request $request): Response
  17. {
  18. if ($user = $this->getUser()) {
  19. if (in_array('ROLE_ADMIN', $user->getRoles())) {
  20. return $this->redirectToRoute('admin_index');
  21. }
  22. /** @var Customer $user */
  23. if ($user instanceof Customer) {
  24. return $this->redirectToRoute('customer_dashboard', ['id' => $user->getId()]);
  25. }
  26. }
  27. // get the login error if there is one
  28. $error = $authenticationUtils->getLastAuthenticationError();
  29. // last username entered by the user
  30. $lastUsername = $authenticationUtils->getLastUsername();
  31. $label = "Adresse Email";
  32. $inputType = "email";
  33. $titre = "";
  34. $connexion = "user";
  35. if ($request->get("connexion")) {
  36. $label = "Identitifiant";
  37. $inputType = "text";
  38. $titre = "administrateur";
  39. $connexion = "admin";
  40. }
  41. return $this->render(
  42. 'security/login.html.twig',
  43. [
  44. 'titre' => $titre,
  45. 'label' => $label,
  46. 'inputType' => $inputType,
  47. 'last_username' => $lastUsername,
  48. 'error' => $error,
  49. 'connexion' => $connexion,
  50. ]
  51. );
  52. }
  53. /**
  54. * @Route("/logout", name="app_logout")
  55. */
  56. public function logout(): void
  57. {
  58. throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  59. }
  60. /**
  61. * remplit le champ
  62. * @Route("/getEmail", name="user_setEmail", options={"expose"=true})
  63. * @Method("POST")
  64. */
  65. public function setEmail(CollaboratorRepository $collaboratorRepository, Request $request): Response
  66. {
  67. if ($request->isXmlHttpRequest()) {
  68. $inputLogin = $request->request->get('inputLogin');
  69. $user = $collaboratorRepository->findOneBy(['identifiant' => $inputLogin]);
  70. if (!$user) {
  71. throw new \Exception("User not found", Response::HTTP_NOT_FOUND);
  72. }
  73. $data = $user->getEmail();
  74. return new Response($data);
  75. }
  76. return new Response("Cette action n'est pas autorisée");
  77. }
  78. }