forge
Explore
Log in

Don't you dare go Hollow

🔒 role-gate user search so students/parents never 403

Browse files at this commit

f6ff3605688677f0618a2afaa29cb7834fc0fd9bBurak BoduroğluParent: c087533
  • fix
    • 🔒 role-gate user search so students/parents never 403
Show full message
fix: 🔒 role-gate user search so students/parents never 403

/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.tsxmodified+28 -6
@@ -12,6 +12,8 @@ import {
1212 } from "@/components/ui/combobox";
1313 import { Label } from "@/components/ui/label";
1414 import { personLabelWithId } from "@/lib/person";
15+import { hasMinRole } from "@/lib/roles";
16+import { useAuth } from "@/stores/auth-context";
1517 import { useT } from "@/stores/preferences-context";
1618
1719 const MAX_VISIBLE_RESULTS = 8;
@@ -33,6 +35,13 @@ export function UserSearchSelect(props: {
3335 role?: Role;
3436 }) {
3537 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");
3645 const [query, setQuery] = createSignal("");
3746 const [users, setUsers] = createSignal<PersonRef[]>([]);
3847 const [selected, setSelected] = createSignal<PersonRef | null>(null);
@@ -56,9 +65,18 @@ export function UserSearchSelect(props: {
5665 const q = raw.trim();
5766 controller?.abort();
5867 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+ }
5976 if (q.length === 0) {
6077 setUsers([]);
6178 setLoading(false);
79+ setOpen(false);
6280 return;
6381 }
6482 // Mark pending now, not after the debounce fires — otherwise the empty
@@ -97,8 +115,8 @@ export function UserSearchSelect(props: {
97115 // input) — allow it, and control open so the panel stays up while
98116 // loading, then results pop in.
99117 allowsEmptyCollection
100- open={open()}
101- onOpenChange={setOpen}
118+ open={open() && query().trim().length > 0}
119+ onOpenChange={(nextOpen) => setOpen(nextOpen && query().trim().length > 0)}
102120 value={selected()}
103121 onChange={(user) => {
104122 setSelected(user);
@@ -114,7 +132,7 @@ export function UserSearchSelect(props: {
114132 optionTextValue={(user) => personLabelWithId(user)}
115133 defaultFilter={() => true}
116134 placeholder={props.placeholder ?? t("common.searchPlaceholder")}
117- disabled={props.disabled}
135+ disabled={props.disabled || !canSearch()}
118136 itemComponent={(itemProps) => (
119137 <ComboboxItem item={itemProps.item}>
120138 <ComboboxItemLabel>{personLabelWithId(itemProps.item.rawValue)}</ComboboxItemLabel>
@@ -125,10 +143,14 @@ export function UserSearchSelect(props: {
125143 <ComboboxInput id={props.id} autocomplete="off" />
126144 <ComboboxTrigger />
127145 </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>
129151 </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>
132154 </Show>
133155 </div>
134156 );
src/i18n/messages.tsmodified+9 -0
@@ -371,6 +371,8 @@ export type MessageKey =
371371 | "notes.title"
372372 | "notes.subtitle"
373373 | "notes.new"
374+ | "notes.write"
375+ | "notes.preview"
374376 | "notes.empty"
375377 | "notes.noContent"
376378 | "notes.files"
@@ -712,6 +714,7 @@ export type MessageKey =
712714 | "form.selectStudent"
713715 | "form.noStudents"
714716 | "form.noTeachers"
717+ | "form.searchNoPermission"
715718 | "form.titleRequired"
716719 | "form.titleMax"
717720 | "form.contentMax"
@@ -1636,6 +1639,8 @@ const en: Dict = {
16361639 "notes.title": "Notebook",
16371640 "notes.subtitle": "Your private notebook for class ideas and reminders.",
16381641 "notes.new": "New note",
1642+ "notes.write": "Write",
1643+ "notes.preview": "Preview",
16391644 "notes.empty": "Nothing here yet. Write your first note.",
16401645 "notes.noContent": "No content",
16411646 "notes.files": "Attachments",
@@ -2063,6 +2068,7 @@ const en: Dict = {
20632068 "form.selectStudent": "Select student",
20642069 "form.noStudents": "No available students",
20652070 "form.noTeachers": "No available teachers",
2071+ "form.searchNoPermission": "You do not have permission to search users.",
20662072 "form.titleRequired": "Title is required",
20672073 "form.titleMax": "Title must be at most 200 characters",
20682074 "form.contentMax": "Content must be at most 10 000 characters",
@@ -2913,6 +2919,8 @@ const tr: Dict = {
29132919 "notes.title": "Defter",
29142920 "notes.subtitle": "Ders fikirleriniz ve hatırlatmalarınız için özel defteriniz.",
29152921 "notes.new": "Yeni not",
2922+ "notes.write": "Yaz",
2923+ "notes.preview": "Önizleme",
29162924 "notes.empty": "Henüz bir şey yok. İlk notunu yaz.",
29172925 "notes.noContent": "İçerik yok",
29182926 "notes.files": "Ekler",
@@ -3340,6 +3348,7 @@ const tr: Dict = {
33403348 "form.selectStudent": "Öğrenci seç",
33413349 "form.noStudents": "Uygun öğrenci yok",
33423350 "form.noTeachers": "Uygun öğretmen yok",
3351+ "form.searchNoPermission": "Kullanıcı aramak için yetkin yok.",
33433352 "form.titleRequired": "Başlık gerekli",
33443353 "form.titleMax": "Başlık en fazla 200 karakter olmalı",
33453354 "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";
1717 import { cn } from "@/lib/cn";
1818 import { formatDate } from "@/lib/format";
1919 import { personLabel } from "@/lib/person";
20+import { hasMinRole } from "@/lib/roles";
2021 import { useAuth } from "@/stores/auth-context";
2122 import { usePreferences, useT } from "@/stores/preferences-context";
2223
@@ -122,6 +123,7 @@ function CreateBoardPanel(props: {
122123 onCreated: (board: Board) => void;
123124 }) {
124125 const t = useT();
126+ const auth = useAuth();
125127 const { locale } = usePreferences();
126128 const [title, setTitle] = createSignal("");
127129 const [query, setQuery] = createSignal("");
@@ -129,9 +131,11 @@ function CreateBoardPanel(props: {
129131 const [pending, setPending] = createSignal(false);
130132 const [error, setError] = createSignal("");
131133
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");
135139 const [searchResults, setSearchResults] = createSignal<PersonRef[]>([]);
136140 let searchController: AbortController | null = null;
137141 let searchTimer: ReturnType<typeof setTimeout> | undefined;
@@ -139,7 +143,7 @@ function CreateBoardPanel(props: {
139143 const q = query().trim();
140144 searchController?.abort();
141145 clearTimeout(searchTimer);
142- if (q.length === 0) {
146+ if (!canSearch() || q.length === 0) {
143147 setSearchResults([]);
144148 return;
145149 }
@@ -248,9 +252,13 @@ function CreateBoardPanel(props: {
248252 <Input
249253 placeholder={t("whiteboard.addParticipant")}
250254 value={query()}
255+ disabled={!canSearch()}
251256 onInput={(e) => setQuery(e.currentTarget.value)}
252257 onKeyDown={onPickerKeyDown}
253258 />
259+ <Show when={!canSearch()}>
260+ <p class="text-xs text-muted-foreground">{t("form.searchNoPermission")}</p>
261+ </Show>
254262 <Show when={query().trim() && filtered().length > 0}>
255263 <div class="rounded-lg border">
256264 <For each={filtered()}>