OAuthRegistrationFormHandler.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. * This file is part of Packagist.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. * Nils Adermann <naderman@naderman.de>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Packagist\WebBundle\Form\Handler;
  12. use FOS\UserBundle\Model\UserManagerInterface;
  13. use FOS\UserBundle\Util\TokenGeneratorInterface;
  14. use HWI\Bundle\OAuthBundle\Form\RegistrationFormHandlerInterface;
  15. use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
  16. use HWI\Bundle\OAuthBundle\OAuth\Response\AdvancedUserResponseInterface;
  17. use Symfony\Component\Form\Form;
  18. use Symfony\Component\HttpFoundation\Request;
  19. /**
  20. * OAuthRegistrationFormHandler
  21. *
  22. * @author Alexander <iam.asm89@gmail.com>
  23. */
  24. class OAuthRegistrationFormHandler implements RegistrationFormHandlerInterface
  25. {
  26. private $userManager;
  27. private $tokenGenerator;
  28. /**
  29. * Constructor.
  30. *
  31. * @param UserManagerInterface $userManager
  32. * @param TokenGeneratorInterface $tokenGenerator
  33. */
  34. public function __construct(UserManagerInterface $userManager, TokenGeneratorInterface $tokenGenerator)
  35. {
  36. $this->tokenGenerator = $tokenGenerator;
  37. $this->userManager = $userManager;
  38. }
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function process(Request $request, Form $form, UserResponseInterface $userInformation)
  43. {
  44. $user = $this->userManager->createUser();
  45. $form->setData($user);
  46. if ('POST' === $request->getMethod()) {
  47. $form->bind($request);
  48. if ($form->isValid()) {
  49. $randomPassword = $this->tokenGenerator->generateToken();
  50. $user->setPlainPassword($randomPassword);
  51. $user->setEnabled(true);
  52. return true;
  53. }
  54. // if the form is not posted we'll try to set some properties
  55. } else {
  56. $user->setUsername($this->getUniqueUsername($userInformation->getNickname()));
  57. if ($userInformation instanceof AdvancedUserResponseInterface) {
  58. $user->setEmail($userInformation->getEmail());
  59. }
  60. }
  61. return false;
  62. }
  63. /**
  64. * Attempts to get a unique username for the user.
  65. *
  66. * @param string $name
  67. *
  68. * @return string Name, or empty string if it failed after 10 times
  69. *
  70. * @see HWI\Bundle\OAuthBundle\Form\FOSUBRegistrationHandler
  71. */
  72. protected function getUniqueUserName($name)
  73. {
  74. $i = 0;
  75. $testName = $name;
  76. do {
  77. $user = $this->userManager->findUserByUsername($testName);
  78. } while ($user !== null && $i < 10 && $testName = $name.++$i);
  79. return $user !== null ? '' : $testName;
  80. }
  81. }