src/Controller/SecurityController.php line 86

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ActorInformation;
  4. use App\Entity\User;
  5. use App\Form\CreateUserType;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  13. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  14. class SecurityController extends AbstractController
  15. {
  16.     /**
  17.      * @Route("/register", name="register", methods={"GET", "POST" })
  18.      */
  19.     public function register(UserPasswordHasherInterface $passwordHasherRequest $requestEntityManagerInterface $entityManager): Response
  20.     {
  21.         $user = new User();
  22.         $form $this->createForm(CreateUserType::class, $user);
  23.         $form->handleRequest($request);
  24.         if ($form->isSubmitted() && $form->isValid() && !empty($request->request->get("g-recaptcha-response"))) {
  25.             // hash the password (based on the security.yaml config for the $user class)
  26.             $hashedPassword $passwordHasher->hashPassword(
  27.                 $user,
  28.                 $user->getPassword()
  29.             );
  30.             $user->setRoles(['ROLE_USER''ROLE_ACTOR']);
  31.             $user->setPassword($hashedPassword);
  32.             $entityManager->persist($user);
  33.             $entityManager->flush();
  34.             $actorInformation = new ActorInformation();
  35.             $actorInformation->setUser($user);
  36.             $entityManager->persist($actorInformation);
  37.             $entityManager->flush();
  38.             return $this->redirectToRoute('user', [], Response::HTTP_SEE_OTHER);
  39.         }
  40.         return $this->renderForm('security/register.html.twig', [
  41.             'user' => $user,
  42.             'form' => $form,
  43.         ]);
  44.     }
  45.     /**
  46.      * @Route("/fakeregister", name="fakeregister", methods={"GET"})
  47.      */
  48.     public function fakeregister(UserPasswordHasherInterface $passwordHasherManagerRegistry $doctrine): Response
  49.     {
  50.         $entityManager $doctrine->getManager();
  51.         $user = new User();
  52.         $user->setEmail('julien@gentleman-codeur.fr');
  53.         $plaintextPassword "codeur";
  54.         // hash the password (based on the security.yaml config for the $user class)
  55.         $hashedPassword $passwordHasher->hashPassword(
  56.             $user,
  57.             $plaintextPassword
  58.         );
  59.         $user->setPassword($hashedPassword);
  60.         $user->setRoles(['ROLE_USER''ROLE_ADMIN']);
  61.         $entityManager->persist($user);
  62.         $entityManager->flush();
  63.         return $this->json([
  64.             'message' => 'success',
  65.         ]);
  66.     }
  67.     /**
  68.      * @Route("/login", name="login")
  69.      */
  70.     public function index(AuthenticationUtils $authenticationUtils): Response
  71.     {
  72.         // get the login error if there is one
  73.         $error $authenticationUtils->getLastAuthenticationError();
  74.         // last username entered by the user
  75.         $lastUsername $authenticationUtils->getLastUsername();
  76.         return $this->render('security/login.html.twig', [
  77.             'last_username' => $lastUsername,
  78.             'error'         => $error,
  79.         ]);
  80.     }
  81.     /**
  82.      * @Route("/logout", name="logout", methods={"GET"})
  83.      */
  84.     public function logout(): void
  85.     {
  86.         // controller can be blank: it will never be called!
  87.         throw new \Exception('Don\'t forget to activate logout in security.yaml');
  88.     }
  89.     /**
  90.      * @Route("/forgotold", name="forgotold", methods={"GET", "POST"})
  91.      */
  92.     public function forgotold(): void
  93.     {
  94.         // controller can be blank: it will never be called!
  95.         throw new \Exception('Don\'t forget to activate forgot in security.yaml');
  96.     }
  97. }