mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 22:56:27 +00:00
Add RBAC menu and role management
Adds permission, menu, and role administration with hierarchical access controls, plus admin breadcrumbs and a redirect from the legacy user-role page. Remove `credentials.md` before committing; it contains server credentials.
This commit is contained in:
@@ -0,0 +1,322 @@
|
||||
'use client';
|
||||
|
||||
import React, { FC, useState, useMemo } from 'react';
|
||||
import { Checkbox, Input } from 'antd';
|
||||
import { ChevronDown, ChevronRight, Search, Shield, ShieldCheck } from 'lucide-react';
|
||||
import { type IMenu } from './type';
|
||||
import RbacBadge from '@/components/ui/rbac-badge';
|
||||
import { Button } from '@/components/ui/button';
|
||||
|
||||
export interface IMenuPermissionTreeProps {
|
||||
menus: IMenu[];
|
||||
allowAdminPanel: boolean;
|
||||
// Map of menu_id -> array of selected action names
|
||||
selectedPermissions: Record<number, string[]>;
|
||||
onPermissionToggle: (menuId: number, actionName: string, checked: boolean) => void;
|
||||
onMenuSelectAll: (menuId: number, allActions: string[]) => void;
|
||||
onMenuClearAll: (menuId: number) => void;
|
||||
}
|
||||
|
||||
interface TreeNode extends IMenu {
|
||||
children?: TreeNode[];
|
||||
level: number;
|
||||
}
|
||||
|
||||
// Transform flat list to tree
|
||||
function buildTree(items: IMenu[], parentId: number | null = null, level: number = 0): TreeNode[] {
|
||||
const result: TreeNode[] = [];
|
||||
const directChildren = items.filter(i => {
|
||||
if (parentId === null) {
|
||||
return i.menu_parent_id === null || i.menu_parent_id === undefined;
|
||||
}
|
||||
return i.menu_parent_id === parentId;
|
||||
});
|
||||
|
||||
for (const item of directChildren) {
|
||||
if (item.id === undefined) continue;
|
||||
const children = buildTree(items, item.id, level + 1);
|
||||
const node: TreeNode = {
|
||||
...item,
|
||||
level,
|
||||
children: children.length > 0 ? children : undefined,
|
||||
};
|
||||
result.push(node);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export const MenuPermissionTree: FC<IMenuPermissionTreeProps> = ({
|
||||
menus,
|
||||
allowAdminPanel,
|
||||
selectedPermissions,
|
||||
onPermissionToggle,
|
||||
onMenuSelectAll,
|
||||
onMenuClearAll,
|
||||
}) => {
|
||||
const [searchQuery, setSearchQuery] = useState('');
|
||||
const [collapsedIds, setCollapsedIds] = useState<Set<number>>(new Set());
|
||||
|
||||
const toggleCollapse = (id: number) => {
|
||||
setCollapsedIds(prev => {
|
||||
const next = new Set(prev);
|
||||
if (next.has(id)) {
|
||||
next.delete(id);
|
||||
} else {
|
||||
next.add(id);
|
||||
}
|
||||
return next;
|
||||
});
|
||||
};
|
||||
|
||||
// Separate Non-Admin and Admin menus
|
||||
const { nonAdminTree, adminTree } = useMemo(() => {
|
||||
const nonAdminList = menus.filter(m => !m.is_admin_menu);
|
||||
const adminList = menus.filter(m => Boolean(m.is_admin_menu));
|
||||
|
||||
return {
|
||||
nonAdminTree: buildTree(nonAdminList),
|
||||
adminTree: buildTree(adminList),
|
||||
};
|
||||
}, [menus]);
|
||||
|
||||
// Filter matching IDs when searching
|
||||
const searchMatchIds = useMemo(() => {
|
||||
if (!searchQuery.trim()) return null;
|
||||
const lower = searchQuery.toLowerCase();
|
||||
const matches = new Set<number>();
|
||||
|
||||
menus.forEach(m => {
|
||||
const matchName = m.name?.toLowerCase().includes(lower);
|
||||
const matchPath = m.path_url?.toLowerCase().includes(lower);
|
||||
const matchActions = m.action?.some(a => a.toLowerCase().includes(lower));
|
||||
|
||||
if (matchName || matchPath || matchActions) {
|
||||
if (m.id !== undefined) matches.add(m.id);
|
||||
// Also add parent IDs so parent stays visible
|
||||
let parentId = m.menu_parent_id;
|
||||
while (parentId) {
|
||||
matches.add(parentId);
|
||||
const parent = menus.find(p => p.id === parentId);
|
||||
parentId = parent?.menu_parent_id;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return matches;
|
||||
}, [menus, searchQuery]);
|
||||
|
||||
// Renders a single menu row and recursively its children
|
||||
const renderNode = (node: TreeNode) => {
|
||||
if (node.id === undefined) return null;
|
||||
if (searchMatchIds && !searchMatchIds.has(node.id)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const isCollapsed = collapsedIds.has(node.id);
|
||||
const hasChildren = node.children && node.children.length > 0;
|
||||
const assignedActions = node.action || [];
|
||||
const currentSelectedActions = selectedPermissions[node.id] || [];
|
||||
const isAllSelected = assignedActions.length > 0 && assignedActions.every(a => currentSelectedActions.includes(a));
|
||||
const hasAnySelected = currentSelectedActions.length > 0;
|
||||
const isIndeterminate = hasAnySelected && !isAllSelected;
|
||||
|
||||
return (
|
||||
<React.Fragment key={node.id}>
|
||||
<div
|
||||
className={`flex flex-col sm:flex-row sm:items-center justify-between py-3 px-3 sm:px-4 border-b border-zinc-200 dark:border-zinc-800 transition-colors ${node.level === 0
|
||||
? 'bg-zinc-50/70 dark:bg-zinc-900/60 font-semibold'
|
||||
: 'bg-white dark:bg-zinc-950/40 hover:bg-zinc-50/50 dark:hover:bg-zinc-900/30'
|
||||
}`}
|
||||
>
|
||||
{/* Left: Menu Name, Hierarchy Indent & Parent indicator */}
|
||||
<div
|
||||
className="flex items-center gap-2 sm:gap-3 flex-1 min-w-0 mb-2 sm:mb-0"
|
||||
style={{ paddingLeft: `${node.level * 24}px` }}
|
||||
>
|
||||
{hasChildren ? (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => toggleCollapse(node.id!)}
|
||||
className="w-5 h-5 flex items-center justify-center text-zinc-400 hover:text-zinc-700 dark:hover:text-zinc-200 rounded"
|
||||
>
|
||||
{isCollapsed ? <ChevronRight size={15} /> : <ChevronDown size={15} />}
|
||||
</button>
|
||||
) : (
|
||||
<span className="w-5" />
|
||||
)}
|
||||
|
||||
{node.level > 0 && (
|
||||
<span className="text-zinc-400 dark:text-zinc-600 font-mono text-xs select-none">
|
||||
↳
|
||||
</span>
|
||||
)}
|
||||
|
||||
<div className="flex items-center gap-2 min-w-0">
|
||||
{assignedActions.length > 0 && (
|
||||
<Checkbox
|
||||
checked={isAllSelected}
|
||||
indeterminate={isIndeterminate}
|
||||
onChange={(e) => {
|
||||
if (e.target.checked) {
|
||||
onMenuSelectAll(node.id!, assignedActions);
|
||||
} else {
|
||||
onMenuClearAll(node.id!);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
<div>
|
||||
<div className="flex items-center gap-2">
|
||||
{node.icon && <i className={`${node.icon} text-sm text-zinc-500`} />}
|
||||
<span className="text-xs sm:text-sm font-medium text-zinc-900 dark:text-zinc-100 truncate">
|
||||
{node.name}
|
||||
</span>
|
||||
{node.is_admin_menu && (
|
||||
<span className="text-[10px] uppercase font-semibold px-1.5 py-0.2 rounded bg-indigo-500/10 text-indigo-600 dark:text-indigo-400">
|
||||
Admin
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<span className="text-[11px] font-mono text-zinc-400 dark:text-zinc-500 block truncate">
|
||||
{node.path_url || '#'}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Right: Available Actions Checkboxes & Row Select All button */}
|
||||
<div className="flex items-center flex-wrap gap-2 sm:gap-3 sm:ml-4">
|
||||
{assignedActions.length > 0 ? (
|
||||
<>
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
{assignedActions.map(action => {
|
||||
const isChecked = currentSelectedActions.includes(action);
|
||||
return (
|
||||
<label
|
||||
key={action}
|
||||
className={`inline-flex items-center gap-1.5 px-2 py-1 rounded border text-xs cursor-pointer select-none transition-all ${isChecked
|
||||
? 'bg-zinc-100 dark:bg-zinc-800 border-zinc-300 dark:border-zinc-700 shadow-xs'
|
||||
: 'bg-transparent border-transparent hover:border-zinc-200 dark:hover:border-zinc-800 opacity-65 hover:opacity-100'
|
||||
}`}
|
||||
>
|
||||
<Checkbox
|
||||
checked={isChecked}
|
||||
onChange={(e) =>
|
||||
onPermissionToggle(node.id!, action, e.target.checked)
|
||||
}
|
||||
/>
|
||||
<RbacBadge permission={action} size="sm" />
|
||||
</label>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="h-7 text-xs px-2.5 ml-2"
|
||||
onClick={() => {
|
||||
if (isAllSelected) {
|
||||
onMenuClearAll(node.id!);
|
||||
} else {
|
||||
onMenuSelectAll(node.id!, assignedActions);
|
||||
}
|
||||
}}
|
||||
>
|
||||
{isAllSelected ? 'Batal' : 'Semua'}
|
||||
</Button>
|
||||
</>
|
||||
) : (
|
||||
<span className="text-xs text-zinc-400 italic px-2">
|
||||
Tidak ada aksi terkonfigurasi
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Render children recursively if not collapsed */}
|
||||
{hasChildren && !isCollapsed && node.children!.map(child => renderNode(child))}
|
||||
</React.Fragment>
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="flex flex-col space-y-6">
|
||||
{/* Search Bar */}
|
||||
<div className="flex items-center justify-between gap-4">
|
||||
<div className="w-full sm:w-72">
|
||||
<Input
|
||||
prefix={<Search size={14} className="text-muted-foreground mr-1" />}
|
||||
placeholder="Cari menu atau permission..."
|
||||
value={searchQuery}
|
||||
onChange={(e) => setSearchQuery(e.target.value)}
|
||||
allowClear
|
||||
className="h-9"
|
||||
/>
|
||||
</div>
|
||||
<div className="hidden sm:flex items-center gap-2 text-xs text-muted-foreground">
|
||||
<span>Gunakan tombol</span>
|
||||
<span className="px-1.5 py-0.5 rounded bg-zinc-100 dark:bg-zinc-800 font-mono text-[11px] font-medium">Semua</span>
|
||||
<span>atau centang permission per menu</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Section 1: Non-Admin Menus */}
|
||||
<div className="border border-zinc-200 dark:border-zinc-800 rounded-lg overflow-hidden bg-white dark:bg-zinc-950">
|
||||
<div className="py-3 px-4 bg-zinc-100/80 dark:bg-zinc-900 border-b border-zinc-200 dark:border-zinc-800 flex items-center justify-between">
|
||||
<div className="flex items-center gap-2">
|
||||
<Shield size={16} className="text-his" />
|
||||
<h4 className="text-sm font-semibold text-zinc-900 dark:text-zinc-100 m-0">
|
||||
Non-Admin Menus (General Application)
|
||||
</h4>
|
||||
</div>
|
||||
<span className="text-xs text-muted-foreground font-medium">
|
||||
{menus.filter(m => !m.is_admin_menu).length} menu
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="divide-y divide-zinc-200 dark:divide-zinc-800">
|
||||
{nonAdminTree.length > 0 ? (
|
||||
nonAdminTree.map(node => renderNode(node))
|
||||
) : (
|
||||
<div className="py-8 text-center text-xs text-muted-foreground">
|
||||
Belum ada menu Non-Admin yang terdaftar.
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Section 2: Admin Menus (Rendered ONLY if allowAdminPanel === true) */}
|
||||
{allowAdminPanel && (
|
||||
<div className="border border-zinc-200 dark:border-zinc-800 rounded-lg overflow-hidden bg-white dark:bg-zinc-950">
|
||||
<div className="py-3 px-4 bg-zinc-100/80 dark:bg-zinc-900 border-b border-zinc-200 dark:border-zinc-800 flex items-center justify-between">
|
||||
<div className="flex items-center gap-2">
|
||||
<ShieldCheck size={16} className="text-red-600 dark:text-red-400" />
|
||||
<h4 className="text-sm font-semibold text-zinc-900 dark:text-zinc-100 m-0">
|
||||
Admin Menus (Admin Panel)
|
||||
</h4>
|
||||
</div>
|
||||
<span className="text-xs text-muted-foreground font-medium">
|
||||
{menus.filter(m => Boolean(m.is_admin_menu)).length} menu
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="divide-y divide-zinc-200 dark:divide-zinc-800">
|
||||
{adminTree.length > 0 ? (
|
||||
adminTree.map(node => renderNode(node))
|
||||
) : (
|
||||
<div className="py-8 text-center text-xs text-muted-foreground">
|
||||
Belum ada menu Admin Panel yang terdaftar.
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default MenuPermissionTree;
|
||||
|
||||
Reference in New Issue
Block a user