🔒 admin route guard, sidebar section & role-based dashboard actions
Browse files at this commit436f6d0ef9b2b885047f820bb6dcf6c4c299739aParent: 9f03be4- feat
- 🔒 admin route guard, sidebar section & role-based dashboard actions
4 changed files +40 -3
- src/components/dashboard/role-home-panel.tsx +3 -1
- src/components/layout/side-nav.tsx +15 -2
- src/i18n/messages.ts +3 -0
- src/routes/router.tsx +19 -0
src/components/dashboard/role-home-panel.tsxmodified+3 -1
| @@ -35,11 +35,13 @@ const ROLE_ACTIONS: Record<Role, RoleAction[]> = { | |||
| 35 | { to: "/courses", title: "nav.courses", hint: "courses.subtitle", marker: "▣", tone: "violet" }, | 35 | { to: "/courses", title: "nav.courses", hint: "courses.subtitle", marker: "▣", tone: "violet" }, |
| 36 | { to: "/events", title: "nav.events", hint: "events.subtitle", marker: "◷", tone: "sky" }, | 36 | { to: "/events", title: "nav.events", hint: "events.subtitle", marker: "◷", tone: "sky" }, |
| 37 | { to: "/exams", title: "exams.liveMonitor", hint: "exams.liveMonitorDesc", marker: "◎", tone: "amber" }, | 37 | { to: "/exams", title: "exams.liveMonitor", hint: "exams.liveMonitorDesc", marker: "◎", tone: "amber" }, |
| 38 | + { to: "/marks", title: "dashboard.action.marks", hint: "dashboard.action.marksHint", marker: "▤", tone: "mint" }, | ||
| 38 | ], | 39 | ], |
| 39 | admin: [ | 40 | admin: [ |
| 40 | { to: "/admin/users", title: "nav.users", hint: "admin.subtitle", marker: "◉", tone: "amber" }, | 41 | { to: "/admin/users", title: "nav.users", hint: "admin.subtitle", marker: "◉", tone: "amber" }, |
| 41 | { to: "/courses", title: "nav.courses", hint: "courses.subtitle", marker: "▣", tone: "violet" }, | 42 | { to: "/courses", title: "nav.courses", hint: "courses.subtitle", marker: "▣", tone: "violet" }, |
| 42 | { to: "/events", title: "nav.events", hint: "events.subtitle", marker: "◷", tone: "sky" }, | 43 | { to: "/events", title: "nav.events", hint: "events.subtitle", marker: "◷", tone: "sky" }, |
| 44 | + { to: "/exams", title: "exams.liveMonitor", hint: "exams.liveMonitorDesc", marker: "◎", tone: "rose" }, | ||
| 43 | ], | 45 | ], |
| 44 | }; | 46 | }; |
| 45 | 47 | ||
| @@ -68,7 +70,7 @@ export function RoleHomePanel(props: { role: Role }) { | |||
| 68 | </div> | 70 | </div> |
| 69 | </div> | 71 | </div> |
| 70 | 72 | ||
| 71 | - <div class="grid gap-3 md:grid-cols-3"> | 73 | + <div class="grid gap-3 sm:grid-cols-2 lg:grid-cols-4"> |
| 72 | <For each={actions()}> | 74 | <For each={actions()}> |
| 73 | {(action) => ( | 75 | {(action) => ( |
| 74 | <Link | 76 | <Link |
src/components/layout/side-nav.tsxmodified+15 -2
| @@ -1,5 +1,5 @@ | |||
| 1 | import { Link, useRouterState } from "@tanstack/solid-router"; | 1 | import { Link, useRouterState } from "@tanstack/solid-router"; |
| 2 | -import { For, createMemo, type Component } from "solid-js"; | 2 | +import { For, Show, createMemo, type Component } from "solid-js"; |
| 3 | import { useAuth } from "@/stores/auth-context"; | 3 | import { useAuth } from "@/stores/auth-context"; |
| 4 | import { useT } from "@/stores/preferences-context"; | 4 | import { useT } from "@/stores/preferences-context"; |
| 5 | import { hasMinRole } from "@/lib/roles"; | 5 | import { hasMinRole } from "@/lib/roles"; |
| @@ -96,12 +96,25 @@ export function SideNav(props: { onNavigate?: () => void; collapsed?: boolean }) | |||
| 96 | MAIN_ITEMS.filter((item) => !item.adminOnly || hasMinRole(auth.user()?.role, "admin")), | 96 | MAIN_ITEMS.filter((item) => !item.adminOnly || hasMinRole(auth.user()?.role, "admin")), |
| 97 | ); | 97 | ); |
| 98 | 98 | ||
| 99 | + const t = useT(); | ||
| 100 | + | ||
| 99 | return ( | 101 | return ( |
| 100 | <nav class="flex h-full flex-col" aria-label="Main"> | 102 | <nav class="flex h-full flex-col" aria-label="Main"> |
| 101 | <div class="flex flex-col gap-1 px-2"> | 103 | <div class="flex flex-col gap-1 px-2"> |
| 102 | <For each={items()}> | 104 | <For each={items()}> |
| 103 | {(item) => ( | 105 | {(item) => ( |
| 104 | - <NavLink item={item} collapsed={props.collapsed} onNavigate={props.onNavigate} /> | 106 | + <> |
| 107 | + <Show when={item.adminOnly && !props.collapsed}> | ||
| 108 | + <div class="flex items-center gap-2 px-2 pb-0.5 pt-3"> | ||
| 109 | + <span class="h-px flex-1 bg-border" /> | ||
| 110 | + <span lang="en" class="text-[10px] font-semibold uppercase tracking-[0.15em] text-muted-foreground/60"> | ||
| 111 | + {t("nav.admin")} | ||
| 112 | + </span> | ||
| 113 | + <span class="h-px flex-1 bg-border" /> | ||
| 114 | + </div> | ||
| 115 | + </Show> | ||
| 116 | + <NavLink item={item} collapsed={props.collapsed} onNavigate={props.onNavigate} /> | ||
| 117 | + </> | ||
| 105 | )} | 118 | )} |
| 106 | </For> | 119 | </For> |
| 107 | </div> | 120 | </div> |
src/i18n/messages.tsmodified+3 -0
| @@ -12,6 +12,7 @@ export type MessageKey = | |||
| 12 | | "nav.users" | 12 | | "nav.users" |
| 13 | | "nav.guide" | 13 | | "nav.guide" |
| 14 | | "nav.logout" | 14 | | "nav.logout" |
| 15 | + | "nav.admin" | ||
| 15 | | "nav.menu" | 16 | | "nav.menu" |
| 16 | | "nav.close" | 17 | | "nav.close" |
| 17 | | "nav.collapse" | 18 | | "nav.collapse" |
| @@ -350,6 +351,7 @@ const en: Dict = { | |||
| 350 | "nav.marks": "Report card", | 351 | "nav.marks": "Report card", |
| 351 | "nav.users": "Users", | 352 | "nav.users": "Users", |
| 352 | "nav.guide": "Guide", | 353 | "nav.guide": "Guide", |
| 354 | + "nav.admin": "Admin", | ||
| 353 | "nav.logout": "Log out", | 355 | "nav.logout": "Log out", |
| 354 | "nav.menu": "Menu", | 356 | "nav.menu": "Menu", |
| 355 | "nav.close": "Close", | 357 | "nav.close": "Close", |
| @@ -702,6 +704,7 @@ const tr: Dict = { | |||
| 702 | "nav.marks": "Karnem", | 704 | "nav.marks": "Karnem", |
| 703 | "nav.users": "Kullanıcılar", | 705 | "nav.users": "Kullanıcılar", |
| 704 | "nav.guide": "Rehber", | 706 | "nav.guide": "Rehber", |
| 707 | + "nav.admin": "Yönetim", | ||
| 705 | "nav.logout": "Çıkış yap", | 708 | "nav.logout": "Çıkış yap", |
| 706 | "nav.menu": "Menü", | 709 | "nav.menu": "Menü", |
| 707 | "nav.close": "Kapat", | 710 | "nav.close": "Kapat", |
src/routes/router.tsxmodified+19 -0
| @@ -3,7 +3,12 @@ import { | |||
| 3 | createRootRoute, | 3 | createRootRoute, |
| 4 | createRoute, | 4 | createRoute, |
| 5 | createRouter, | 5 | createRouter, |
| 6 | + isRedirect, | ||
| 7 | + redirect, | ||
| 6 | } from "@tanstack/solid-router"; | 8 | } from "@tanstack/solid-router"; |
| 9 | +import { getMe } from "@/api/getMe"; | ||
| 10 | +import { hasMinRole } from "@/lib/roles"; | ||
| 11 | +import { ApiError } from "@/api/client"; | ||
| 7 | import { Suspense, lazy, type Component } from "solid-js"; | 12 | import { Suspense, lazy, type Component } from "solid-js"; |
| 8 | import { AppShell } from "@/components/layout/app-shell"; | 13 | import { AppShell } from "@/components/layout/app-shell"; |
| 9 | import { PageSpinner } from "@/components/ui/page-spinner"; | 14 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| @@ -143,6 +148,20 @@ const profileRoute = createRoute({ | |||
| 143 | const adminUsersRoute = createRoute({ | 148 | const adminUsersRoute = createRoute({ |
| 144 | getParentRoute: () => rootRoute, | 149 | getParentRoute: () => rootRoute, |
| 145 | path: "/admin/users", | 150 | path: "/admin/users", |
| 151 | + beforeLoad: async () => { | ||
| 152 | + try { | ||
| 153 | + const user = await getMe(); | ||
| 154 | + if (!hasMinRole(user.role, "admin")) { | ||
| 155 | + throw redirect({ to: "/" }); | ||
| 156 | + } | ||
| 157 | + } catch (err) { | ||
| 158 | + if (err instanceof ApiError && err.status === 401) { | ||
| 159 | + throw redirect({ to: "/login" }); | ||
| 160 | + } | ||
| 161 | + if (isRedirect(err)) throw err; | ||
| 162 | + throw redirect({ to: "/login" }); | ||
| 163 | + } | ||
| 164 | + }, | ||
| 146 | component: AdminUsersPage, | 165 | component: AdminUsersPage, |
| 147 | }); | 166 | }); |
| 148 | 167 | ||