From f658a4e04ab47e6256ec6ffe0a0228e41b939ee4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 20 Jul 2026 00:03:53 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(users):=20blank=20search=20q?= =?UTF-8?q?uery=20lists=20the=20whole=20role?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit /users/search?q=&role=student was a 400, but the frontend pickers use exactly that shape as their initial unfiltered listing. A blank q now skips the fragment clause, so blank + role lists everyone in that role (still ordered by username, still paged). Blank q without a role stays a 400 — an unscoped full dump was never a search. --- src/domain/user.rs | 32 ++++++++++++++++++++------------ src/web/users.rs | 17 +++++++++-------- tests/pagination.rs | 24 +++++++++++++++++++++++- 3 files changed, 52 insertions(+), 21 deletions(-) diff --git a/src/domain/user.rs b/src/domain/user.rs index 7321201..75d3439 100644 --- a/src/domain/user.rs +++ b/src/domain/user.rs @@ -303,28 +303,36 @@ impl User { /// Case-insensitive fragment search over username, name, and surname — /// backs the user pickers. `role` narrows to one role (e.g. only students - /// for an enroll picker); `None` searches everyone. Returns every match, - /// ordered by username; the HTTP layer pages the result like any other list - /// (no built-in cap — an over-broad fragment is windowed by `?limit`). + /// for an enroll picker); `None` searches everyone. A blank `query` + /// matches everyone, so blank + `role` is a role-scoped listing. Returns + /// every match, ordered by username; the HTTP layer pages the result like + /// any other list (no built-in cap — an over-broad fragment is windowed + /// by `?limit`). pub async fn search( query: &str, role: Option, db: &Database, ) -> Result, AppError> { let needle = query.trim().to_lowercase(); - let role_clause = if role.is_some() { - "AND role = $role" + let mut clauses = Vec::new(); + if !needle.is_empty() { + clauses.push( + "(string::lowercase(username) CONTAINS $q \ + OR string::lowercase(name ?? '') CONTAINS $q \ + OR string::lowercase(surname ?? '') CONTAINS $q)", + ); + } + if role.is_some() { + clauses.push("role = $role"); + } + let where_clause = if clauses.is_empty() { + "true".to_string() } else { - "" + clauses.join(" AND ") }; let mut query = db .query(format!( - "SELECT * FROM user WHERE \ - (string::lowercase(username) CONTAINS $q \ - OR string::lowercase(name ?? '') CONTAINS $q \ - OR string::lowercase(surname ?? '') CONTAINS $q) \ - {role_clause} \ - ORDER BY username" + "SELECT * FROM user WHERE {where_clause} ORDER BY username" )) .bind(("q", needle)); if let Some(role) = role { diff --git a/src/web/users.rs b/src/web/users.rs index 57f1745..38cbbab 100644 --- a/src/web/users.rs +++ b/src/web/users.rs @@ -133,7 +133,8 @@ async fn apply_preferences( #[derive(Deserialize, IntoParams)] struct SearchUsers { - /// Case-insensitive fragment of a username, name, or surname. + /// Case-insensitive fragment of a username, name, or surname. May be + /// blank when `role` is given — that lists the whole role. q: String, /// Restrict matches to one role: `student`, `teacher`, `manager`, or /// `admin`. Omit to search every role. @@ -148,10 +149,10 @@ struct SearchUsers { /// Find users by username or name — backs the pickers (enroll, grade, mark /// attendance). Requires teacher+. `role` narrows to one role (e.g. -/// `role=student` for an enroll picker). Paged via `?limit=&offset=` like the -/// other lists (omit `limit` for every match); returns a -/// `{items, total, limit, offset}` envelope carrying only id/username/display -/// name — no contact details. +/// `role=student` for an enroll picker); a blank `q` with a `role` lists +/// everyone in that role. Paged via `?limit=&offset=` like the other lists +/// (omit `limit` for every match); returns a `{items, total, limit, offset}` +/// envelope carrying only id/username/display name — no contact details. #[utoipa::path( get, path = "/search", @@ -160,7 +161,7 @@ struct SearchUsers { params(SearchUsers), responses( (status = 200, description = "A page of matching users (all matches when unpaged)", body = Page), - (status = 400, description = "Empty query, unknown role, or invalid limit/offset", body = ErrorResponse), + (status = 400, description = "Blank query without a role, unknown role, or invalid limit/offset", body = ErrorResponse), (status = 401, description = "Not authenticated", body = ErrorResponse), (status = 403, description = "Requires teacher role or higher", body = ErrorResponse), ), @@ -170,10 +171,10 @@ async fn search_users( _teacher: RequireTeacher, Query(req): Query, ) -> Result>, AppError> { - if req.q.trim().is_empty() { + if req.q.trim().is_empty() && req.role.is_none() { return Err(AppError::Validation(ValidationError::Invalid { field: "q", - reason: "must not be empty", + reason: "must not be empty unless role is given", })); } let (limit, offset) = PageParams { diff --git a/tests/pagination.rs b/tests/pagination.rs index aa6bb5c..5979acf 100644 --- a/tests/pagination.rs +++ b/tests/pagination.rs @@ -180,6 +180,28 @@ async fn user_search_is_paged_not_capped() { assert_eq!( res.status, StatusCode::BAD_REQUEST, - "blank q is still a 400" + "blank q without a role is still a 400" ); + + // Blank q scoped to a role is a listing, not an error. + let res = send( + &app, + "GET", + "/users/search?q=&role=student", + Some(&teacher), + None, + ) + .await; + assert_eq!(res.status, StatusCode::OK); + assert_eq!(total(&res.body), 12, "blank q + role lists the whole role"); + let res = send( + &app, + "GET", + "/users/search?q=&role=teacher", + Some(&teacher), + None, + ) + .await; + assert_eq!(res.status, StatusCode::OK); + assert_eq!(total(&res.body), 1, "role scope excludes the students"); }