src/Controller/AuthController.php line 56

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\LoginFormType;
  4. use App\Repository\UserRepository;
  5. use App\Security\EmailVerifier;
  6. use App\Service\PasswordResetService;
  7. use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\Config\Definition\Exception\Exception;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  15. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  16. /**
  17.  * @Route("/auth", name="auth_")
  18.  */
  19. class AuthController extends AbstractController
  20. {
  21.     private EmailVerifier $emailVerifier;
  22.     private PasswordResetService $passwordResetService;
  23.     public function __construct(EmailVerifier $emailVerifierPasswordResetService $passwordResetService)
  24.     {
  25.         $this->emailVerifier $emailVerifier;
  26.         $this->passwordResetService $passwordResetService;
  27.     }
  28.     /**
  29.      * Link to this controller to start the "connect" process
  30.      *
  31.      * @Route("/irms", name="irms_start")
  32.      */
  33.     public function connectAction(ClientRegistry $clientRegistry): RedirectResponse
  34.     {
  35.         return $clientRegistry
  36.             ->getClient('irms')
  37.             ->redirect();
  38.     }
  39.     /**
  40.      * @Route("/irms/callback", name="irms_callback")
  41.      */
  42.     public function connectCheckAction(Request $requestClientRegistry $clientRegistry)
  43.     {
  44.         // deliberately empty as the callback is handled by IrmsAuthenticator
  45.     }
  46.     /**
  47.      * @Route("/login", name="login")
  48.      */
  49.     public function login(Request $requestAuthenticationUtils $authenticationUtils): Response
  50.     {
  51.         if ($this->getUser()) {
  52.             return $this->redirectToRoute('main');
  53.         }
  54.         $form $this->createForm(LoginFormType::class);
  55.         $form->handleRequest($request);
  56.         $error $authenticationUtils->getLastAuthenticationError();
  57.         if (!$error) {
  58.             $this->redirectToRoute('main');
  59.         } else {
  60.             $this->addFlash('error''Error logging in, try again or contact your instructor for assistance.');
  61.         }
  62.         return $this->render('auth/login.html.twig', [
  63.             'loginForm' => $form->createView()
  64.         ]);
  65.     }
  66.     /**
  67.      * @Route("/logout", name="logout", methods={"GET"})
  68.      * @throws Exception
  69.      */
  70.     public function logout(): void
  71.     {
  72.         throw new Exception('Error logging out');
  73.     }
  74.     /**
  75.      * @Route("/verify/email", name="verify_email")
  76.      */
  77.     public function verifyUserEmail(Request $requestUserRepository $userRepository): Response
  78.     {
  79.         $id $request->get('id');
  80.         if ($id === null) {
  81.             $this->addFlash('error''Error verifying email, contact your instructor for assistance.');
  82.             return $this->redirectToRoute('auth_login');
  83.         }
  84.         $user $userRepository->find($id);
  85.         if ($user === null) {
  86.             $this->addFlash('error''Error verifying email, contact your instructor for assistance.');
  87.             return $this->redirectToRoute('auth_login');
  88.         }
  89.         // validate email confirmation link, sets User::isVerified=true and persists
  90.         try {
  91.             $this->emailVerifier->handleEmailConfirmation($request$user);
  92.         } catch (VerifyEmailExceptionInterface $exception) {
  93.             $this->addFlash('error'$exception->getReason());
  94.             return $this->redirectToRoute('auth_login');
  95.         }
  96.         $this->passwordResetService->sendResetEmail($user->getEmail());
  97.         $this->addFlash('success''Your email address has been verified. Please check your email for a link to set your password.');
  98.         return $this->redirectToRoute('auth_login');
  99.     }
  100. }