Skip to content

Instantly share code, notes, and snippets.

@zerrvox
Created August 8, 2012 10:21
Show Gist options
  • Save zerrvox/3294063 to your computer and use it in GitHub Desktop.
Save zerrvox/3294063 to your computer and use it in GitHub Desktop.
FOSUserBundle and Email confirmation problem
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Application\FOS\UserBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Exception\AccountStatusException;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Controller\RegistrationController as BaseController;
use Eeni\BackendBundle\Entity\Person;
/**
* Controller managing the registration
*
* @author Thibault Duplessis <[email protected]>
* @author Christophe Coevoet <[email protected]>
*/
class RegistrationController extends BaseController
{
public function registerAction()
{
$form = $this->container->get('fos_user.registration.form');
$formHandler = $this->container->get('fos_user.registration.form.handler');
$confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');
$process = $formHandler->process($confirmationEnabled);
if ($process) {
$user = $form->getData();
// Create a person for the user
$em = $this->container->get('doctrine')->getEntityManager();
$person = new Person($em->merge($user) );
// Save person to database
$em->persist($person);
$em->flush();
$authUser = true;
if ($confirmationEnabled) {
$this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
$route = 'profile';
} else {
$authUser = true;
$route = 'profile';
}
$this->setFlash('fos_user_success', 'registration.flash.user_created');
$url = $this->container->get('router')->generate($route);
$response = new RedirectResponse($url);
if ($authUser) {
$this->authenticateUser($user, $response);
}
return $response;
}
return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
'form' => $form->createView(),
));
}
/**
* Receive the confirmation token from user email provider, login the user
*/
public function confirmAction($token)
{
$user = $this->container->get('fos_user.user_manager')->findUserByConfirmationToken($token);
if (null === $user) {
throw new NotFoundHttpException(sprintf('The user with confirmation token "%s" does not exist', $token));
}
$user->setConfirmationToken(null);
$user->setEnabled(true);
$user->setLastLogin(new \DateTime());
$user->setEmailConfirmed(true);
$this->container->get('fos_user.user_manager')->updateUser($user);
$response = new RedirectResponse($this->container->get('router')->generate('fos_user_registration_confirmed'));
$this->authenticateUser($user, $response);
return $response;
}
}
<?php
namespace Application\FOS\UserBundle\Form\Handler;
use FOS\UserBundle\Form\Handler\RegistrationFormHandler as BaseHandler;
use FOS\UserBundle\Model\UserInterface;
class RegistrationFormHandler extends BaseHandler
{
public function process($confirmation = false)
{
$user = $this->userManager->createUser();
$this->form->setData($user);
if ('POST' == $this->request->getMethod()) {
$allFormValues = $this->request->request->get($this->form->getName());
if (empty($allFormValues['username'])) {
$allFormValues['username'] = $allFormValues['email'];
}
$this->form->bind($allFormValues);
if ($this->form->isValid()) {
$this->onSuccess($user, $confirmation);
return true;
}
}
return false;
}
protected function onSuccess(UserInterface $user, $confirmation)
{
if ($confirmation) {
//$user->setEnabled(false);
// We want the user to be enabled but also want email confirmation
$user->setEnabled(true);
if (null === $user->getConfirmationToken()) {
$user->setConfirmationToken($this->tokenGenerator->generateToken());
}
$this->mailer->sendConfirmationEmailMessage($user);
} else {
$user->setEnabled(true);
if (null === $user->getConfirmationToken()) {
$user->setConfirmationToken($this->tokenGenerator->generateToken());
}
//TODO Fix bug when not being authenticated when email is sent
$this->mailer->sendConfirmationEmailMessage($user);
}
$this->userManager->updateUser($user);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment