src/Controller/SecurityController.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Users\Child;
  4. use App\Entity\Users\User;
  5. use App\Repository\Users\UserRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\EventDispatcher\EventDispatcher;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  13. use Symfony\Component\Security\Http\Attribute\CurrentUser;
  14. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  15. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  16. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  17. use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
  18. class SecurityController extends AbstractController
  19. {
  20.     //ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
  21.     /**
  22.      * @Route("/connexion", name="app_login")
  23.      */
  24.     public function login(AuthenticationUtils $authenticationUtils): Response
  25.     {
  26.          if ($this->getUser()) {
  27.              return $this->redirectToRoute('dashboard');
  28.          }
  29.         // get the login error if there is one
  30.         $error = $authenticationUtils->getLastAuthenticationError();
  31.         // last username entered by the user
  32.         $lastUsername = $authenticationUtils->getLastUsername();
  33.         return $this->render('security/connexion.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
  34.     }
  35.     /**
  36.      * @Route("/verify", name="registration_confirmation_route")
  37.      */
  38.     public function verifyUserEmail(Request $request,VerifyEmailHelperInterface $helper,UserRepository $userRepository): Response
  39.     {
  40.         $id = $request->get('id');
  41.        if (null === $id) {
  42.            return $this->redirectToRoute('front');
  43.        }
  44.        $user = $userRepository->find($id);
  45.        // Ensure the user exists in persistence
  46.        if (null === $user) {
  47.            return $this->redirectToRoute('front');
  48.        }
  49.         // Do not get the User's Id or Email Address from the Request object
  50.         try {
  51.             $helper->validateEmailConfirmation($request->getUri(), $user->getId(), $user->getEmail());
  52.             $user->setIsVerified(true);
  53.             $this->getDoctrine()->getManager()->flush();
  54.         } catch (VerifyEmailExceptionInterface $e) {
  55.             $this->addFlash('verify_email_error', $e->getReason());
  56.             return $this->redirectToRoute('front');
  57.         }
  58.         // Mark your user as verified. e.g. switch a User::verified property to true
  59.         $this->addFlash('notice', 'Super ! Tu peux maintenant te connecter à ton espace de révisions.');
  60.         return $this->redirectToRoute('app_login');
  61.     }
  62.     /**
  63.      * @Route("/logout", name="app_logout")
  64.      */
  65.     public function logout()
  66.     {
  67.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  68.     }
  69.     /**
  70.      * @Route("/api/login", name="api_login_v2",methods={"post"})
  71.      */
  72.     public function index(?User $user)
  73.     {
  74.         if ($this->getUser() === $user) {
  75.             return $this->json([
  76.                 'message' => 'missing credentials',
  77.             ], Response::HTTP_UNAUTHORIZED);
  78.         }
  79.         return $this->json([
  80.             'user'  => $this->getUser()->getUserIdentifier(),
  81.             'path'=> $_SERVER['HTTP_REFERER']
  82.         ]);
  83.     }
  84.     /**
  85.      * @Route("/impersonate/user/switch")
  86.      */
  87.     public function impersonateUser(Request $request,EntityManagerInterface $entityManager){
  88.         $email = $request->get('email');
  89.         $email = urldecode($email);
  90.         $email = str_replace(' ','+',$email);
  91.         if(filter_var($email,FILTER_VALIDATE_EMAIL)){
  92.             $current = $entityManager->getRepository(User::class)->findOneByEmail($email);
  93.             if($current){
  94.               //  $cookies = $request->cookies;
  95.             //    if (!is_null($cookies->get('admin_switch_user'))) {
  96.               //      $cookieTraining = $cookies->get('admin_switch_user');
  97.                //     $value = json_decode($cookieTraining,true);
  98.                     //$current = $entityManager->getRepository(User::class)->findOneByEmail($value['current']);
  99.                     // login user
  100.                     //if($current){
  101.                         $token = new UsernamePasswordToken($current, $current->getPassword(), "main", $current->getRoles());
  102.                         $this->get("security.token_storage")->setToken($token);
  103.                         $this->get('session')->set('_security_main', serialize($token));
  104.                         $event = new InteractiveLoginEvent($request, $token);
  105.                         $dispatcher = new EventDispatcher();
  106.                         $dispatcher->dispatch($event, "security.interactive_login");
  107.                         $url = $this->generateUrl('dashboard').'?_switch_user='.urlencode($email);
  108.                         return $this->redirect($url);
  109.                     //}
  110.                 //}
  111.             }
  112.         }
  113.         return $this->redirectToRoute("dashboard");
  114.     }
  115. }