feat: add role-based access control
This commit is contained in:
parent
c42b8e2904
commit
8ab9a9a497
21 changed files with 629 additions and 46 deletions
|
|
@ -15,6 +15,7 @@ import {
|
|||
LayoutGrid,
|
||||
MenuIcon,
|
||||
Server,
|
||||
Users,
|
||||
Wrench,
|
||||
} from "lucide-react";
|
||||
import { useEffect, useState } from "react";
|
||||
|
|
@ -25,30 +26,42 @@ const dashboardLinks = [
|
|||
label: "Overview",
|
||||
description: "Health, alerts, and recent checks",
|
||||
icon: LayoutGrid,
|
||||
permission: "dashboard.read",
|
||||
},
|
||||
{
|
||||
to: "/dashboard/servers",
|
||||
label: "Servers",
|
||||
description: "Inventory and host details",
|
||||
icon: Server,
|
||||
permission: "servers.read",
|
||||
},
|
||||
{
|
||||
to: "/dashboard/services",
|
||||
label: "Services",
|
||||
description: "Processes and uptime targets",
|
||||
icon: Wrench,
|
||||
permission: "services.read",
|
||||
},
|
||||
{
|
||||
to: "/dashboard/domains",
|
||||
label: "Domains",
|
||||
description: "DNS, SSL, and expiry status",
|
||||
icon: Globe,
|
||||
permission: "domains.read",
|
||||
},
|
||||
{
|
||||
to: "/dashboard/notes",
|
||||
label: "Notes",
|
||||
description: "Runbooks and operational handover",
|
||||
icon: BookText,
|
||||
permission: "notes.read",
|
||||
},
|
||||
{
|
||||
to: "/dashboard/users",
|
||||
label: "Users",
|
||||
description: "Roles and permission assignments",
|
||||
icon: Users,
|
||||
permission: "users.read",
|
||||
},
|
||||
] as const;
|
||||
|
||||
|
|
@ -60,14 +73,18 @@ function isActive(pathname: string, to: string): boolean {
|
|||
|
||||
function NavItems({
|
||||
pathname,
|
||||
permissions,
|
||||
onNavigate,
|
||||
}: {
|
||||
pathname: string;
|
||||
permissions: string[];
|
||||
onNavigate?: () => void;
|
||||
}) {
|
||||
const allowedPermissions = new Set(permissions);
|
||||
|
||||
return (
|
||||
<nav className="grid gap-1 p-2" aria-label="Dashboard sections">
|
||||
{dashboardLinks.map(({ to, label, description, icon: Icon }) => {
|
||||
{dashboardLinks.filter((link) => allowedPermissions.has(link.permission)).map(({ to, label, description, icon: Icon }) => {
|
||||
const active = isActive(pathname, to);
|
||||
return (
|
||||
<Link
|
||||
|
|
@ -97,15 +114,18 @@ function NavItems({
|
|||
export function DashboardShell({
|
||||
userName,
|
||||
userEmail,
|
||||
permissions,
|
||||
}: {
|
||||
userName?: string | null;
|
||||
userEmail?: string | null;
|
||||
permissions: string[];
|
||||
}) {
|
||||
const pathname = useRouterState({
|
||||
select: (state) => state.location.pathname,
|
||||
});
|
||||
|
||||
const [mobileOpen, setMobileOpen] = useState(false);
|
||||
const allowedPermissions = new Set(permissions);
|
||||
|
||||
useEffect(() => {
|
||||
setMobileOpen(false);
|
||||
|
|
@ -139,6 +159,7 @@ export function DashboardShell({
|
|||
</SheetHeader>
|
||||
<NavItems
|
||||
pathname={pathname}
|
||||
permissions={permissions}
|
||||
onNavigate={() => setMobileOpen(false)}
|
||||
/>
|
||||
</SheetContent>
|
||||
|
|
@ -171,14 +192,18 @@ export function DashboardShell({
|
|||
</p>
|
||||
</div>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<Link to="/dashboard/servers/new">
|
||||
<Button size="sm">Add server</Button>
|
||||
</Link>
|
||||
<Link to="/dashboard/notes/new">
|
||||
<Button size="sm" variant="outline">
|
||||
New note
|
||||
</Button>
|
||||
</Link>
|
||||
{allowedPermissions.has("servers.create") ? (
|
||||
<Link to="/dashboard/servers/new">
|
||||
<Button size="sm">Add server</Button>
|
||||
</Link>
|
||||
) : null}
|
||||
{allowedPermissions.has("notes.create") ? (
|
||||
<Link to="/dashboard/notes/new">
|
||||
<Button size="sm" variant="outline">
|
||||
New note
|
||||
</Button>
|
||||
</Link>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -192,7 +217,7 @@ export function DashboardShell({
|
|||
Navigation
|
||||
</div>
|
||||
</div>
|
||||
<NavItems pathname={pathname} />
|
||||
<NavItems pathname={pathname} permissions={permissions} />
|
||||
</aside>
|
||||
|
||||
<main className="min-w-0">
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue