diff --git a/src/app/(app)/tickets/all/page.tsx b/src/app/(app)/tickets/all/page.tsx index aace297..4015c3e 100644 --- a/src/app/(app)/tickets/all/page.tsx +++ b/src/app/(app)/tickets/all/page.tsx @@ -12,7 +12,7 @@ export default function AllTicketsPage() { fetch('/api/auth/me') .then(r => r.json()) .then(u => { - if (u.user?.role !== 'admin') { + if (!u.user?.permissions?.includes('*')) { router.replace('/tickets/pending') } else { setReady(true) diff --git a/src/components/layout/Sidebar.tsx b/src/components/layout/Sidebar.tsx index 7e70e45..647e880 100644 --- a/src/components/layout/Sidebar.tsx +++ b/src/components/layout/Sidebar.tsx @@ -5,42 +5,53 @@ import { usePathname } from 'next/navigation' import { LayoutDashboard, FileText, Settings, Users, Shield, Key, Clock, CheckCircle, PlusSquare, Upload, List } from 'lucide-react' const navItems = [ - { href: '/dashboard', label: '仪表盘', icon: LayoutDashboard }, - { href: '/tickets/pending', label: '待办工单', icon: Clock }, - { href: '/tickets/completed', label: '已办工单', icon: CheckCircle }, - { href: '/tickets/create', label: '手动建单', icon: PlusSquare }, - { href: '/tickets/import', label: '导入工单', icon: Upload }, - { href: '/reports', label: '报告管理', icon: FileText }, + { href: '/dashboard', label: '仪表盘', icon: LayoutDashboard, perm: null }, + { href: '/tickets/pending', label: '待办工单', icon: Clock, perm: 'tickets:read' }, + { href: '/tickets/completed', label: '已办工单', icon: CheckCircle, perm: 'tickets:read' }, + { href: '/tickets/create', label: '手动建单', icon: PlusSquare, perm: 'tickets:create' }, + { href: '/tickets/import', label: '导入工单', icon: Upload, perm: 'tickets:import' }, + { href: '/reports', label: '报告管理', icon: FileText, perm: 'reports:read' }, ] const settingsItems = [ - { href: '/settings/users', label: '用户管理', icon: Users }, - { href: '/settings/roles', label: '角色权限', icon: Shield }, - { href: '/settings/api-keys', label: 'API Key', icon: Key }, + { href: '/settings/users', label: '用户管理', icon: Users, perm: 'users:read' }, + { href: '/settings/roles', label: '角色权限', icon: Shield, perm: 'roles:read' }, + { href: '/settings/api-keys', label: 'API Key', icon: Key, perm: 'api-keys:read' }, ] +function hasAnyAdminPerm(permissions: string[]): boolean { + return permissions.includes('*') || permissions.some(p => p.startsWith('users:') || p.startsWith('roles:') || p.startsWith('api-keys:')) +} + export default function Sidebar() { const pathname = usePathname() - const [isAdmin, setIsAdmin] = useState(false) + const [permissions, setPermissions] = useState([]) useEffect(() => { fetch('/api/auth/me') .then(r => r.json()) - .then(u => { if (u.user?.role === 'admin') setIsAdmin(true) }) + .then(u => { if (u.user?.permissions) setPermissions(u.user.permissions) }) .catch(() => {}) }, []) + + const canSee = (perm: string | null) => { + if (perm === null) return true + if (permissions.includes('*')) return true + return permissions.includes(perm) + } + return (