public function requestAction(Request $request) { $form = $this->formFactory->create('admin_password_reset_request'); $form->handleRequest($request); if ($form->isValid()) { /** @var UserPasswordResetInterface $user */ $user = $this->userRepository->findUserByEmail($form->get('email')->getData()); if (null === $user) { return $this->addFlashAndRedirect($request, 'alert-success', 'admin.password_reset.request.mail_sent'); } if ($user->isPasswordRequestNonExpired($this->tokeTtl)) { return $this->addFlashAndRedirect($request, 'alert-warning', 'admin.password_reset.request.already_requested'); } $user->setConfirmationToken($this->tokenGenerator->generateToken()); $user->setPasswordRequestedAt(new \DateTime()); $this->userRepository->save($user); $this->mailer->sendPasswordResetMail($user); return $this->addFlashAndRedirect($request, 'alert-success', 'admin.password_reset.request.mail_sent'); } return $this->templating->renderResponse($this->requestActionTemplate, array('form' => $form->createView())); }
/** * @param \Symfony\Component\HttpFoundation\Request $request * @param \Symfony\Component\Form\FormFactoryInterface $formFactory * @param \Symfony\Component\Form\FormInterface $form * @param \Symfony\Component\Form\FormInterface $form2 * @param \FSi\Bundle\AdminSecurityBundle\Model\UserRepositoryInterface $userRepository * @param \FSi\Bundle\AdminSecurityBundle\Model\UserPasswordResetInterface $user * @param \FSi\Bundle\AdminSecurityBundle\Token\TokenGeneratorInterface $tokenGenerator * @param \FSi\Bundle\AdminSecurityBundle\Mailer\MailerInterface $mailer * @param \Symfony\Component\HttpFoundation\Session\Session $session * @param \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface $flashBag * @param \Symfony\Component\Routing\RouterInterface $router */ function it_updates_confirmation_token_and_sends_mail($request, $formFactory, $form, $form2, $userRepository, $user, $tokenGenerator, $mailer, $session, $flashBag, $router) { $formFactory->create('admin_password_reset_request')->willReturn($form); $form->handleRequest($request)->shouldBeCalled(); $form->isValid()->willReturn(true); $form->get('email')->willReturn($form2); $form2->getData()->willReturn('*****@*****.**'); $userRepository->findUserByEmail('*****@*****.**')->willReturn($user); $user->isPasswordRequestNonExpired(3600 * 12)->willReturn(false); $tokenGenerator->generateToken()->willReturn('token1234'); $user->setConfirmationToken('token1234')->shouldBeCalled(); $user->setPasswordRequestedAt(Argument::type('\\DateTime'))->shouldBeCalled(); $userRepository->save($user)->shouldBeCalled(); $mailer->sendPasswordResetMail($user)->shouldBeCalled(); $request->getSession()->willReturn($session); $session->getFlashBag()->willReturn($flashBag); $flashBag->add('alert-success', 'admin.password_reset.request.mail_sent')->shouldBeCalled(); $router->generate('fsi_admin_security_password_reset_request')->willReturn('url'); $response = $this->requestAction($request); $response->shouldHaveType('Symfony\\Component\\HttpFoundation\\RedirectResponse'); }