forge
Explore
Log in

Don't you dare go Hollow

Optional role filter on user search

Browse files at this commit

9ecc81842ddf49b5dc56ca4aad5c00505b4d0637Ahmet Kaan GümüşParent: f17d0ef
  • feat
    • optional role filter on user search
Show full message
✨ feat(users): optional role filter on user search

Pickers want one role at a time — enroll wants students, not the
teacher who shares the name fragment. `role` narrows the match;
omitted keeps the old all-roles behavior. Unknown role is a 400.
src/domain/user.rsmodified+23 -13
@@ -275,21 +275,31 @@ impl User {
275 }275 }
276 276
277 /// Case-insensitive fragment search over username, name, and surname —277 /// Case-insensitive fragment search over username, name, and surname —
278- /// backs the user pickers. Capped at 10 rows: pickers show a short list,278+ /// backs the user pickers. `role` narrows to one role (e.g. only students
279- /// and the cap keeps an over-broad fragment from hauling the whole table.279+ /// for an enroll picker); `None` searches everyone. Capped at 10 rows:
280- pub async fn search(query: &str, db: &Database) -> Result<Vec<User>, AppError> {280+ /// pickers show a short list, and the cap keeps an over-broad fragment
281+ /// from hauling the whole table.
282+ pub async fn search(
283+ query: &str,
284+ role: Option<Role>,
285+ db: &Database,
286+ ) -> Result<Vec<User>, AppError> {
281 let needle = query.trim().to_lowercase();287 let needle = query.trim().to_lowercase();
282- let mut result = db288+ let role_clause = if role.is_some() { "AND role = $role" } else { "" };
283- .query(289+ let mut query = db
290+ .query(format!(
284 "SELECT * FROM user WHERE \291 "SELECT * FROM user WHERE \
285- string::lowercase(username) CONTAINS $q \292+ (string::lowercase(username) CONTAINS $q \
286- OR string::lowercase(name ?? '') CONTAINS $q \293+ OR string::lowercase(name ?? '') CONTAINS $q \
287- OR string::lowercase(surname ?? '') CONTAINS $q \294+ OR string::lowercase(surname ?? '') CONTAINS $q) \
288- ORDER BY username LIMIT 10",295+ {role_clause} \
289- )296+ ORDER BY username LIMIT 10"
290- .bind(("q", needle))297+ ))
291- .await?298+ .bind(("q", needle));
292- .check()?;299+ if let Some(role) = role {
300+ query = query.bind(("role", role));
301+ }
302+ let mut result = query.await?.check()?;
293 Ok(result.take::<Vec<User>>(0)?)303 Ok(result.take::<Vec<User>>(0)?)
294 }304 }
295 305
src/web/users.rsmodified+8 -3
@@ -95,10 +95,14 @@ async fn apply_profile(
95 struct SearchUsers {95 struct SearchUsers {
96 /// Case-insensitive fragment of a username, name, or surname.96 /// Case-insensitive fragment of a username, name, or surname.
97 q: String,97 q: String,
98+ /// Restrict matches to one role: `student`, `teacher`, `manager`, or
99+ /// `admin`. Omit to search every role.
100+ role: Option<String>,
98 }101 }
99 102
100 /// Find users by username or name — backs the pickers (enroll, grade, mark103 /// Find users by username or name — backs the pickers (enroll, grade, mark
101-/// attendance). Requires teacher+. Returns at most 10 matches, and only104+/// attendance). Requires teacher+. `role` narrows to one role (e.g.
105+/// `role=student` for an enroll picker). Returns at most 10 matches, and only
102 /// id/username/display name — no contact details.106 /// id/username/display name — no contact details.
103 #[utoipa::path(107 #[utoipa::path(
104 get,108 get,
@@ -108,7 +112,7 @@ struct SearchUsers {
108 params(SearchUsers),112 params(SearchUsers),
109 responses(113 responses(
110 (status = 200, description = "Matching users, at most 10", body = [PersonRef]),114 (status = 200, description = "Matching users, at most 10", body = [PersonRef]),
111- (status = 400, description = "Empty query", body = ErrorResponse),115+ (status = 400, description = "Empty query or unknown role", body = ErrorResponse),
112 (status = 401, description = "Not authenticated", body = ErrorResponse),116 (status = 401, description = "Not authenticated", body = ErrorResponse),
113 (status = 403, description = "Requires teacher role or higher", body = ErrorResponse),117 (status = 403, description = "Requires teacher role or higher", body = ErrorResponse),
114 ),118 ),
@@ -124,7 +128,8 @@ async fn search_users(
124 reason: "must not be empty",128 reason: "must not be empty",
125 }));129 }));
126 }130 }
127- let users = User::search(&req.q, &st.db).await?;131+ let role = req.role.as_deref().map(Role::try_from_str).transpose()?;
132+ let users = User::search(&req.q, role, &st.db).await?;
128 Ok(Json(users.iter().map(PersonRef::new).collect()))133 Ok(Json(users.iter().map(PersonRef::new).collect()))
129 }134 }
130 135