From f6ff3605688677f0618a2afaa29cb7834fc0fd9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Boduro=C4=9Flu?= <80620802+burakboduroglu@users.noreply.github.com> Date: Sun, 2 Aug 2026 23:42:32 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=94=92=20role-gate=20user=20search?= =?UTF-8?q?=20so=20students/parents=20never=20403?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit /users/search is teacher+ on the backend, but the picker that calls it is reachable by every role — most visibly the message-compose recipient field (messages is open to all roles) and the whiteboard participant picker (boards are student+). A student or parent typing there hit a guaranteed 403. - UserSearchSelect now checks the viewer's role once, centrally: below teacher it never calls the endpoint, disables the input, and shows a "no permission to search" hint. This covers every call site (compose, meal service, course/class management, …) in one place. - whiteboards create panel uses its own inline search, so it gets the same teacher+ gate: a student creates a board for themselves and a teacher can add participants later. - new form.searchNoPermission string (en/tr). Note: this commit also carries in-flight edits that share these two files from a parallel change — the combobox open-on-input behavior in user-search-select and the notes.write/notes.preview i18n keys — since they could not be separated from the same files. --- src/components/users/user-search-select.tsx | 34 +++++++++++++++++---- src/i18n/messages.ts | 9 ++++++ src/pages/whiteboards-page.tsx | 16 +++++++--- 3 files changed, 49 insertions(+), 10 deletions(-) diff --git a/src/components/users/user-search-select.tsx b/src/components/users/user-search-select.tsx index 28106d2..f12ef6b 100644 --- a/src/components/users/user-search-select.tsx +++ b/src/components/users/user-search-select.tsx @@ -12,6 +12,8 @@ import { } from "@/components/ui/combobox"; import { Label } from "@/components/ui/label"; import { personLabelWithId } from "@/lib/person"; +import { hasMinRole } from "@/lib/roles"; +import { useAuth } from "@/stores/auth-context"; import { useT } from "@/stores/preferences-context"; const MAX_VISIBLE_RESULTS = 8; @@ -33,6 +35,13 @@ export function UserSearchSelect(props: { role?: Role; }) { const t = useT(); + const auth = useAuth(); + // `/users/search` is teacher+ on the backend, so this component must never + // fire it for a student or parent — they would only ever 403. Below teacher + // the input is disabled and the request is never sent; this is the single + // role gate for every UserSearchSelect in the app (messages compose, meal + // service, course/class management, …). + const canSearch = () => hasMinRole(auth.user()?.role, "teacher"); const [query, setQuery] = createSignal(""); const [users, setUsers] = createSignal([]); const [selected, setSelected] = createSignal(null); @@ -56,9 +65,18 @@ export function UserSearchSelect(props: { const q = raw.trim(); controller?.abort(); clearTimeout(timer); + if (!canSearch()) { + // A sub-teacher role reached a picker (e.g. message compose): never call + // the teacher+ search endpoint. + setUsers([]); + setLoading(false); + setOpen(false); + return; + } if (q.length === 0) { setUsers([]); setLoading(false); + setOpen(false); return; } // Mark pending now, not after the debounce fires — otherwise the empty @@ -97,8 +115,8 @@ export function UserSearchSelect(props: { // input) — allow it, and control open so the panel stays up while // loading, then results pop in. allowsEmptyCollection - open={open()} - onOpenChange={setOpen} + open={open() && query().trim().length > 0} + onOpenChange={(nextOpen) => setOpen(nextOpen && query().trim().length > 0)} value={selected()} onChange={(user) => { setSelected(user); @@ -114,7 +132,7 @@ export function UserSearchSelect(props: { optionTextValue={(user) => personLabelWithId(user)} defaultFilter={() => true} placeholder={props.placeholder ?? t("common.searchPlaceholder")} - disabled={props.disabled} + disabled={props.disabled || !canSearch()} itemComponent={(itemProps) => ( {personLabelWithId(itemProps.item.rawValue)} @@ -125,10 +143,14 @@ export function UserSearchSelect(props: { - + + +

{emptyText()}

+
+
- -

{emptyText()}

+ +

{t("form.searchNoPermission")}

); diff --git a/src/i18n/messages.ts b/src/i18n/messages.ts index 55b9e79..fe36bde 100644 --- a/src/i18n/messages.ts +++ b/src/i18n/messages.ts @@ -371,6 +371,8 @@ export type MessageKey = | "notes.title" | "notes.subtitle" | "notes.new" + | "notes.write" + | "notes.preview" | "notes.empty" | "notes.noContent" | "notes.files" @@ -712,6 +714,7 @@ export type MessageKey = | "form.selectStudent" | "form.noStudents" | "form.noTeachers" + | "form.searchNoPermission" | "form.titleRequired" | "form.titleMax" | "form.contentMax" @@ -1636,6 +1639,8 @@ const en: Dict = { "notes.title": "Notebook", "notes.subtitle": "Your private notebook for class ideas and reminders.", "notes.new": "New note", + "notes.write": "Write", + "notes.preview": "Preview", "notes.empty": "Nothing here yet. Write your first note.", "notes.noContent": "No content", "notes.files": "Attachments", @@ -2063,6 +2068,7 @@ const en: Dict = { "form.selectStudent": "Select student", "form.noStudents": "No available students", "form.noTeachers": "No available teachers", + "form.searchNoPermission": "You do not have permission to search users.", "form.titleRequired": "Title is required", "form.titleMax": "Title must be at most 200 characters", "form.contentMax": "Content must be at most 10 000 characters", @@ -2913,6 +2919,8 @@ const tr: Dict = { "notes.title": "Defter", "notes.subtitle": "Ders fikirleriniz ve hatırlatmalarınız için özel defteriniz.", "notes.new": "Yeni not", + "notes.write": "Yaz", + "notes.preview": "Önizleme", "notes.empty": "Henüz bir şey yok. İlk notunu yaz.", "notes.noContent": "İçerik yok", "notes.files": "Ekler", @@ -3340,6 +3348,7 @@ const tr: Dict = { "form.selectStudent": "Öğrenci seç", "form.noStudents": "Uygun öğrenci yok", "form.noTeachers": "Uygun öğretmen yok", + "form.searchNoPermission": "Kullanıcı aramak için yetkin yok.", "form.titleRequired": "Başlık gerekli", "form.titleMax": "Başlık en fazla 200 karakter olmalı", "form.contentMax": "İçerik en fazla 10 000 karakter olmalı", diff --git a/src/pages/whiteboards-page.tsx b/src/pages/whiteboards-page.tsx index 1359f89..2aa0a31 100644 --- a/src/pages/whiteboards-page.tsx +++ b/src/pages/whiteboards-page.tsx @@ -17,6 +17,7 @@ import { SidePanel } from "@/components/ui/side-panel"; import { cn } from "@/lib/cn"; import { formatDate } from "@/lib/format"; import { personLabel } from "@/lib/person"; +import { hasMinRole } from "@/lib/roles"; import { useAuth } from "@/stores/auth-context"; import { usePreferences, useT } from "@/stores/preferences-context"; @@ -122,6 +123,7 @@ function CreateBoardPanel(props: { onCreated: (board: Board) => void; }) { const t = useT(); + const auth = useAuth(); const { locale } = usePreferences(); const [title, setTitle] = createSignal(""); const [query, setQuery] = createSignal(""); @@ -129,9 +131,11 @@ function CreateBoardPanel(props: { const [pending, setPending] = createSignal(false); const [error, setError] = createSignal(""); - // `/users/search` (teacher+) rather than the admin-only `/users` list: a - // student or teacher creating a board would otherwise 403 the moment this - // panel mounted, before they'd even typed anything. + // `/users/search` is teacher+, so a student (boards are student+) can't search + // for participants — the picker is disabled for them, and they create a board + // for themselves that a teacher can later add people to. Teacher+ uses the + // live search below (the admin-only `/users` list would 403 a manager/teacher). + const canSearch = () => hasMinRole(auth.user()?.role, "teacher"); const [searchResults, setSearchResults] = createSignal([]); let searchController: AbortController | null = null; let searchTimer: ReturnType | undefined; @@ -139,7 +143,7 @@ function CreateBoardPanel(props: { const q = query().trim(); searchController?.abort(); clearTimeout(searchTimer); - if (q.length === 0) { + if (!canSearch() || q.length === 0) { setSearchResults([]); return; } @@ -248,9 +252,13 @@ function CreateBoardPanel(props: { setQuery(e.currentTarget.value)} onKeyDown={onPickerKeyDown} /> + +

{t("form.searchNoPermission")}

+
0}>