translator = $translator; $this->allow_email_pw_reset = $allow_email_pw_reset; } /** * @Route("/login", name="login", methods={"GET", "POST"}) * @param AuthenticationUtils $authenticationUtils * @return \Symfony\Component\HttpFoundation\Response */ public function login(AuthenticationUtils $authenticationUtils): \Symfony\Component\HttpFoundation\Response { // get the login error if there is one $error = $authenticationUtils->getLastAuthenticationError(); // last username entered by the user $lastUsername = $authenticationUtils->getLastUsername(); return $this->render('security/login.html.twig', [ 'last_username' => $lastUsername, 'error' => $error, ]); } /** * @Route("/pw_reset/request", name="pw_reset_request") * @param PasswordResetManager $passwordReset * @param Request $request * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response */ public function requestPwReset(PasswordResetManager $passwordReset, Request $request) { if (! $this->allow_email_pw_reset) { throw new AccessDeniedHttpException('The password reset via email is disabled!'); } if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) { throw new AccessDeniedHttpException('You are already logged in, so you can not reset your password!'); } $builder = $this->createFormBuilder(); $builder->add('user', TextType::class, [ 'label' => $this->translator->trans('pw_reset.user_or_email'), 'constraints' => [new NotBlank()], ]); $builder->add('captcha', CaptchaType::class, [ 'width' => 200, 'height' => 50, 'length' => 6, ]); $builder->add('submit', SubmitType::class, [ 'label' => 'pw_reset.submit', ]); $form = $builder->getForm(); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $passwordReset->request($form->getData()['user']); $this->addFlash('success', 'pw_reset.request.success'); return $this->redirectToRoute('login'); } return $this->render('security/pw_reset_request.html.twig', [ 'form' => $form->createView(), ]); } /** * @Route("/pw_reset/new_pw/{user}/{token}", name="pw_reset_new_pw") * @param PasswordResetManager $passwordReset * @param Request $request * @param string|null $user * @param string|null $token * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response */ public function pwResetNewPw(PasswordResetManager $passwordReset, Request $request, ?string $user = null, ?string $token = null) { if (! $this->allow_email_pw_reset) { throw new AccessDeniedHttpException('The password reset via email is disabled!'); } if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) { throw new AccessDeniedHttpException('You are already logged in, so you can not reset your password!'); } $data = [ 'username' => $user, 'token' => $token, ]; $builder = $this->createFormBuilder($data); $builder->add('username', TextType::class, [ 'label' => $this->translator->trans('pw_reset.username'), ]); $builder->add('token', TextType::class, [ 'label' => $this->translator->trans('pw_reset.token'), ]); $builder->add('new_password', RepeatedType::class, [ 'type' => PasswordType::class, 'first_options' => [ 'label' => 'user.settings.pw_new.label', ], 'second_options' => [ 'label' => 'user.settings.pw_confirm.label', ], 'invalid_message' => 'password_must_match', 'constraints' => [new Length([ 'min' => 6, 'max' => 128, ])], ]); $builder->add('submit', SubmitType::class, [ 'label' => 'pw_reset.submit', ]); $form = $builder->getForm(); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $data = $form->getData(); //Try to set the new password $success = $passwordReset->setNewPassword($data['username'], $data['token'], $data['new_password']); if (! $success) { $this->addFlash('error', 'pw_reset.new_pw.error'); } else { $this->addFlash('success', 'pw_reset.new_pw.success'); return $this->redirectToRoute('login'); } } return $this->render('security/pw_reset_new_pw.html.twig', [ 'form' => $form->createView(), ]); } /** * @Route("/logout", name="logout") */ public function logout(): void { throw new RuntimeException('Will be intercepted before getting here'); } }