🐛 exclude students from staff-work search, add role-band guards for work pages
Browse files at this commit52be1aee0821aec55d1b8d4cbe39bbb4cf186091Parent: a6bada1- fix
- 🐛 exclude students from staff-work search, add role-band guards for work pages
10 changed files +53 -18
- src/api/getUserSearch.ts +2 -2
- src/components/layout/route-guard.tsx +4 -2
- src/components/layout/side-nav.tsx +5 -3
- src/components/users/user-search-select.tsx +1 -1
- src/i18n/messages.ts +9 -3
- src/lib/roles.ts +17 -0
- src/pages/dashboard-page.tsx +8 -1
- src/pages/staff-work-page.tsx +4 -3
- src/pages/work-log-page.tsx +1 -1
- src/routes/router.tsx +2 -2
src/api/getUserSearch.tsmodified+2 -2
| @@ -5,11 +5,11 @@ import type { PersonRef, Role } from "./types"; | |||
| 5 | export async function getUserSearch( | 5 | export async function getUserSearch( |
| 6 | query: string, | 6 | query: string, |
| 7 | signal?: AbortSignal, | 7 | signal?: AbortSignal, |
| 8 | - role?: Role, | 8 | + role?: Role | Role[], |
| 9 | page?: PageParams, | 9 | page?: PageParams, |
| 10 | ): Promise<Page<PersonRef>> { | 10 | ): Promise<Page<PersonRef>> { |
| 11 | const params = new URLSearchParams({ q: query }); | 11 | const params = new URLSearchParams({ q: query }); |
| 12 | - if (role) params.set("role", role); | 12 | + if (role) params.set("role", Array.isArray(role) ? role.join(",") : role); |
| 13 | appendPageParams(params, page); | 13 | appendPageParams(params, page); |
| 14 | const data = await client<unknown>(`/users/search?${params.toString()}`, { signal }); | 14 | const data = await client<unknown>(`/users/search?${params.toString()}`, { signal }); |
| 15 | return normalizePage<PersonRef>(data); | 15 | return normalizePage<PersonRef>(data); |
src/components/layout/route-guard.tsxmodified+4 -2
| @@ -3,13 +3,15 @@ import { type ParentProps, Show } from "solid-js"; | |||
| 3 | import type { Role } from "@/api/types"; | 3 | import type { Role } from "@/api/types"; |
| 4 | import { Alert } from "@/components/ui/alert"; | 4 | import { Alert } from "@/components/ui/alert"; |
| 5 | import { PageSpinner } from "@/components/ui/page-spinner"; | 5 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| 6 | -import { hasExactRole, hasMinRole } from "@/lib/roles"; | 6 | +import { hasExactRole, roleInRange } from "@/lib/roles"; |
| 7 | import { useAuth } from "@/stores/auth-context"; | 7 | import { useAuth } from "@/stores/auth-context"; |
| 8 | import { useT } from "@/stores/preferences-context"; | 8 | import { useT } from "@/stores/preferences-context"; |
| 9 | 9 | ||
| 10 | export function RouteGuard( | 10 | export function RouteGuard( |
| 11 | props: ParentProps<{ | 11 | props: ParentProps<{ |
| 12 | minRole?: Role; | 12 | minRole?: Role; |
| 13 | + /** Inclusive upper bound (e.g. maxRole: "manager" excludes admin). */ | ||
| 14 | + maxRole?: Role; | ||
| 13 | /** When set, only this exact role may enter (e.g. student-only pages). */ | 15 | /** When set, only this exact role may enter (e.g. student-only pages). */ |
| 14 | exactRole?: Role; | 16 | exactRole?: Role; |
| 15 | }>, | 17 | }>, |
| @@ -19,7 +21,7 @@ export function RouteGuard( | |||
| 19 | 21 | ||
| 20 | const allowed = (role: Role) => { | 22 | const allowed = (role: Role) => { |
| 21 | if (props.exactRole) return hasExactRole(role, props.exactRole); | 23 | if (props.exactRole) return hasExactRole(role, props.exactRole); |
| 22 | - if (props.minRole) return hasMinRole(role, props.minRole); | 24 | + if (props.minRole || props.maxRole) return roleInRange(role, props.minRole, props.maxRole); |
| 23 | return true; | 25 | return true; |
| 24 | }; | 26 | }; |
| 25 | 27 | ||
src/components/layout/side-nav.tsxmodified+5 -3
| @@ -8,7 +8,7 @@ import { | |||
| 8 | } from "@/components/ui/dropdown-menu"; | 8 | } from "@/components/ui/dropdown-menu"; |
| 9 | import { useAuth } from "@/stores/auth-context"; | 9 | import { useAuth } from "@/stores/auth-context"; |
| 10 | import { useT } from "@/stores/preferences-context"; | 10 | import { useT } from "@/stores/preferences-context"; |
| 11 | -import { hasExactRole, hasMinRole } from "@/lib/roles"; | 11 | +import { hasExactRole, hasMinRole, roleInRange } from "@/lib/roles"; |
| 12 | import { cn } from "@/lib/cn"; | 12 | import { cn } from "@/lib/cn"; |
| 13 | import type { MessageKey } from "@/i18n/messages"; | 13 | import type { MessageKey } from "@/i18n/messages"; |
| 14 | import type { Role } from "@/api/types"; | 14 | import type { Role } from "@/api/types"; |
| @@ -34,6 +34,8 @@ type NavItem = { | |||
| 34 | labelKey: MessageKey; | 34 | labelKey: MessageKey; |
| 35 | Icon: Component<{ class?: string }>; | 35 | Icon: Component<{ class?: string }>; |
| 36 | minRole?: Role; | 36 | minRole?: Role; |
| 37 | + /** Inclusive upper bound (e.g. hide from admin with maxRole: "manager"). */ | ||
| 38 | + maxRole?: Role; | ||
| 37 | /** When set, only this exact role sees the item. */ | 39 | /** When set, only this exact role sees the item. */ |
| 38 | exactRole?: Role; | 40 | exactRole?: Role; |
| 39 | exact?: boolean; | 41 | exact?: boolean; |
| @@ -83,7 +85,7 @@ const NAV_GROUPS: NavGroup[] = [ | |||
| 83 | items: [ | 85 | items: [ |
| 84 | { to: "/management/student-marks", labelKey: "nav.studentMarks", Icon: IconChart, minRole: "teacher" }, | 86 | { to: "/management/student-marks", labelKey: "nav.studentMarks", Icon: IconChart, minRole: "teacher" }, |
| 85 | { to: "/management/student-attendance", labelKey: "nav.studentAttendance", Icon: IconClipboardCheck, minRole: "teacher" }, | 87 | { to: "/management/student-attendance", labelKey: "nav.studentAttendance", Icon: IconClipboardCheck, minRole: "teacher" }, |
| 86 | - { to: "/work", labelKey: "nav.work", Icon: IconReportAnalytics, minRole: "teacher" }, | 88 | + { to: "/work", labelKey: "nav.work", Icon: IconReportAnalytics, minRole: "teacher", maxRole: "manager" }, |
| 87 | { to: "/management/staff-work", labelKey: "nav.staffWork", Icon: IconReportAnalytics, minRole: "manager" }, | 89 | { to: "/management/staff-work", labelKey: "nav.staffWork", Icon: IconReportAnalytics, minRole: "manager" }, |
| 88 | ], | 90 | ], |
| 89 | }, | 91 | }, |
| @@ -161,7 +163,7 @@ export function SideNav(props: { onNavigate?: () => void; collapsed?: boolean }) | |||
| 161 | const itemVisible = (item: NavItem) => { | 163 | const itemVisible = (item: NavItem) => { |
| 162 | const role = auth.user()?.role; | 164 | const role = auth.user()?.role; |
| 163 | if (item.exactRole) return hasExactRole(role, item.exactRole); | 165 | if (item.exactRole) return hasExactRole(role, item.exactRole); |
| 164 | - if (item.minRole) return hasMinRole(role, item.minRole); | 166 | + if (item.minRole || item.maxRole) return roleInRange(role, item.minRole, item.maxRole); |
| 165 | return true; | 167 | return true; |
| 166 | }; | 168 | }; |
| 167 | 169 | ||
src/components/users/user-search-select.tsxmodified+1 -1
| @@ -20,7 +20,7 @@ export function UserSearchSelect(props: { | |||
| 20 | emptyMessage?: string; | 20 | emptyMessage?: string; |
| 21 | disabled?: boolean; | 21 | disabled?: boolean; |
| 22 | allowManualValue?: boolean; | 22 | allowManualValue?: boolean; |
| 23 | - role?: Role; | 23 | + role?: Role | Role[]; |
| 24 | }) { | 24 | }) { |
| 25 | let inputRef: HTMLInputElement | undefined; | 25 | let inputRef: HTMLInputElement | undefined; |
| 26 | const t = useT(); | 26 | const t = useT(); |
src/i18n/messages.tsmodified+9 -3
| @@ -494,7 +494,9 @@ export type MessageKey = | |||
| 494 | | "work.correctHelp" | 494 | | "work.correctHelp" |
| 495 | | "work.cannotEditOpen" | 495 | | "work.cannotEditOpen" |
| 496 | | "work.timesRequired" | 496 | | "work.timesRequired" |
| 497 | - | "work.deleteSummary"; | 497 | + | "work.deleteSummary" |
| 498 | + | "work.teacherIdentity" | ||
| 499 | + | "work.noTeachers"; | ||
| 498 | 500 | ||
| 499 | type Dict = Record<MessageKey, string>; | 501 | type Dict = Record<MessageKey, string>; |
| 500 | 502 | ||
| @@ -998,7 +1000,9 @@ const en: Dict = { | |||
| 998 | "work.closed": "Closed", | 1000 | "work.closed": "Closed", |
| 999 | "work.status": "Status", | 1001 | "work.status": "Status", |
| 1000 | "work.staffTitle": "Staff work logs", | 1002 | "work.staffTitle": "Staff work logs", |
| 1001 | - "work.staffSubtitle": "Look up a staff member, correct closed stints, or delete entries.", | 1003 | + "work.staffSubtitle": "Look up a teacher, correct closed stints, or delete entries.", |
| 1004 | + "work.teacherIdentity": "Teacher", | ||
| 1005 | + "work.noTeachers": "No teachers found.", | ||
| 1002 | "work.show": "Show log", | 1006 | "work.show": "Show log", |
| 1003 | "work.forUser": "Log for {user}", | 1007 | "work.forUser": "Log for {user}", |
| 1004 | "work.correct": "Correct entry", | 1008 | "work.correct": "Correct entry", |
| @@ -1508,7 +1512,9 @@ const tr: Dict = { | |||
| 1508 | "work.closed": "Kapalı", | 1512 | "work.closed": "Kapalı", |
| 1509 | "work.status": "Durum", | 1513 | "work.status": "Durum", |
| 1510 | "work.staffTitle": "Personel mesai kayıtları", | 1514 | "work.staffTitle": "Personel mesai kayıtları", |
| 1511 | - "work.staffSubtitle": "Personel ara, kapalı mesaileri düzelt veya kayıt sil.", | 1515 | + "work.staffSubtitle": "Öğretmen ara, kapalı mesaileri düzelt veya kayıt sil.", |
| 1516 | + "work.teacherIdentity": "Öğretmen", | ||
| 1517 | + "work.noTeachers": "Öğretmen bulunamadı.", | ||
| 1512 | "work.show": "Kaydı göster", | 1518 | "work.show": "Kaydı göster", |
| 1513 | "work.forUser": "{user} için kayıtlar", | 1519 | "work.forUser": "{user} için kayıtlar", |
| 1514 | "work.correct": "Kaydı düzelt", | 1520 | "work.correct": "Kaydı düzelt", |
src/lib/roles.tsmodified+17 -0
| @@ -12,8 +12,25 @@ export function hasMinRole(role: Role | undefined | null, min: Role): boolean { | |||
| 12 | return RANK[role] >= RANK[min]; | 12 | return RANK[role] >= RANK[min]; |
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | +export function hasMaxRole(role: Role | undefined | null, max: Role): boolean { | ||
| 16 | + if (!role) return false; | ||
| 17 | + return RANK[role] <= RANK[max]; | ||
| 18 | +} | ||
| 19 | + | ||
| 15 | export function hasExactRole(role: Role | undefined | null, exact: Role): boolean { | 20 | export function hasExactRole(role: Role | undefined | null, exact: Role): boolean { |
| 16 | return role === exact; | 21 | return role === exact; |
| 17 | } | 22 | } |
| 18 | 23 | ||
| 24 | +/** Inclusive role band: minRole <= role <= maxRole. */ | ||
| 25 | +export function roleInRange( | ||
| 26 | + role: Role | undefined | null, | ||
| 27 | + min?: Role, | ||
| 28 | + max?: Role, | ||
| 29 | +): boolean { | ||
| 30 | + if (!role) return false; | ||
| 31 | + if (min && !hasMinRole(role, min)) return false; | ||
| 32 | + if (max && !hasMaxRole(role, max)) return false; | ||
| 33 | + return true; | ||
| 34 | +} | ||
| 35 | + | ||
| 19 | export const ROLES: Role[] = ["student", "teacher", "manager", "admin"]; | 36 | export const ROLES: Role[] = ["student", "teacher", "manager", "admin"]; |
src/pages/dashboard-page.tsxmodified+8 -1
| @@ -206,12 +206,19 @@ function DashboardContent() { | |||
| 206 | { icon: <IconExam class="h-5 w-5" />, titleKey: "nav.exams", to: "/exams", descKey: "dashboard.portal.examsDesc", stat: ec, statSuffix: "dashboard.records", accent: "amber" }, | 206 | { icon: <IconExam class="h-5 w-5" />, titleKey: "nav.exams", to: "/exams", descKey: "dashboard.portal.examsDesc", stat: ec, statSuffix: "dashboard.records", accent: "amber" }, |
| 207 | { icon: <IconCalendar class="h-5 w-5" />, titleKey: "nav.events", to: "/events", descKey: "dashboard.portal.eventsDesc", stat: evc, statSuffix: "dashboard.records", accent: "sky" }, | 207 | { icon: <IconCalendar class="h-5 w-5" />, titleKey: "nav.events", to: "/events", descKey: "dashboard.portal.eventsDesc", stat: evc, statSuffix: "dashboard.records", accent: "sky" }, |
| 208 | { icon: <IconNote class="h-5 w-5" />, titleKey: "nav.notes", to: "/notes", descKey: "dashboard.portal.notesDesc", stat: nc, statSuffix: "dashboard.records", accent: "sky" }, | 208 | { icon: <IconNote class="h-5 w-5" />, titleKey: "nav.notes", to: "/notes", descKey: "dashboard.portal.notesDesc", stat: nc, statSuffix: "dashboard.records", accent: "sky" }, |
| 209 | - { icon: <IconReportAnalytics class="h-5 w-5" />, titleKey: "nav.work", to: "/work", descKey: "dashboard.portal.workDesc", stat: "", accent: "amber", minRole: "teacher" }, | ||
| 210 | { icon: <IconChart class="h-5 w-5" />, titleKey: "nav.studentMarks", to: "/management/student-marks", descKey: "dashboard.portal.studentMarksDesc", stat: "", accent: "mint", minRole: "teacher" }, | 209 | { icon: <IconChart class="h-5 w-5" />, titleKey: "nav.studentMarks", to: "/management/student-marks", descKey: "dashboard.portal.studentMarksDesc", stat: "", accent: "mint", minRole: "teacher" }, |
| 211 | { icon: <IconClipboardCheck class="h-5 w-5" />, titleKey: "nav.studentAttendance", to: "/management/student-attendance", descKey: "dashboard.portal.attendanceDesc", stat: "", accent: "rose", minRole: "teacher" }, | 210 | { icon: <IconClipboardCheck class="h-5 w-5" />, titleKey: "nav.studentAttendance", to: "/management/student-attendance", descKey: "dashboard.portal.attendanceDesc", stat: "", accent: "rose", minRole: "teacher" }, |
| 212 | { icon: <IconSettings class="h-5 w-5" />, titleKey: "nav.settings", to: "/management/settings", descKey: "dashboard.portal.settingsDesc", stat: "", accent: "mint", minRole: "manager" }, | 211 | { icon: <IconSettings class="h-5 w-5" />, titleKey: "nav.settings", to: "/management/settings", descKey: "dashboard.portal.settingsDesc", stat: "", accent: "mint", minRole: "manager" }, |
| 213 | { icon: <IconBook class="h-5 w-5" />, titleKey: "nav.terms", to: "/management/terms", descKey: "dashboard.portal.termsDesc", stat: "", accent: "rose", minRole: "manager" }, | 212 | { icon: <IconBook class="h-5 w-5" />, titleKey: "nav.terms", to: "/management/terms", descKey: "dashboard.portal.termsDesc", stat: "", accent: "rose", minRole: "manager" }, |
| 214 | ); | 213 | ); |
| 214 | + if (roleVal === "manager") { | ||
| 215 | + list.push( | ||
| 216 | + { icon: <IconReportAnalytics class="h-5 w-5" />, titleKey: "nav.work", to: "/work", descKey: "dashboard.portal.workDesc", stat: "", accent: "amber", minRole: "teacher" }, | ||
| 217 | + ); | ||
| 218 | + } | ||
| 219 | + list.push( | ||
| 220 | + { icon: <IconReportAnalytics class="h-5 w-5" />, titleKey: "nav.staffWork", to: "/management/staff-work", descKey: "dashboard.portal.workDesc", stat: "", accent: "amber", minRole: "manager" }, | ||
| 221 | + ); | ||
| 215 | if (roleVal === "admin") { | 222 | if (roleVal === "admin") { |
| 216 | list.push( | 223 | list.push( |
| 217 | { icon: <IconUsers class="h-5 w-5" />, titleKey: "nav.users", to: "/admin/users", descKey: "dashboard.portal.usersDesc", stat: "", accent: "rose", minRole: "admin" }, | 224 | { icon: <IconUsers class="h-5 w-5" />, titleKey: "nav.users", to: "/admin/users", descKey: "dashboard.portal.usersDesc", stat: "", accent: "rose", minRole: "admin" }, |
src/pages/staff-work-page.tsxmodified+4 -3
| @@ -166,14 +166,15 @@ function StaffWorkContent() { | |||
| 166 | > | 166 | > |
| 167 | <UserSearchSelect | 167 | <UserSearchSelect |
| 168 | id="staff-work-user" | 168 | id="staff-work-user" |
| 169 | - label={t("marks.userIdentity")} | 169 | + label={t("work.teacherIdentity")} |
| 170 | value={userId()} | 170 | value={userId()} |
| 171 | onChange={setUserId} | 171 | onChange={setUserId} |
| 172 | onSelectUser={setSelectedUser} | 172 | onSelectUser={setSelectedUser} |
| 173 | placeholder={t("common.searchPlaceholder")} | 173 | placeholder={t("common.searchPlaceholder")} |
| 174 | - selectPlaceholder={t("marks.userIdentity")} | 174 | + selectPlaceholder={t("work.teacherIdentity")} |
| 175 | - emptyMessage={t("admin.noUsers")} | 175 | + emptyMessage={t("work.noTeachers")} |
| 176 | allowManualValue | 176 | allowManualValue |
| 177 | + role={["teacher", "manager", "admin"]} | ||
| 177 | /> | 178 | /> |
| 178 | <Button type="submit" class="h-9 w-full rounded-lg sm:w-auto"> | 179 | <Button type="submit" class="h-9 w-full rounded-lg sm:w-auto"> |
| 179 | {t("work.show")} | 180 | {t("work.show")} |
src/pages/work-log-page.tsxmodified+1 -1
| @@ -21,7 +21,7 @@ const WORK_PAGE_SIZE = 15; | |||
| 21 | 21 | ||
| 22 | export default function WorkLogPage() { | 22 | export default function WorkLogPage() { |
| 23 | return ( | 23 | return ( |
| 24 | - <RouteGuard minRole="teacher"> | 24 | + <RouteGuard minRole="teacher" maxRole="manager"> |
| 25 | <WorkLogContent /> | 25 | <WorkLogContent /> |
| 26 | </RouteGuard> | 26 | </RouteGuard> |
| 27 | ); | 27 | ); |
src/routes/router.tsxmodified+2 -2
| @@ -7,7 +7,7 @@ import { | |||
| 7 | redirect, | 7 | redirect, |
| 8 | } from "@tanstack/solid-router"; | 8 | } from "@tanstack/solid-router"; |
| 9 | import { getMe } from "@/api/getMe"; | 9 | import { getMe } from "@/api/getMe"; |
| 10 | -import { hasMinRole } from "@/lib/roles"; | 10 | +import { hasMinRole, roleInRange } from "@/lib/roles"; |
| 11 | import { ApiError } from "@/api/client"; | 11 | import { ApiError } from "@/api/client"; |
| 12 | import { Suspense, lazy, type Component } from "solid-js"; | 12 | import { Suspense, lazy, type Component } from "solid-js"; |
| 13 | import { AppShell } from "@/components/layout/app-shell"; | 13 | import { AppShell } from "@/components/layout/app-shell"; |
| @@ -222,7 +222,7 @@ const workRoute = createRoute({ | |||
| 222 | beforeLoad: async () => { | 222 | beforeLoad: async () => { |
| 223 | try { | 223 | try { |
| 224 | const user = await getMe(); | 224 | const user = await getMe(); |
| 225 | - if (!hasMinRole(user.role, "teacher")) throw redirect({ to: "/" }); | 225 | + if (!roleInRange(user.role, "teacher", "manager")) throw redirect({ to: "/" }); |
| 226 | } catch (err) { | 226 | } catch (err) { |
| 227 | if (err instanceof ApiError && err.status === 401) throw redirect({ to: "/login" }); | 227 | if (err instanceof ApiError && err.status === 401) throw redirect({ to: "/login" }); |
| 228 | if (isRedirect(err)) throw err; | 228 | if (isRedirect(err)) throw err; |