RegistrationFormHandler.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace Muzich\UserBundle\Form\Handler;
  3. use FOS\UserBundle\Model\UserManagerInterface;
  4. use FOS\UserBundle\Model\UserInterface;
  5. use FOS\UserBundle\Mailer\MailerInterface;
  6. use FOS\UserBundle\Util\TokenGeneratorInterface;
  7. use Symfony\Component\Form\FormInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. class RegistrationFormHandler
  10. {
  11. protected $request;
  12. protected $userManager;
  13. protected $form;
  14. protected $mailer;
  15. protected $tokenGenerator;
  16. protected $translator;
  17. protected $doctrine;
  18. protected $errors = array();
  19. protected $token;
  20. public function __construct(FormInterface $form, Request $request, UserManagerInterface $userManager, MailerInterface $mailer, TokenGeneratorInterface $tokenGenerator, $translator, $doctrine)
  21. {
  22. $this->form = $form;
  23. $this->request = $request;
  24. $this->userManager = $userManager;
  25. $this->mailer = $mailer;
  26. $this->tokenGenerator = $tokenGenerator;
  27. $this->translator = $translator;
  28. $this->doctrine = $doctrine;
  29. }
  30. protected function checkRegistrationInformations($user)
  31. {
  32. $form_values = $this->request->get($this->form->getName());
  33. $this->token = $this->doctrine->getRepository('MuzichCoreBundle:RegistrationToken')
  34. ->findOneBy(array('token' => $form_values["token"], 'used' => false))
  35. ;
  36. if (!$this->token)
  37. {
  38. $this->errors[] = $this->translator->trans(
  39. 'registration.token.error',
  40. array(),
  41. 'validators'
  42. );
  43. }
  44. if (strlen($user->getUsername()) < 3)
  45. {
  46. $this->errors[] = $this->translator->trans(
  47. 'error.registration.username.min',
  48. array('%limit%' => 3),
  49. 'validators'
  50. );
  51. }
  52. if (strlen($user->getUsername()) > 32)
  53. {
  54. $this->errors[] = $this->translator->trans(
  55. 'error.registration.username.max',
  56. array('%limit%' => 32),
  57. 'validators'
  58. );
  59. }
  60. if ($form_values['plainPassword']['first'] != $form_values['plainPassword']['second'])
  61. {
  62. $this->errors[] = $this->translator->trans(
  63. 'error.registration.password.notsame',
  64. array(),
  65. 'validators'
  66. );
  67. }
  68. }
  69. /**
  70. * @param boolean $confirmation
  71. */
  72. public function process($confirmation = false)
  73. {
  74. $user = $this->createUser();
  75. $this->form->setData($user);
  76. if ('POST' === $this->request->getMethod()) {
  77. $this->form->bind($this->request);
  78. $this->checkRegistrationInformations($user);
  79. if ($this->form->isValid() && !count($this->errors)) {
  80. $this->onSuccess($user, $confirmation);
  81. return true;
  82. }
  83. }
  84. return false;
  85. }
  86. /**
  87. * @param boolean $confirmation
  88. */
  89. protected function onSuccess(UserInterface $user, $confirmation)
  90. {
  91. if ($confirmation) {
  92. $user->setEnabled(false);
  93. if (null === $user->getConfirmationToken()) {
  94. $user->setConfirmationToken($this->tokenGenerator->generateToken());
  95. }
  96. $this->mailer->sendConfirmationEmailMessage($user);
  97. } else {
  98. $user->setEnabled(true);
  99. }
  100. $this->userManager->updateUser($user);
  101. }
  102. /**
  103. * @return UserInterface
  104. */
  105. protected function createUser()
  106. {
  107. return $this->userManager->createUser();
  108. }
  109. public function getErrors()
  110. {
  111. return $this->errors;
  112. }
  113. public function getToken()
  114. {
  115. return $this->token;
  116. }
  117. }