diff --git a/src/domain/user.rs b/src/domain/user.rs index 46db600..eeca062 100644 --- a/src/domain/user.rs +++ b/src/domain/user.rs @@ -275,21 +275,31 @@ impl User { } /// Case-insensitive fragment search over username, name, and surname — - /// backs the user pickers. Capped at 10 rows: pickers show a short list, - /// and the cap keeps an over-broad fragment from hauling the whole table. - pub async fn search(query: &str, db: &Database) -> Result, AppError> { + /// backs the user pickers. `role` narrows to one role (e.g. only students + /// for an enroll picker); `None` searches everyone. Capped at 10 rows: + /// pickers show a short list, and the cap keeps an over-broad fragment + /// from hauling the whole table. + pub async fn search( + query: &str, + role: Option, + db: &Database, + ) -> Result, AppError> { let needle = query.trim().to_lowercase(); - let mut result = db - .query( + let role_clause = if role.is_some() { "AND role = $role" } else { "" }; + 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 \ - ORDER BY username LIMIT 10", - ) - .bind(("q", needle)) - .await? - .check()?; + (string::lowercase(username) CONTAINS $q \ + OR string::lowercase(name ?? '') CONTAINS $q \ + OR string::lowercase(surname ?? '') CONTAINS $q) \ + {role_clause} \ + ORDER BY username LIMIT 10" + )) + .bind(("q", needle)); + if let Some(role) = role { + query = query.bind(("role", role)); + } + let mut result = query.await?.check()?; Ok(result.take::>(0)?) } diff --git a/src/web/users.rs b/src/web/users.rs index e456cbe..26b3d7c 100644 --- a/src/web/users.rs +++ b/src/web/users.rs @@ -95,10 +95,14 @@ async fn apply_profile( struct SearchUsers { /// Case-insensitive fragment of a username, name, or surname. q: String, + /// Restrict matches to one role: `student`, `teacher`, `manager`, or + /// `admin`. Omit to search every role. + role: Option, } /// Find users by username or name — backs the pickers (enroll, grade, mark -/// attendance). Requires teacher+. Returns at most 10 matches, and only +/// attendance). Requires teacher+. `role` narrows to one role (e.g. +/// `role=student` for an enroll picker). Returns at most 10 matches, and only /// id/username/display name — no contact details. #[utoipa::path( get, @@ -108,7 +112,7 @@ struct SearchUsers { params(SearchUsers), responses( (status = 200, description = "Matching users, at most 10", body = [PersonRef]), - (status = 400, description = "Empty query", body = ErrorResponse), + (status = 400, description = "Empty query or unknown role", body = ErrorResponse), (status = 401, description = "Not authenticated", body = ErrorResponse), (status = 403, description = "Requires teacher role or higher", body = ErrorResponse), ), @@ -124,7 +128,8 @@ async fn search_users( reason: "must not be empty", })); } - let users = User::search(&req.q, &st.db).await?; + let role = req.role.as_deref().map(Role::try_from_str).transpose()?; + let users = User::search(&req.q, role, &st.db).await?; Ok(Json(users.iter().map(PersonRef::new).collect())) }