src/Controller/RegistrationController.php line 30

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Repository\UserRepository;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Bundle\SecurityBundle\Security;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. /**
  13.  * Passenger sign-up for Tuk-n-Tour.
  14.  *
  15.  * This used to belong to an unrelated "hotel" project — it built a Hotel user,
  16.  * mailed a verification link (there is no mailer configured) and then sent the
  17.  * new account to the admin dashboard, which it had no right to see, so /register
  18.  * simply 500'd. It now does the one thing this app needs: create a passenger,
  19.  * sign them in, and drop them on the map ready to book.
  20.  *
  21.  * The form is hand-rolled rather than built with the form component so the page
  22.  * can carry the app's own look with no theme wrestling.
  23.  */
  24. class RegistrationController extends AbstractController
  25. {
  26.     #[Route('/register'name'app_register'methods: ['GET''POST'])]
  27.     public function register(
  28.         Request $request,
  29.         UserPasswordHasherInterface $hasher,
  30.         EntityManagerInterface $em,
  31.         UserRepository $users,
  32.         Security $security,
  33.     ): Response {
  34.         // Already signed in? Nothing to register.
  35.         if ($this->getUser()) {
  36.             return $this->redirectToRoute('page_home');
  37.         }
  38.         $old   = ['name' => '''email' => '''phone' => ''];
  39.         $error null;
  40.         if ($request->isMethod('POST')) {
  41.             $name     trim((string) $request->request->get('name'));
  42.             $email    trim((string) $request->request->get('email'));
  43.             $phone    trim((string) $request->request->get('phone'));
  44.             $password = (string) $request->request->get('password');
  45.             $old      = ['name' => $name'email' => $email'phone' => $phone];
  46.             if (!$this->isCsrfTokenValid('register', (string) $request->request->get('_token'))) {
  47.                 $error 'Your session expired. Please try again.';
  48.             } elseif ($name === '' || $email === '' || $phone === '' || $password === '') {
  49.                 $error 'Please fill in every field.';
  50.             } elseif (!filter_var($email\FILTER_VALIDATE_EMAIL)) {
  51.                 $error 'That email address does not look right.';
  52.             } elseif (mb_strlen($password) < 6) {
  53.                 $error 'Choose a password of at least 6 characters.';
  54.             } elseif ($users->findOneBy(['email' => $email]) !== null) {
  55.                 $error 'An account with this email already exists. Try signing in instead.';
  56.             } else {
  57.                 $user = new User();
  58.                 $user->setName($name);
  59.                 $user->setEmail($email);
  60.                 $user->setPhone($phone);
  61.                 $user->setRoles(['ROLE_USER']);
  62.                 // No mailer is configured, so accounts are usable immediately.
  63.                 $user->setIsVerified(true);
  64.                 $user->setPassword($hasher->hashPassword($user$password));
  65.                 $em->persist($user);
  66.                 $em->flush();
  67.                 // Sign them straight in and send them to the map to book.
  68.                 $security->login($user);
  69.                 return $this->redirectToRoute('app_map');
  70.             }
  71.         }
  72.         return $this->render('registration/register.html.twig', [
  73.             'old'   => $old,
  74.             'error' => $error,
  75.         ]);
  76.     }
  77. }