Commit 3702dea9 authored by greggles's avatar greggles

Issue #1903646 by aadityawalawalkar, greggles: Separate cleaning of names from...

Issue #1903646 by aadityawalawalkar, greggles: Separate cleaning of names from unique check in email_registration_unique_username: fixing single spaces replaced by underscores
parent 73e4e49b
......@@ -26,7 +26,10 @@
* A string defining a generated username.
*/
function hook_email_registration_name($edit, $account) {
return 'u' . $account->uid;
// Your hook implementation should ensure that the resulting string
// works as a username. You can use email_registration_cleanup_username($name)
// to clean up the name.
return email_registration_cleanup_username('u' . $account->uid);
}
/**
......
......@@ -24,6 +24,8 @@ function email_registration_user_insert(&$edit, &$account, $category = NULL) {
if (empty($names)) {
// Strip off everything after the @ sign.
$new_name = preg_replace('/@.*$/', '', $edit['mail']);
// Clean up the username.
$new_name = email_registration_cleanup_username($new_name);
}
else {
// One would expect a single implementation of the hook, but if there
......@@ -67,6 +69,27 @@ function email_registration_user_insert(&$edit, &$account, $category = NULL) {
*
*/
function email_registration_unique_username($name, $uid = 0) {
// Iterate until we find a unique name.
$i = 0;
do {
$new_name = empty($i) ? $name : $name . '_' . $i;
$found = db_query_range("SELECT uid from {users} WHERE uid <> :uid AND name = :name", 0, 1, array(':uid' => $uid, ':name' => $new_name))->fetchAssoc();
$i++;
} while (!empty($found));
return $new_name;
}
/**
* Function to clean up username
* e.g.
* Replace two or more spaces with a single underscore
* Strip illegal characters.
*
* @param username to be cleaned up
* @return cleaned up username
*/
function email_registration_cleanup_username($name) {
// Strip illegal characters.
$name = preg_replace('/[^\x{80}-\x{F7} a-zA-Z0-9@_.\'-]/', '', $name);
......@@ -74,26 +97,16 @@ function email_registration_unique_username($name, $uid = 0) {
$name = trim($name);
// Convert any other series of spaces to a single underscore.
$name = preg_replace('/ +/', '_', $name);
$name = preg_replace('/ +/', '_', $name);
// If there's nothing left use a default.
$name = ('' === $name) ? t('user') : $name;
// Truncate to reasonable size.
// Truncate to a reasonable size.
$name = (drupal_strlen($name) > (USERNAME_MAX_LENGTH - 10)) ? drupal_substr($name, 0, USERNAME_MAX_LENGTH - 11) : $name;
// Iterate until we find a unique name.
$i = 0;
do {
$new_name = empty($i) ? $name : $name . '_' . $i;
$found = db_query_range("SELECT uid from {users} WHERE uid <> :uid AND name = :name", 0, 1, array(':uid' => $uid, ':name' => $new_name))->fetchAssoc();
$i++;
} while (!empty($found));
return $new_name;
return $name;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment