/src/Zpi/UserBundle/Controller/RegistrationController.php

https://github.com/quba/ZPI · PHP · 264 lines · 201 code · 39 blank · 24 comment · 25 complexity · f49528a5458c853b46d60453f6efa1b0 MD5 · raw file

  1. <?php
  2. namespace Zpi\UserBundle\Controller;
  3. use FOS\UserBundle\Controller\RegistrationController as BaseController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\Validator\Constraints\Email;
  7. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  8. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  9. use Symfony\Component\DependencyInjection\ContainerAware;
  10. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  11. use FOS\UserBundle\Model\UserInterface;
  12. class RegistrationController extends BaseController
  13. {
  14. public function registerAction()
  15. {
  16. $form = $this->container->get('fos_user.registration.form');
  17. $formHandler = $this->container->get('fos_user.registration.form.handler');
  18. $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');
  19. $process = $formHandler->process($confirmationEnabled);
  20. if ($process) {
  21. $user = $form->getData();
  22. if ($confirmationEnabled) {
  23. $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
  24. $route = 'fos_user_registration_check_email';
  25. } else {
  26. $this->authenticateUser($user);
  27. $route = 'fos_user_registration_confirmed';
  28. }
  29. $this->setFlash('fos_user_success', 'registration.flash.user_created');
  30. $url = $this->container->get('router')->generate($route);
  31. return new RedirectResponse($url);
  32. }
  33. return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
  34. 'form' => $form->createView(),
  35. 'theme' => $this->container->getParameter('fos_user.template.theme'),
  36. ));
  37. }
  38. /**
  39. * Tell the user to check his email provider
  40. */
  41. public function checkEmailAction()
  42. {
  43. $email = $this->container->get('session')->get('fos_user_send_confirmation_email/email');
  44. $this->container->get('session')->remove('fos_user_send_confirmation_email/email');
  45. $user = $this->container->get('fos_user.user_manager')->findUserByEmail($email);
  46. if (null === $user) {
  47. throw new NotFoundHttpException(sprintf('The user with email "%s" does not exist', $email));
  48. }
  49. return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:checkEmail.html.'.$this->getEngine(), array(
  50. 'user' => $user,
  51. ));
  52. }
  53. /**
  54. * Receive the confirmation token from user email provider, login the user
  55. */
  56. public function confirmAction($token)
  57. {
  58. $user = $this->container->get('fos_user.user_manager')->findUserByConfirmationToken($token);
  59. if (null === $user) {
  60. throw new NotFoundHttpException(sprintf('The user with confirmation token "%s" does not exist', $token));
  61. }
  62. $user->setConfirmationToken(null);
  63. $user->setEnabled(true);
  64. $this->container->get('fos_user.user_manager')->updateUser($user);
  65. $this->authenticateUser($user);
  66. return new RedirectResponse($this->container->get('router')->generate('fos_user_registration_confirmed'));
  67. }
  68. /**
  69. * Tell the user his account is now confirmed
  70. */
  71. public function confirmedAction()
  72. {
  73. $user = $this->container->get('security.context')->getToken()->getUser();
  74. if (!is_object($user) || !$user instanceof UserInterface) {
  75. throw new AccessDeniedException('This user does not have access to this section.');
  76. }
  77. return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:confirmed.html.'.$this->getEngine(), array(
  78. 'user' => $user,
  79. ));
  80. }
  81. public function emailValAction()
  82. {
  83. # Is the request an ajax one?
  84. if ($this->container->get('request')->isXmlHttpRequest())
  85. {
  86. # Lets get the email parameter's value
  87. $email = $this->container->get('request')->request->get('email');
  88. #if the email is correct
  89. $emailConstraint = new Email();
  90. // all constraint "options" can be set this way
  91. $emailConstraint->message = 'Invalid email address';
  92. // use the validator to validate the value
  93. $errorList = $this->container->get('validator')->validateValue($email, $emailConstraint);
  94. $em = $this->container->get('doctrine')->getEntityManager();
  95. $user = $em
  96. ->createQuery('SELECT u FROM ZpiUserBundle:User u WHERE u.email = :email')
  97. ->setParameters(array(
  98. 'email' => $email,
  99. ))->getOneOrNullResult();
  100. if(empty($email))
  101. {
  102. $response = new Response(json_encode(array('reply' => 'E-mail cannot be empty')));
  103. $response->headers->set('Content-Type', 'application/json');
  104. return $response;
  105. }
  106. else if(count($errorList) > 0)
  107. {
  108. $response = new Response(json_encode(array('reply' => $errorList[0]->getMessage())));
  109. $response->headers->set('Content-Type', 'application/json');
  110. return $response;
  111. }
  112. else if(!is_null($user))
  113. {
  114. $response = new Response(json_encode(array('reply' => 'This e-mail is already taken.')));
  115. $response->headers->set('Content-Type', 'application/json');
  116. return $response;
  117. }
  118. else
  119. {
  120. $response = new Response(json_encode(array('reply' => 'OK!')));
  121. $response->headers->set('Content-Type', 'application/json');
  122. return $response;
  123. }#endelse
  124. }# endif this is an ajax request
  125. $response = new Response('Page not found.', 404);
  126. $response->headers->set('Content-Type', 'application/json');
  127. return $response;
  128. }
  129. public function emailValExistAction()
  130. {
  131. # Is the request an ajax one?
  132. if ($this->container->get('request')->isXmlHttpRequest())
  133. {
  134. # Lets get the email parameter's value
  135. $email = $this->container->get('request')->request->get('email');
  136. #if the email is correct
  137. $emailConstraint = new Email();
  138. // all constraint "options" can be set this way
  139. $emailConstraint->message = 'Invalid email address';
  140. // use the validator to validate the value
  141. $errorList = $this->container->get('validator')->validateValue($email, $emailConstraint);
  142. $em = $this->container->get('doctrine')->getEntityManager();
  143. $user = $em
  144. ->createQuery('SELECT u FROM ZpiUserBundle:User u WHERE u.email = :email')
  145. ->setParameters(array(
  146. 'email' => $email,
  147. ))->getOneOrNullResult();
  148. if(empty($email))
  149. {
  150. $response = new Response(json_encode(array('reply' => 'E-mail cannot be empty')));
  151. $response->headers->set('Content-Type', 'application/json');
  152. return $response;
  153. }
  154. else if(count($errorList) > 0)
  155. {
  156. $response = new Response(json_encode(array('reply' => $errorList[0]->getMessage())));
  157. $response->headers->set('Content-Type', 'application/json');
  158. return $response;
  159. }
  160. else if(is_null($user))
  161. {
  162. $response = new Response(json_encode(array('reply' => 'User with this e-mail does not exist.')));
  163. $response->headers->set('Content-Type', 'application/json');
  164. return $response;
  165. }
  166. else
  167. {
  168. $response = new Response(json_encode(array('reply' => 'OK!')));
  169. $response->headers->set('Content-Type', 'application/json');
  170. return $response;
  171. }#endelse
  172. }# endif this is an ajax request
  173. $response = new Response('Page not found.', 404);
  174. $response->headers->set('Content-Type', 'application/json');
  175. return $response;
  176. }
  177. public function emailValNonExistAction()
  178. {
  179. # Is the request an ajax one?
  180. if ($this->container->get('request')->isXmlHttpRequest())
  181. {
  182. # Lets get the email parameter's value
  183. $email = $this->container->get('request')->request->get('email');
  184. #if the email is correct
  185. $emailConstraint = new Email();
  186. // all constraint "options" can be set this way
  187. $emailConstraint->message = 'Invalid email address';
  188. // use the validator to validate the value
  189. $errorList = $this->container->get('validator')->validateValue($email, $emailConstraint);
  190. $em = $this->container->get('doctrine')->getEntityManager();
  191. $user = $em
  192. ->createQuery('SELECT u FROM ZpiUserBundle:User u WHERE u.email = :email')
  193. ->setParameters(array(
  194. 'email' => $email,
  195. ))->getOneOrNullResult();
  196. if(empty($email))
  197. {
  198. $response = new Response(json_encode(array('reply' => '')));
  199. $response->headers->set('Content-Type', 'application/json');
  200. return $response;
  201. }
  202. else if(count($errorList) > 0)
  203. {
  204. $response = new Response(json_encode(array('reply' => $errorList[0]->getMessage())));
  205. $response->headers->set('Content-Type', 'application/json');
  206. return $response;
  207. }
  208. else if(!is_null($user))
  209. {
  210. $response = new Response(json_encode(array('reply' => 'Such user already exists.')));
  211. $response->headers->set('Content-Type', 'application/json');
  212. return $response;
  213. }
  214. else
  215. {
  216. $response = new Response(json_encode(array('reply' => 'OK!')));
  217. $response->headers->set('Content-Type', 'application/json');
  218. return $response;
  219. }#endelse
  220. }# endif this is an ajax request
  221. $response = new Response('Page not found.', 404);
  222. $response->headers->set('Content-Type', 'application/json');
  223. return $response;
  224. }
  225. }