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