Skip to content

Commit 47a697e

Browse files
committed
Handle the case where there are multiple users with the same username, but with a different casing.
1 parent 7012fd3 commit 47a697e

File tree

1 file changed

+20
-4
lines changed
  • crates/storage-pg/src/user

1 file changed

+20
-4
lines changed

crates/storage-pg/src/user/mod.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ impl UserRepository for PgUserRepository<'_> {
165165
err,
166166
)]
167167
async fn find_by_username(&mut self, username: &str) -> Result<Option<User>, Self::Error> {
168+
// We may have multiple users with the same username, but with a different
169+
// casing. In this case, we want to return the one which matches the exact
170+
// casing
168171
let res = sqlx::query_as!(
169172
UserLookup,
170173
r#"
@@ -180,12 +183,25 @@ impl UserRepository for PgUserRepository<'_> {
180183
username,
181184
)
182185
.traced()
183-
.fetch_optional(&mut *self.conn)
186+
.fetch_all(&mut *self.conn)
184187
.await?;
185188

186-
let Some(res) = res else { return Ok(None) };
187-
188-
Ok(Some(res.into()))
189+
match &res[..] {
190+
// Happy path: there is only one user matching the username…
191+
[user] => Ok(Some(user.clone().into())),
192+
// …or none.
193+
[] => Ok(None),
194+
list => {
195+
// If there are multiple users with the same username, we want to
196+
// return the one which matches the exact casing
197+
if let Some(user) = list.iter().find(|user| user.username == username) {
198+
Ok(Some(user.clone().into()))
199+
} else {
200+
// If none match exactly, we prefer to return nothing
201+
Ok(None)
202+
}
203+
}
204+
}
189205
}
190206

191207
#[tracing::instrument(

0 commit comments

Comments
 (0)