diff --git a/auth/ldap/auth.php b/auth/ldap/auth.php index cc1374e3dd7..b77c305fcea 100644 --- a/auth/ldap/auth.php +++ b/auth/ldap/auth.php @@ -737,6 +737,7 @@ class auth_plugin_ldap extends auth_plugin_base { do { $value = ldap_get_values_len($ldapconnection, $entry, $this->config->user_attribute); $value = core_text::convert($value[0], $this->config->ldapencoding, 'utf-8'); + $value = trim($value); $this->ldap_bulk_insert($value); } while ($entry = ldap_next_entry($ldapconnection, $entry)); } diff --git a/config-dist.php b/config-dist.php index baf1dded39f..513b4370ec7 100644 --- a/config-dist.php +++ b/config-dist.php @@ -139,6 +139,10 @@ $CFG->admin = 'admin'; // any existing key. // $CFG->mnetkeylifetime = 28; // +// Not recommended: Set the following to true to allow the use +// off non-Moodle standard characters in usernames. +// $CFG->extendedusernamechars = true; +// // Allow user passwords to be included in backup files. Very dangerous // setting as far as it publishes password hashes that can be unencrypted // if the backup file is publicy available. Use it only if you can guarantee diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 52d79e79925..9ea79827918 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -1171,10 +1171,11 @@ function clean_param($param, $type) { case PARAM_USERNAME: $param = fix_utf8($param); - $param = str_replace(" " , "", $param); + $param = trim($param); // Convert uppercase to lowercase MDL-16919. $param = core_text::strtolower($param); if (empty($CFG->extendedusernamechars)) { + $param = str_replace(" " , "", $param); // Regular expression, eliminate all chars EXCEPT: // alphanum, dash (-), underscore (_), at sign (@) and period (.) characters. $param = preg_replace('/[^-\.@_a-z0-9]/', '', $param); diff --git a/lib/tests/moodlelib_test.php b/lib/tests/moodlelib_test.php index f9d4dadcb06..3738cfc5350 100644 --- a/lib/tests/moodlelib_test.php +++ b/lib/tests/moodlelib_test.php @@ -656,6 +656,8 @@ class core_moodlelib_testcase extends advanced_testcase { $this->assertSame('john@doe', clean_param('john@doe', PARAM_USERNAME)); $this->assertSame('johndoe', clean_param('john~doe', PARAM_USERNAME)); $this->assertSame('johndoe', clean_param('john´doe', PARAM_USERNAME)); + $this->assertSame(clean_param('john# $%&()+_^', PARAM_USERNAME), 'john_'); + $this->assertSame(clean_param(' john# $%&()+_^ ', PARAM_USERNAME), 'john_'); $this->assertSame(clean_param('john#$%&() ', PARAM_USERNAME), 'john'); $this->assertSame('johnd', clean_param('JOHNdóé ', PARAM_USERNAME)); $this->assertSame(clean_param('john.,:;-_/|\ñÑ[]A_X-,D {} ~!@#$%^&*()_+ ?><[] ščřžžý ?ýáž?žý??šdoe ', PARAM_USERNAME), 'john.-_a_x-d@_doe'); @@ -664,7 +666,8 @@ class core_moodlelib_testcase extends advanced_testcase { $CFG->extendedusernamechars = true; $this->assertSame('john_doe', clean_param('john_doe', PARAM_USERNAME)); $this->assertSame('john@doe', clean_param('john@doe', PARAM_USERNAME)); - $this->assertSame(clean_param('john# $%&()+_^', PARAM_USERNAME), 'john#$%&()+_^'); + $this->assertSame(clean_param('john# $%&()+_^', PARAM_USERNAME), 'john# $%&()+_^'); + $this->assertSame(clean_param(' john# $%&()+_^ ', PARAM_USERNAME), 'john# $%&()+_^'); $this->assertSame('john~doe', clean_param('john~doe', PARAM_USERNAME)); $this->assertSame('john´doe', clean_param('joHN´doe', PARAM_USERNAME)); $this->assertSame('johndoe', clean_param('johnDOE', PARAM_USERNAME));