src/Controller/SecurityController.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\UserType;
  5. use App\Repository\UserRepository;
  6. use App\Repository\ExpertRepository;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  12. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  13. class SecurityController extends AbstractController
  14. {
  15.     /**
  16.      * @Route("/login", name="app_login")
  17.      */
  18.     public function login(AuthenticationUtils $authenticationUtils): Response
  19.     {
  20.         // if ($this->getUser()) {
  21.         //     return $this->redirectToRoute('target_path');
  22.         // }
  23.         // get the login error if there is one
  24.         $error $authenticationUtils->getLastAuthenticationError();
  25.         // last username entered by the user
  26.         $lastUsername $authenticationUtils->getLastUsername();
  27.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error,'error2' => '','success'=>'']);
  28.     }
  29.     /**
  30.      * @Route("/register", name="register", methods={"GET", "POST"})
  31.      */
  32.     public function new(ExpertRepository $expertRepositoryUserPasswordHasherInterface $passwordHasherRequest $requestUserRepository $userRepository): Response
  33.     {
  34.         $user = new User();
  35.         $form $this->createForm(UserType::class, $user);
  36.         $form->handleRequest($request);
  37.         if ($form->isSubmitted() && $form->isValid()) {
  38.             $users $userRepository->findByEmail($user->getEmail());
  39.             $error="";
  40.             if ($user->getPassword() != $form['password2']->getData())
  41.                 $error="Error: Password don't match!";
  42.             if (count($users)>0)    $error="Error: This email has already been registered!";
  43.             $experts $expertRepository->findByEmail($user->getEmail());
  44.             if (count($experts) == 0)    $error="Error: This email does not belong to the experts list!";
  45.             if ($error != "")       return $this->render('user/new.html.twig', ['user' => $user,'form' => $form->createView(),'error' => $error]); 
  46.             $hashedPassword $passwordHasher->hashPassword(
  47.                 $user,
  48.                 $user->getPassword()
  49.             );
  50.             $user->setPassword($hashedPassword);
  51.             $userRepository->add($usertrue);
  52.             $this->addFlash('success''Your account has been sucessfully created!');
  53.             return $this->redirectToRoute('accueil'); 
  54.         }
  55.         return $this->renderForm('user/new.html.twig', [
  56.             'user' => $user,
  57.             'form' => $form,
  58.             'error' => '',
  59.         ]);
  60.     }
  61.     /**
  62.      * @Route("/registerOther", name="register_other", methods={"GET", "POST"})
  63.      */
  64.     public function newOther(ExpertRepository $expertRepositoryUserPasswordHasherInterface $passwordHasherRequest $requestUserRepository $userRepository): Response
  65.     {
  66.         $user = new User();
  67.         $form $this->createForm(UserType::class, $user);
  68.         $form->handleRequest($request);
  69.         if ($form->isSubmitted() && $form->isValid()) {
  70.             $users $userRepository->findByEmail($user->getEmail());
  71.             $error="";
  72.             if ($user->getPassword() != $form['password2']->getData())
  73.                 $error="Error: Password don't match!";
  74.             if (count($users)>0)    $error="Error: This email has already been registered!";
  75.             if ($error != "")       return $this->render('user/new.html.twig', ['user' => $user,'form' => $form->createView(),'error' => $error]); 
  76.             $hashedPassword $passwordHasher->hashPassword(
  77.                 $user,
  78.                 $user->getPassword()
  79.             );
  80.             $user->setPassword($hashedPassword);
  81.             $userRepository->add($usertrue);
  82.             $this->addFlash('success''The account has been sucessfully created!');
  83.             return $this->redirectToRoute('accueil'); 
  84.         }
  85.         return $this->renderForm('user/new.html.twig', [
  86.             'user' => $user,
  87.             'form' => $form,
  88.             'error' => '',
  89.         ]);
  90.     }
  91.     /**
  92.      * @Route("/logout", name="app_logout")
  93.      */
  94.     public function logout(): void
  95.     {
  96.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  97.     }
  98. }