Skip to content

Commit 8708bc5

Browse files
authored
Merge pull request #694 from cmacmackin/autocomplete_numeric_usernames
Fix issue with autocompletion of numeric usernames
2 parents 9ff7ee8 + 9c1b8e3 commit 8708bc5

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

_test/types/UserTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,31 @@ public function test_ajax()
8484

8585
$INPUT->set('search', 'test');
8686
$this->assertEquals([], $user->handleAjax());
87+
88+
// Check that numeric usernames are handled properly; PHP's
89+
// strange handling of array keys that look like integers has
90+
// caused bugs with this in the past.
91+
global $auth;
92+
$auth->createUser('12345', 'secret_password', 'Some Person', 'someone@example.com');
93+
$auth->createUser('54321', 'another_password', 'Someone Else', 's.else@example.com');
94+
95+
$user = new User(
96+
[
97+
'autocomplete' => [
98+
'fullname' => true,
99+
'mininput' => 2,
100+
'maxresult' => 5,
101+
],
102+
]
103+
);
104+
105+
$INPUT->set('search', 'Some');
106+
$this->assertEquals(
107+
[
108+
['label' => 'Some Person [12345]', 'value' => '12345'],
109+
['label' => 'Someone Else [54321]', 'value' => '54321']
110+
],
111+
$user->handleAjax()
112+
);
87113
}
88114
}

types/User.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,31 @@ public function handleAjax()
8181
if ($max <= 0) return [];
8282

8383
// find users by login, fill up with names if wanted
84-
$logins = (array)$auth->retrieveUsers(0, $max, ['user' => $lookup]);
84+
// Because a value might be interpreted as integer in the
85+
// array key, we temporarily pad each key with a space at the
86+
// end to enforce string keys.
87+
$pad_keys = function ($logins) {
88+
$result = [];
89+
foreach ($logins as $login => $info) {
90+
$result["$login "] = $info;
91+
}
92+
return $result;
93+
};
94+
$logins = $pad_keys($auth->retrieveUsers(0, $max, ['user' => $lookup]));
8595
if ((count($logins) < $max) && $this->config['autocomplete']['fullname']) {
86-
$logins = array_merge($logins, (array)$auth->retrieveUsers(0, $max, ['name' => $lookup]));
96+
$logins = array_merge(
97+
$logins,
98+
$pad_keys($auth->retrieveUsers(0, $max, ['name' => $lookup]))
99+
);
87100
}
88101

89102
// reformat result for jQuery UI Autocomplete
90103
$users = [];
91104
foreach ($logins as $login => $info) {
105+
$true_login = substr($login, 0, -1);
92106
$users[] = [
93-
'label' => $info['name'] . ' [' . $login . ']',
94-
'value' => $login
107+
'label' => $info['name'] . ' [' . $true_login . ']',
108+
'value' => $true_login
95109
];
96110
}
97111

0 commit comments

Comments
 (0)