diff --git a/tests/php/test_user_username_filtered.php b/tests/php/test_user_username_filtered.php
new file mode 100644
index 0000000000..dfb04e9a86
--- /dev/null
+++ b/tests/php/test_user_username_filtered.php
@@ -0,0 +1,113 @@
+p = $p; $this->gid = $gid; }
+ function Permission() { return $this->p; }
+ function save($v) { $GLOBALS['saved_perms'][] = array($this->gid, $v['Permission']); $this->p = $v['Permission']; return true; }
+ }
+ class Group {
+ public $id;
+ function __construct($id) { $this->id = $id; }
+ static function find() { return $GLOBALS['groups']; }
+ function Id() { return $this->id; }
+ function Group_Permission($uid) { return new StubPerm('Inherit', $this->id); }
+ function Permissions($n = null) {}
+ }
+ class Monitor {
+ static function find($p = null) { return array(); }
+ }
+ class User {
+ public $f;
+ function __construct($id = 0) {
+ $this->f = $id ? $GLOBALS['users'][$id] : array('Id' => null, 'Username' => '', 'Password' => '');
+ }
+ static function find_one($p) {
+ foreach ($GLOBALS['users'] as $u) {
+ foreach ($p as $k => $v) { if ($u[$k] != $v) continue 2; }
+ $o = new User(); $o->f = $u; return $o;
+ }
+ }
+ function Id() { return $this->f['Id']; }
+ function Username() { return $this->f['Username']; }
+ function changes($new) {
+ $c = array();
+ foreach ($new as $k => $v) {
+ if (($this->f[$k] ?? null) != $v) $c[$k] = $v;
+ }
+ return $c;
+ }
+ function save($c) { $GLOBALS['user_saves'][] = $c; $this->f = array_merge($this->f, $c); if (!$this->f['Id']) $this->f['Id'] = 99; return true; }
+ function get_last_error() { return 'err'; }
+ function Monitor_Permissions($n = null) {}
+ }
+}
+
+namespace {
+ $root = isset($argv[1]) ? rtrim($argv[1], '/') : __DIR__.'/../..';
+ $tmp = sys_get_temp_dir().'/zm_user_edit_save_'.getmypid();
+ @mkdir($tmp.'/includes', 0700, true);
+ file_put_contents($tmp.'/includes/User.php', "getMessage(); }
+ return $notice;
+ }
+
+ $groups = array();
+ $users = array(1 => array('Id' => 1, 'Username' => 'alice', 'Password' => 'x', 'Enabled' => 1),
+ 2 => array('Id' => 2, 'Username' => 'bob', 'Password' => 'x', 'Enabled' => 1));
+ $alice = new ZM\User(1);
+
+ // A new user whose name only differs from an existing one by characters that are filtered out.
+ $n = run_save(array('user' => array('Username' => 'bob!', 'Password' => 'x')), array(), $alice);
+ check('filtered name that exists: no notice', $n, null);
+ check('filtered name that exists: refused', $error_message, 'There already exists a user with this Username
');
+ check('filtered name that exists: nothing saved', $user_saves, array());
+
+ // The saved name is the filtered one.
+ $n = run_save(array('user' => array('Username' => ' carol ', 'Password' => 'x')), array(), $alice);
+ check('filtered name: no notice', $n, null);
+ check('filtered name: saved without the removed characters', $user_saves[0]['Username'] ?? null, 'carolb');
+
+ echo "$passed passed, $failed failed\n";
+ exit($failed ? 1 : 0);
+}
diff --git a/web/includes/actions/user.php b/web/includes/actions/user.php
index 29b606201c..b42acdd8ae 100644
--- a/web/includes/actions/user.php
+++ b/web/includes/actions/user.php
@@ -33,9 +33,11 @@ if ($action == 'Save') {
unset($_REQUEST['redirect']);
return;
}
+ # Save and check the filtered name, not what was typed.
+ $_REQUEST['user']['Username'] = $username;
}
# Need to check for uniqueness of Username
- $user_with_my_username = ZM\User::find_one(array('Username'=>$_REQUEST['user']['Username']));
+ $user_with_my_username = isset($_REQUEST['user']['Username']) ? ZM\User::find_one(array('Username'=>$_REQUEST['user']['Username'])) : null;
if ($user_with_my_username and
( ( $uid and ($user_with_my_username->Id() != $uid) ) or !$uid)
) {