🔒 role-gate user search so students/parents never 403
Browse files at this commitf6ff3605688677f0618a2afaa29cb7834fc0fd9bParent: c087533- fix
- 🔒 role-gate user search so students/parents never 403
3 changed files +49 -10
- src/components/users/user-search-select.tsx +28 -6
- src/i18n/messages.ts +9 -0
- src/pages/whiteboards-page.tsx +12 -4
src/components/users/user-search-select.tsxmodified+28 -6
| @@ -12,6 +12,8 @@ import { | ||
| 12 | 12 | } from "@/components/ui/combobox"; |
| 13 | 13 | import { Label } from "@/components/ui/label"; |
| 14 | 14 | import { personLabelWithId } from "@/lib/person"; |
| 15 | +import { hasMinRole } from "@/lib/roles"; | |
| 16 | +import { useAuth } from "@/stores/auth-context"; | |
| 15 | 17 | import { useT } from "@/stores/preferences-context"; |
| 16 | 18 | |
| 17 | 19 | const MAX_VISIBLE_RESULTS = 8; |
| @@ -33,6 +35,13 @@ export function UserSearchSelect(props: { | ||
| 33 | 35 | role?: Role; |
| 34 | 36 | }) { |
| 35 | 37 | const t = useT(); |
| 38 | + const auth = useAuth(); | |
| 39 | + // `/users/search` is teacher+ on the backend, so this component must never | |
| 40 | + // fire it for a student or parent — they would only ever 403. Below teacher | |
| 41 | + // the input is disabled and the request is never sent; this is the single | |
| 42 | + // role gate for every UserSearchSelect in the app (messages compose, meal | |
| 43 | + // service, course/class management, …). | |
| 44 | + const canSearch = () => hasMinRole(auth.user()?.role, "teacher"); | |
| 36 | 45 | const [query, setQuery] = createSignal(""); |
| 37 | 46 | const [users, setUsers] = createSignal<PersonRef[]>([]); |
| 38 | 47 | const [selected, setSelected] = createSignal<PersonRef | null>(null); |
| @@ -56,9 +65,18 @@ export function UserSearchSelect(props: { | ||
| 56 | 65 | const q = raw.trim(); |
| 57 | 66 | controller?.abort(); |
| 58 | 67 | clearTimeout(timer); |
| 68 | + if (!canSearch()) { | |
| 69 | + // A sub-teacher role reached a picker (e.g. message compose): never call | |
| 70 | + // the teacher+ search endpoint. | |
| 71 | + setUsers([]); | |
| 72 | + setLoading(false); | |
| 73 | + setOpen(false); | |
| 74 | + return; | |
| 75 | + } | |
| 59 | 76 | if (q.length === 0) { |
| 60 | 77 | setUsers([]); |
| 61 | 78 | setLoading(false); |
| 79 | + setOpen(false); | |
| 62 | 80 | return; |
| 63 | 81 | } |
| 64 | 82 | // Mark pending now, not after the debounce fires — otherwise the empty |
| @@ -97,8 +115,8 @@ export function UserSearchSelect(props: { | ||
| 97 | 115 | // input) — allow it, and control open so the panel stays up while |
| 98 | 116 | // loading, then results pop in. |
| 99 | 117 | allowsEmptyCollection |
| 100 | - open={open()} | |
| 101 | - onOpenChange={setOpen} | |
| 118 | + open={open() && query().trim().length > 0} | |
| 119 | + onOpenChange={(nextOpen) => setOpen(nextOpen && query().trim().length > 0)} | |
| 102 | 120 | value={selected()} |
| 103 | 121 | onChange={(user) => { |
| 104 | 122 | setSelected(user); |
| @@ -114,7 +132,7 @@ export function UserSearchSelect(props: { | ||
| 114 | 132 | optionTextValue={(user) => personLabelWithId(user)} |
| 115 | 133 | defaultFilter={() => true} |
| 116 | 134 | placeholder={props.placeholder ?? t("common.searchPlaceholder")} |
| 117 | - disabled={props.disabled} | |
| 135 | + disabled={props.disabled || !canSearch()} | |
| 118 | 136 | itemComponent={(itemProps) => ( |
| 119 | 137 | <ComboboxItem item={itemProps.item}> |
| 120 | 138 | <ComboboxItemLabel>{personLabelWithId(itemProps.item.rawValue)}</ComboboxItemLabel> |
| @@ -125,10 +143,14 @@ export function UserSearchSelect(props: { | ||
| 125 | 143 | <ComboboxInput id={props.id} autocomplete="off" /> |
| 126 | 144 | <ComboboxTrigger /> |
| 127 | 145 | </ComboboxControl> |
| 128 | - <ComboboxContent /> | |
| 146 | + <ComboboxContent> | |
| 147 | + <Show when={emptyText()}> | |
| 148 | + <p class="px-2 py-2 text-xs font-medium text-destructive">{emptyText()}</p> | |
| 149 | + </Show> | |
| 150 | + </ComboboxContent> | |
| 129 | 151 | </Combobox> |
| 130 | - <Show when={emptyText()}> | |
| 131 | - <p class="mt-1 text-xs font-medium text-destructive">{emptyText()}</p> | |
| 152 | + <Show when={!canSearch()}> | |
| 153 | + <p class="text-xs text-muted-foreground">{t("form.searchNoPermission")}</p> | |
| 132 | 154 | </Show> |
| 133 | 155 | </div> |
| 134 | 156 | ); |
src/i18n/messages.tsmodified+9 -0
| @@ -371,6 +371,8 @@ export type MessageKey = | ||
| 371 | 371 | | "notes.title" |
| 372 | 372 | | "notes.subtitle" |
| 373 | 373 | | "notes.new" |
| 374 | + | "notes.write" | |
| 375 | + | "notes.preview" | |
| 374 | 376 | | "notes.empty" |
| 375 | 377 | | "notes.noContent" |
| 376 | 378 | | "notes.files" |
| @@ -712,6 +714,7 @@ export type MessageKey = | ||
| 712 | 714 | | "form.selectStudent" |
| 713 | 715 | | "form.noStudents" |
| 714 | 716 | | "form.noTeachers" |
| 717 | + | "form.searchNoPermission" | |
| 715 | 718 | | "form.titleRequired" |
| 716 | 719 | | "form.titleMax" |
| 717 | 720 | | "form.contentMax" |
| @@ -1636,6 +1639,8 @@ const en: Dict = { | ||
| 1636 | 1639 | "notes.title": "Notebook", |
| 1637 | 1640 | "notes.subtitle": "Your private notebook for class ideas and reminders.", |
| 1638 | 1641 | "notes.new": "New note", |
| 1642 | + "notes.write": "Write", | |
| 1643 | + "notes.preview": "Preview", | |
| 1639 | 1644 | "notes.empty": "Nothing here yet. Write your first note.", |
| 1640 | 1645 | "notes.noContent": "No content", |
| 1641 | 1646 | "notes.files": "Attachments", |
| @@ -2063,6 +2068,7 @@ const en: Dict = { | ||
| 2063 | 2068 | "form.selectStudent": "Select student", |
| 2064 | 2069 | "form.noStudents": "No available students", |
| 2065 | 2070 | "form.noTeachers": "No available teachers", |
| 2071 | + "form.searchNoPermission": "You do not have permission to search users.", | |
| 2066 | 2072 | "form.titleRequired": "Title is required", |
| 2067 | 2073 | "form.titleMax": "Title must be at most 200 characters", |
| 2068 | 2074 | "form.contentMax": "Content must be at most 10 000 characters", |
| @@ -2913,6 +2919,8 @@ const tr: Dict = { | ||
| 2913 | 2919 | "notes.title": "Defter", |
| 2914 | 2920 | "notes.subtitle": "Ders fikirleriniz ve hatırlatmalarınız için özel defteriniz.", |
| 2915 | 2921 | "notes.new": "Yeni not", |
| 2922 | + "notes.write": "Yaz", | |
| 2923 | + "notes.preview": "Önizleme", | |
| 2916 | 2924 | "notes.empty": "Henüz bir şey yok. İlk notunu yaz.", |
| 2917 | 2925 | "notes.noContent": "İçerik yok", |
| 2918 | 2926 | "notes.files": "Ekler", |
| @@ -3340,6 +3348,7 @@ const tr: Dict = { | ||
| 3340 | 3348 | "form.selectStudent": "Öğrenci seç", |
| 3341 | 3349 | "form.noStudents": "Uygun öğrenci yok", |
| 3342 | 3350 | "form.noTeachers": "Uygun öğretmen yok", |
| 3351 | + "form.searchNoPermission": "Kullanıcı aramak için yetkin yok.", | |
| 3343 | 3352 | "form.titleRequired": "Başlık gerekli", |
| 3344 | 3353 | "form.titleMax": "Başlık en fazla 200 karakter olmalı", |
| 3345 | 3354 | "form.contentMax": "İçerik en fazla 10 000 karakter olmalı", |
src/pages/whiteboards-page.tsxmodified+12 -4
| @@ -17,6 +17,7 @@ import { SidePanel } from "@/components/ui/side-panel"; | ||
| 17 | 17 | import { cn } from "@/lib/cn"; |
| 18 | 18 | import { formatDate } from "@/lib/format"; |
| 19 | 19 | import { personLabel } from "@/lib/person"; |
| 20 | +import { hasMinRole } from "@/lib/roles"; | |
| 20 | 21 | import { useAuth } from "@/stores/auth-context"; |
| 21 | 22 | import { usePreferences, useT } from "@/stores/preferences-context"; |
| 22 | 23 | |
| @@ -122,6 +123,7 @@ function CreateBoardPanel(props: { | ||
| 122 | 123 | onCreated: (board: Board) => void; |
| 123 | 124 | }) { |
| 124 | 125 | const t = useT(); |
| 126 | + const auth = useAuth(); | |
| 125 | 127 | const { locale } = usePreferences(); |
| 126 | 128 | const [title, setTitle] = createSignal(""); |
| 127 | 129 | const [query, setQuery] = createSignal(""); |
| @@ -129,9 +131,11 @@ function CreateBoardPanel(props: { | ||
| 129 | 131 | const [pending, setPending] = createSignal(false); |
| 130 | 132 | const [error, setError] = createSignal(""); |
| 131 | 133 | |
| 132 | - // `/users/search` (teacher+) rather than the admin-only `/users` list: a | |
| 133 | - // student or teacher creating a board would otherwise 403 the moment this | |
| 134 | - // panel mounted, before they'd even typed anything. | |
| 134 | + // `/users/search` is teacher+, so a student (boards are student+) can't search | |
| 135 | + // for participants — the picker is disabled for them, and they create a board | |
| 136 | + // for themselves that a teacher can later add people to. Teacher+ uses the | |
| 137 | + // live search below (the admin-only `/users` list would 403 a manager/teacher). | |
| 138 | + const canSearch = () => hasMinRole(auth.user()?.role, "teacher"); | |
| 135 | 139 | const [searchResults, setSearchResults] = createSignal<PersonRef[]>([]); |
| 136 | 140 | let searchController: AbortController | null = null; |
| 137 | 141 | let searchTimer: ReturnType<typeof setTimeout> | undefined; |
| @@ -139,7 +143,7 @@ function CreateBoardPanel(props: { | ||
| 139 | 143 | const q = query().trim(); |
| 140 | 144 | searchController?.abort(); |
| 141 | 145 | clearTimeout(searchTimer); |
| 142 | - if (q.length === 0) { | |
| 146 | + if (!canSearch() || q.length === 0) { | |
| 143 | 147 | setSearchResults([]); |
| 144 | 148 | return; |
| 145 | 149 | } |
| @@ -248,9 +252,13 @@ function CreateBoardPanel(props: { | ||
| 248 | 252 | <Input |
| 249 | 253 | placeholder={t("whiteboard.addParticipant")} |
| 250 | 254 | value={query()} |
| 255 | + disabled={!canSearch()} | |
| 251 | 256 | onInput={(e) => setQuery(e.currentTarget.value)} |
| 252 | 257 | onKeyDown={onPickerKeyDown} |
| 253 | 258 | /> |
| 259 | + <Show when={!canSearch()}> | |
| 260 | + <p class="text-xs text-muted-foreground">{t("form.searchNoPermission")}</p> | |
| 261 | + </Show> | |
| 254 | 262 | <Show when={query().trim() && filtered().length > 0}> |
| 255 | 263 | <div class="rounded-lg border"> |
| 256 | 264 | <For each={filtered()}> |