Permalink
Browse files

FOSUB Integration: make a configurable amount of guesses for a username

  • Loading branch information...
1 parent d64f90b commit f8af7bfc9e036b8bd508ca7f1beaee3ff69a9284 @asm89 asm89 committed Mar 15, 2012
@@ -37,6 +37,10 @@ public function getConfigTreeBuilder()
->scalarNode('firewall_name')->defaultValue(false)->end()
->arrayNode('fosub')
->children()
+ ->scalarNode('username_iterations')
+ ->defaultValue(5)
+ ->cannotBeEmpty()
+ ->end()
->arrayNode('properties')
->isRequired()
->useAttributeAsKey('name')
@@ -69,6 +69,7 @@ public function load(array $configs, ContainerBuilder $container)
->addArgument(new Reference('fos_user.registration.form.handler'))
->addArgument(new Reference('fos_user.user_manager'))
->addArgument(new Reference('fos_user.mailer'))
+ ->addArgument($config['fosub']['username_iterations'])
->setScope('request');
$container->setAlias('hwi_oauth.registration.form.handler', 'hwi_oauth.registration.form.handler.fosub_bridge');
@@ -33,19 +33,22 @@ class FOSUBRegistrationFormHandler implements RegistrationFormHandlerInterface
protected $userManager;
protected $mailer;
protected $registrationFormHandler;
+ protected $iterations;
/**
* Constructor.
*
* @param RegistrationFormHandler $registrationFormHandler FOSUB registration form handler
* @param UserManagerInterface $userManager FOSUB user manager
* @param MailerInterface $mailer FOSUB mailer
+ * @param integer $iterations Amount of attempts that should be made to 'guess' a unique username
*/
- public function __construct(RegistrationFormHandler $registrationFormHandler, UserManagerInterface $userManager, MailerInterface $mailer)
+ public function __construct(RegistrationFormHandler $registrationFormHandler, UserManagerInterface $userManager, MailerInterface $mailer, $iterations = 5)
{
$this->registrationFormHandler = $registrationFormHandler;
$this->userManager = $userManager;
$this->mailer = $mailer;
+ $this->iterations = $iterations;
}
/**
@@ -68,7 +71,7 @@ public function process(Request $request, Form $form, UserResponseInterface $use
if ('POST' !== $request->getMethod()) {
$user = $form->getData();
- $user->setUsername($userInformation->getDisplayName().' ('.$userInformation->getResourceOwner()->getName().' '.$userInformation->getUsername().')');
+ $user->setUsername($this->getUniqueUsername($userInformation->getDisplayName()));
if ($userInformation instanceof AdvancedUserResponseInterface) {
$user->setEmail($userInformation->getEmail());
@@ -81,6 +84,25 @@ public function process(Request $request, Form $form, UserResponseInterface $use
}
/**
+ * Attempts to get a unique username for the user.
+ *
+ * @param string $name
+ *
+ * @return string Name, or empty string if it failed after all the iterations.
+ */
+ protected function getUniqueUserName($name)
+ {
+ $i = 0;
+ $testName = $name;
+
+ do {
+ $user = $this->userManager->findUserByUsername($testName);
+ } while ($user !== null && $i < $this->iterations && $testName = $name.++$i);
+
+ return $user !== null ? '' : $testName;
+ }
+
+ /**
* Reconstructs the form handler in order to inject the right form.
*
* @param Request $request Active request

0 comments on commit f8af7bf

Please sign in to comment.