I tried using controller events, but it wasn't enough.
I had to override the FOSUserBundle controllers.
So, I created a new bundle (MyUserBundle), child of FOSUserBundle, as explained in the link, and here's my controller:
// src/My/UserBundle/Controller/ResettingController.php
namespace My\UserBundle\Controller;
use \Symfony\Component\HttpFoundation\Request;
use FOS\UserBundle\Controller\ResettingController as BaseController;
use Symfony\Component\HttpFoundation\RedirectResponse;
class ResettingController extends BaseController
{
/**
* Request reset user password: submit form and send email
*/
public function sendEmailAction(Request $request)
{
//$username = $request->request->get('username'); // Since this action was called in a twig file by a "{{ render(controller(...)) }}", the "request" attribute is empty.
$username = $_POST['username']; // But $_POST still works
/** @var $user UserInterface */
$user = $this->container->get('fos_user.user_manager')->findUserByUsernameOrEmail($username);
if (null === $user) {
return $this->container->get('templating')->renderResponse('FOSUserBundle:Resetting:request.html.'.$this->getEngine(), array('invalid_username' => $username));
}
if ($user->isPasswordRequestNonExpired($this->container->getParameter('fos_user.resetting.token_ttl'))) {
return $this->container->get('templating')->renderResponse('FOSUserBundle:Resetting:passwordAlreadyRequested.html.'.$this->getEngine());
}
if (null === $user->getConfirmationToken()) {
/** @var $tokenGenerator \FOS\UserBundle\Util\TokenGeneratorInterface */
$tokenGenerator = $this->container->get('fos_user.util.token_generator');
$user->setConfirmationToken($tokenGenerator->generateToken());
}
$this->container->get('fos_user.mailer')->sendResettingEmailMessage($user);
$user->setPasswordRequestedAt(new \DateTime());
$this->container->get('fos_user.user_manager')->updateUser($user);
//return new RedirectResponse($this->container->get('router')->generate('fos_user_resetting_check_email',
// array('email' => $this->getObfuscatedEmail($user))
//)); // Throws an exception (because this action was called by a "{{ render(controller(..)) }}").
//header('Location: '.$this->container->get('router')->generate('fos_user_resetting_check_email', array('email' => $this->getObfuscatedEmail($user))));
//exit; // The redirect works, but the "exit" prevents the email from being sent
return new \Symfony\Component\HttpFoundation\Response(
'<script type="text/javascript">window.location = "'.$this->container->get('router')->generate('fos_user_resetting_check_email', array('email' => $this->getObfuscatedEmail($user))).'"</script>'
);
}
}
In my layout.html.twig
, I replaced
{{ render(controller('FOSUserBundle:Resetting:sendEmail')) }}
by
{{ render(controller('MyUserBundle:Resetting:sendEmail')) }}
I'm now going to embed the "check_email" action in my sidebar, as well as the other forms/templates from the FOSUserBundle.