-
-
Notifications
You must be signed in to change notification settings - Fork 37
feature: add permission control to navbar #221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -9,16 +9,31 @@ import { | |||||
| } from '@/components/ui/dropdown-menu' | ||||||
| import { Sheet, SheetContent, SheetTrigger } from '@/components/ui/sheet' | ||||||
| import { AdminMenus } from '@/config' | ||||||
| import { ChevronDown, ChevronRight, CircleUser, Menu, Package2, Settings, User, LogOut } from 'lucide-react' | ||||||
| import { useCurrentUser } from '@/lib/hooks/useCurrentUser' | ||||||
| import { useGrantedPolicies } from '@/lib/hooks/useGrantedPolicies' | ||||||
| import { USER_ROLE } from '@/lib/utils' | ||||||
| import useSession from '@/useSession' | ||||||
| import { | ||||||
| ChevronDown, | ||||||
| ChevronRight, | ||||||
| CircleUser, | ||||||
| LogOut, | ||||||
| Menu, | ||||||
| Package2, | ||||||
| Settings, | ||||||
| User, | ||||||
| } from 'lucide-react' | ||||||
| import Link from 'next/link' | ||||||
| import { usePathname } from 'next/navigation' | ||||||
| import { useState } from 'react' | ||||||
| import { useMemo, useState } from 'react' | ||||||
| import ClientLink from '../ui/client-link' | ||||||
| import useSession from '@/useSession' | ||||||
|
|
||||||
| export default function SideNavBarMobile() { | ||||||
| const pathname = usePathname() | ||||||
| const sessionData = useSession() | ||||||
| const { can } = useGrantedPolicies() | ||||||
| const currentUser = useCurrentUser() | ||||||
| const isAdmin = currentUser?.roles?.includes(USER_ROLE.ADMIN) ?? false | ||||||
|
|
||||||
| // Initialize expanded menus based on current path | ||||||
| const getInitialExpandedMenus = () => { | ||||||
|
|
@@ -36,6 +51,29 @@ export default function SideNavBarMobile() { | |||||
|
|
||||||
| const [expandedMenus, setExpandedMenus] = useState<Set<string>>(getInitialExpandedMenus()) | ||||||
|
|
||||||
| // Filter menus based on permissions | ||||||
| const visibleMenus = useMemo(() => { | ||||||
| return AdminMenus.filter((menu) => { | ||||||
| // If menu has policy, check permission | ||||||
| if (menu.policy && !can(menu.policy)) { | ||||||
| return false | ||||||
| } | ||||||
|
|
||||||
| // If menu has submenus, check if at least one submenu is visible | ||||||
| if (menu.submenus && menu.submenus.length > 0) { | ||||||
| const visibleSubmenus = menu.submenus.filter((submenu) => { | ||||||
| // If submenu has policy, check permission; otherwise show it | ||||||
| return !submenu.policy || can(submenu.policy) | ||||||
| }) | ||||||
| // Show parent menu only if at least one submenu is visible | ||||||
| return visibleSubmenus.length > 0 | ||||||
| } | ||||||
|
|
||||||
| // Menu without policy or with permission is visible | ||||||
| return true | ||||||
| }) | ||||||
| }, [can]) | ||||||
|
||||||
| }, [can]) | |
| }, [can, AdminMenus]) |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,10 +3,16 @@ import { AdminMenus } from '@/config' | |||||
| import { ChevronDown, ChevronRight, Package2 } from 'lucide-react' | ||||||
| import Link from 'next/link' | ||||||
| import { usePathname } from 'next/navigation' | ||||||
| import { useState } from 'react' | ||||||
| import { useState, useMemo } from 'react' | ||||||
| import { useGrantedPolicies } from '@/lib/hooks/useGrantedPolicies' | ||||||
| import { useCurrentUser } from '@/lib/hooks/useCurrentUser' | ||||||
| import { USER_ROLE } from '@/lib/utils' | ||||||
|
|
||||||
| export default function SideBarMenu() { | ||||||
| const pathname = usePathname() | ||||||
| const { can } = useGrantedPolicies() | ||||||
| const currentUser = useCurrentUser() | ||||||
| const isAdmin = currentUser?.roles?.includes(USER_ROLE.ADMIN) ?? false | ||||||
|
||||||
| const isAdmin = currentUser?.roles?.includes(USER_ROLE.ADMIN) ?? false |
Copilot
AI
Nov 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The useMemo dependency array is incomplete. It should include 'AdminMenus' to recompute when the menu configuration changes. Add AdminMenus to the dependencies: }, [can, AdminMenus])
| }, [can]) | |
| }, [can, AdminMenus]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The isAdmin variable is computed but never used in the component logic. It only appears in the JSX for displaying the admin badge. Consider removing it if the badge is purely cosmetic, or use it in the permission logic if admin users should bypass permission checks.