From bd8bcfbc37fde840193b11e446ce41e613f778fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Sun, 12 Feb 2023 18:17:23 -0800 Subject: [PATCH] Always trim the username before saving changes in the database Fixes #4742 --- app/Model/UserModel.php | 4 ++++ tests/units/Model/UserModelTest.php | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/app/Model/UserModel.php b/app/Model/UserModel.php index 22d057ced..1e4acf9c0 100644 --- a/app/Model/UserModel.php +++ b/app/Model/UserModel.php @@ -261,6 +261,10 @@ class UserModel extends Base } } + if (isset($values['username'])) { + $values['username'] = trim($values['username']); + } + $this->helper->model->removeFields($values, array('confirmation', 'current_password')); $this->helper->model->resetFields($values, array('is_ldap_user', 'disable_login_form')); $this->helper->model->convertNullFields($values, array('gitlab_id')); diff --git a/tests/units/Model/UserModelTest.php b/tests/units/Model/UserModelTest.php index 5d39cdcc6..d705f0120 100644 --- a/tests/units/Model/UserModelTest.php +++ b/tests/units/Model/UserModelTest.php @@ -394,4 +394,13 @@ class UserModelTest extends Base $project = $projectModel->getById(1); $this->assertEquals(0, $project['is_active']); } + + public function testTrimUsername() + { + $userModel = new UserModel($this->container); + $this->assertNotFalse($userModel->create(array('username' => 'test '))); + + $this->assertNotEmpty($userModel->getByUsername('test')); + $this->assertEmpty($userModel->getByUsername('test ')); + } }