Drupal: Limit username length on import

The Drupal users table definition limits username to 60 characters; on import, limit this to 56 chars, leaving room for a duplicate "tally" at the end of the string (should it be necessary)

(DBOINC-145)
This commit is contained in:
Tristan Olive 2015-10-19 14:34:20 -04:00
parent 27035e70b6
commit 1d47227b9b
1 changed files with 8 additions and 1 deletions

View File

@ -197,8 +197,15 @@ function find_unique_name($requested_name) {
}
$same_name_tally = 1;
$cleaned_name = preg_replace('/[^a-zA-Z0-9_ \.-]/s', '_', $requested_name);
$name_length = strlen($cleaned_name);
if ($name_length > 56) {
// Name is limited to 60 characters, but we want to leave space to add a
// tally if needed (for users with duplicate names); Limit to 56 chars and
// replace the middle of the string with "..." if too long
$cleaned_name = substr_replace($cleaned_name, '...', 28, ($name_length-56)+3);
}
$unique_name = $cleaned_name;
while (db_result(db_query("SELECT uid FROM users WHERE name = '{$unique_name}' LIMIT 1"))) {
while (db_result(db_query("SELECT uid FROM {users} WHERE name = '{$unique_name}' LIMIT 1"))) {
$same_name_tally++;
$unique_name = "{$cleaned_name}_{$same_name_tally}";
}