mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 23:46:28 +00:00
Apply RBAC permissions across admin and tasks
Enforce page access and action-level permissions across admin and task views, including exports and record actions. Move user creation to a client component behind server-side permission checks, standardize data fetching, and remove the deprecated user-role route.
This commit is contained in:
@@ -19,9 +19,10 @@ import RbacBadge from '@/components/ui/rbac-badge';
|
||||
|
||||
type Props = {
|
||||
data: IPermission[];
|
||||
actions?: string[];
|
||||
};
|
||||
|
||||
const ComponentPermissionPage: FC<Props> = ({ data }) => {
|
||||
const ComponentPermissionPage: FC<Props> = ({ data, actions }) => {
|
||||
const [filterData, setFilterData] = useState<IPermission[]>([]);
|
||||
const [selectedData, setSelectedData] = useState<IPermission | undefined>(undefined);
|
||||
const [showFormDialog, setShowFormDialog] = useState(false);
|
||||
@@ -97,7 +98,7 @@ const ComponentPermissionPage: FC<Props> = ({ data }) => {
|
||||
key: 'action',
|
||||
width: 80,
|
||||
align: 'center',
|
||||
render: (_, record) => (
|
||||
render: (_, record) => actions?.includes('UPDATE') || actions?.includes('EDIT') || actions?.includes('DELETE') && (
|
||||
<AlertDialog>
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
@@ -107,19 +108,23 @@ const ComponentPermissionPage: FC<Props> = ({ data }) => {
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
<DropdownMenuItem
|
||||
onClick={() => {
|
||||
setSelectedData(record);
|
||||
setShowFormDialog(true);
|
||||
}}
|
||||
>
|
||||
Edit Permission
|
||||
</DropdownMenuItem>
|
||||
<AlertDialogTrigger asChild>
|
||||
<DropdownMenuItem className="text-rose-600 dark:text-rose-400">
|
||||
Delete Permission
|
||||
{(actions.includes('UPDATE') || actions.includes('EDIT')) && (
|
||||
<DropdownMenuItem
|
||||
onClick={() => {
|
||||
setSelectedData(record);
|
||||
setShowFormDialog(true);
|
||||
}}
|
||||
>
|
||||
Edit Permission
|
||||
</DropdownMenuItem>
|
||||
</AlertDialogTrigger>
|
||||
)}
|
||||
{(actions.includes('DELETE')) && (
|
||||
<AlertDialogTrigger asChild>
|
||||
<DropdownMenuItem className="text-rose-600 dark:text-rose-400">
|
||||
Delete Permission
|
||||
</DropdownMenuItem>
|
||||
</AlertDialogTrigger>
|
||||
)}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
<ConfirmAsync
|
||||
@@ -160,18 +165,20 @@ const ComponentPermissionPage: FC<Props> = ({ data }) => {
|
||||
onChange={handleSearch}
|
||||
/>
|
||||
</div>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
className="h-8 text-his hover:text-his hover:bg-his/10 font-semibold"
|
||||
onClick={() => {
|
||||
setSelectedData(undefined);
|
||||
setShowFormDialog(true);
|
||||
}}
|
||||
>
|
||||
<PlusCircledIcon className="mr-2 h-4 w-4" />
|
||||
Create Permission
|
||||
</Button>
|
||||
{(!actions || actions.includes('CREATE')) && (
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
className="h-8 text-his hover:text-his hover:bg-his/10 font-semibold"
|
||||
onClick={() => {
|
||||
setSelectedData(undefined);
|
||||
setShowFormDialog(true);
|
||||
}}
|
||||
>
|
||||
<PlusCircledIcon className="mr-2 h-4 w-4" />
|
||||
Create Permission
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<Table
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Suspense, lazy } from 'react';
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import LazyLoading from '@/components/loader/LazyLoading';
|
||||
import fetchData from '@/lib/fetchData';
|
||||
import { getAccessPagePermission } from '@/lib/get-access-data';
|
||||
|
||||
const ComponentPage = lazy(() => import('./component'));
|
||||
|
||||
@@ -27,6 +28,7 @@ async function getPermissions() {
|
||||
}
|
||||
|
||||
export default async function PermissionPage() {
|
||||
const actions = await getAccessPagePermission(['/rbac/action-permissions', '/rbac/action-pemission']);
|
||||
const permissions = await getPermissions();
|
||||
|
||||
return (
|
||||
@@ -42,7 +44,7 @@ export default async function PermissionPage() {
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Suspense fallback={<LazyLoading />}>
|
||||
<ComponentPage data={permissions} />
|
||||
<ComponentPage data={permissions} actions={actions} />
|
||||
</Suspense>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
@@ -19,6 +19,7 @@ import { useRouter } from 'next/navigation';
|
||||
|
||||
type Props = {
|
||||
data: IMenu[];
|
||||
actions?: string[];
|
||||
};
|
||||
|
||||
// Transform flat menu list into hierarchical tree for Antd Table
|
||||
@@ -61,7 +62,7 @@ function buildMenuTree(items: IMenu[]): IMenu[] {
|
||||
return roots;
|
||||
}
|
||||
|
||||
const ComponentMenuPage: FC<Props> = ({ data }) => {
|
||||
const ComponentMenuPage: FC<Props> = ({ data, actions }) => {
|
||||
const [rawMenus, setRawMenus] = useState<IMenu[]>([]);
|
||||
const [filterData, setFilterData] = useState<IMenu[]>([]);
|
||||
const [selectedData, setSelectedData] = useState<IMenu | undefined>(undefined);
|
||||
@@ -205,7 +206,7 @@ const ComponentMenuPage: FC<Props> = ({ data }) => {
|
||||
key: 'row_action',
|
||||
width: 80,
|
||||
align: 'center',
|
||||
render: (_, record) => (
|
||||
render: (_, record) => (actions?.includes('UPDATE') || actions?.includes('EDIT') || actions?.includes('DELETE')) && (
|
||||
<AlertDialog>
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
@@ -215,19 +216,23 @@ const ComponentMenuPage: FC<Props> = ({ data }) => {
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
<DropdownMenuItem
|
||||
onClick={() => {
|
||||
setSelectedData(record);
|
||||
setShowFormDialog(true);
|
||||
}}
|
||||
>
|
||||
Edit Menu
|
||||
</DropdownMenuItem>
|
||||
<AlertDialogTrigger asChild>
|
||||
<DropdownMenuItem className="text-rose-600 dark:text-rose-400">
|
||||
Delete Menu
|
||||
{(actions.includes('UPDATE') || actions.includes('EDIT')) && (
|
||||
<DropdownMenuItem
|
||||
onClick={() => {
|
||||
setSelectedData(record);
|
||||
setShowFormDialog(true);
|
||||
}}
|
||||
>
|
||||
Edit Menu
|
||||
</DropdownMenuItem>
|
||||
</AlertDialogTrigger>
|
||||
)}
|
||||
{(actions.includes('DELETE')) && (
|
||||
<AlertDialogTrigger asChild>
|
||||
<DropdownMenuItem className="text-rose-600 dark:text-rose-400">
|
||||
Delete Menu
|
||||
</DropdownMenuItem>
|
||||
</AlertDialogTrigger>
|
||||
)}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
<ConfirmAsync
|
||||
@@ -267,18 +272,20 @@ const ComponentMenuPage: FC<Props> = ({ data }) => {
|
||||
onChange={handleSearch}
|
||||
/>
|
||||
</div>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
className="h-8 text-his hover:text-his hover:bg-his/10 font-semibold"
|
||||
onClick={() => {
|
||||
setSelectedData(undefined);
|
||||
setShowFormDialog(true);
|
||||
}}
|
||||
>
|
||||
<PlusCircledIcon className="mr-2 h-4 w-4" />
|
||||
Create Menu
|
||||
</Button>
|
||||
{(actions?.includes('CREATE')) && (
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
className="h-8 text-his hover:text-his hover:bg-his/10 font-semibold"
|
||||
onClick={() => {
|
||||
setSelectedData(undefined);
|
||||
setShowFormDialog(true);
|
||||
}}
|
||||
>
|
||||
<PlusCircledIcon className="mr-2 h-4 w-4" />
|
||||
Create Menu
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<Table
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Suspense, lazy } from 'react';
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import LazyLoading from '@/components/loader/LazyLoading';
|
||||
import fetchData from '@/lib/fetchData';
|
||||
import { getAccessPagePermission } from '@/lib/get-access-data';
|
||||
|
||||
const ComponentPage = lazy(() => import('./component'));
|
||||
|
||||
@@ -27,6 +28,7 @@ async function getMenus() {
|
||||
}
|
||||
|
||||
export default async function MenuPage() {
|
||||
const actions = await getAccessPagePermission('/rbac/menu');
|
||||
const menus = await getMenus();
|
||||
|
||||
return (
|
||||
@@ -42,7 +44,7 @@ export default async function MenuPage() {
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Suspense fallback={<LazyLoading />}>
|
||||
<ComponentPage data={menus} />
|
||||
<ComponentPage data={menus} actions={actions} />
|
||||
</Suspense>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
@@ -3,6 +3,7 @@ import { type Metadata } from 'next';
|
||||
import Link from 'next/link';
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import fetchData from '@/lib/fetchData';
|
||||
import { getAccessPagePermission } from '@/lib/get-access-data';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { type IRole } from '../component/type';
|
||||
import RolePermissionRow from "../component/role-permission-row";
|
||||
@@ -39,6 +40,7 @@ async function getRoleData(id: string): Promise<IRole | undefined> {
|
||||
}
|
||||
|
||||
const UserRoleDetailPage: FC<Props> = async ({ params }) => {
|
||||
const actions = await getAccessPagePermission('/rbac/user-role', 'VIEW');
|
||||
const { id } = await params;
|
||||
const data: IRole | undefined = await getRoleData(id);
|
||||
|
||||
@@ -109,11 +111,13 @@ const UserRoleDetailPage: FC<Props> = async ({ params }) => {
|
||||
</CardDescription>
|
||||
</div>
|
||||
</div>
|
||||
<Link href={`/admin/rbac/user-role/edit/${id}`}>
|
||||
<Button className="bg-his hover:bg-his/90 text-white font-medium">
|
||||
<PencilLine /> Edit Role
|
||||
</Button>
|
||||
</Link>
|
||||
{(actions.includes('UPDATE') || actions.includes('EDIT')) && (
|
||||
<Link href={`/admin/rbac/user-role/edit/${id}`}>
|
||||
<Button className="bg-his hover:bg-his/90 text-white font-medium">
|
||||
<PencilLine /> Edit Role
|
||||
</Button>
|
||||
</Link>
|
||||
)}
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-6 p-4 md:p-6 rounded-lg bg-zinc-50/60 dark:bg-zinc-900/40 border border-zinc-200 dark:border-zinc-800">
|
||||
|
||||
@@ -17,9 +17,10 @@ import { type IRole } from './type';
|
||||
|
||||
type Props = {
|
||||
data: IRole[];
|
||||
actions?: string[];
|
||||
};
|
||||
|
||||
const ComponentRolePage: FC<Props> = ({ data }) => {
|
||||
const ComponentRolePage: FC<Props> = ({ data, actions }) => {
|
||||
const [filterData, setFilterData] = useState<IRole[]>([]);
|
||||
const [searchText, setSearchText] = useState('');
|
||||
|
||||
@@ -140,7 +141,7 @@ const ComponentRolePage: FC<Props> = ({ data }) => {
|
||||
key: 'action',
|
||||
width: 80,
|
||||
align: 'center',
|
||||
render: (_, record) => (
|
||||
render: (_, record) => (actions?.includes('UPDATE') || actions?.includes('EDIT') || actions?.includes('DELETE')) && (
|
||||
<AlertDialog>
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
@@ -155,16 +156,20 @@ const ComponentRolePage: FC<Props> = ({ data }) => {
|
||||
View Details
|
||||
</Link>
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem asChild>
|
||||
<Link href={`/admin/rbac/user-role/edit/${record.id}`}>
|
||||
Edit Role
|
||||
</Link>
|
||||
</DropdownMenuItem>
|
||||
<AlertDialogTrigger asChild>
|
||||
<DropdownMenuItem className="text-rose-600 dark:text-rose-400">
|
||||
Delete Role
|
||||
{(actions.includes('UPDATE') || actions.includes('EDIT')) && (
|
||||
<DropdownMenuItem asChild>
|
||||
<Link href={`/admin/rbac/user-role/edit/${record.id}`}>
|
||||
Edit Role
|
||||
</Link>
|
||||
</DropdownMenuItem>
|
||||
</AlertDialogTrigger>
|
||||
)}
|
||||
{(actions.includes('DELETE')) && (
|
||||
<AlertDialogTrigger asChild>
|
||||
<DropdownMenuItem className="text-rose-600 dark:text-rose-400">
|
||||
Delete Role
|
||||
</DropdownMenuItem>
|
||||
</AlertDialogTrigger>
|
||||
)}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
<ConfirmAsync
|
||||
@@ -204,16 +209,18 @@ const ComponentRolePage: FC<Props> = ({ data }) => {
|
||||
onChange={handleSearch}
|
||||
/>
|
||||
</div>
|
||||
<Link href="/admin/rbac/user-role/create">
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
className="h-8 text-his hover:text-his hover:bg-his/10 font-semibold"
|
||||
>
|
||||
<PlusCircledIcon className="mr-2 h-4 w-4" />
|
||||
Create Role
|
||||
</Button>
|
||||
</Link>
|
||||
{(actions?.includes('CREATE')) && (
|
||||
<Link href="/admin/rbac/user-role/create">
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
className="h-8 text-his hover:text-his hover:bg-his/10 font-semibold"
|
||||
>
|
||||
<PlusCircledIcon className="mr-2 h-4 w-4" />
|
||||
Create Role
|
||||
</Button>
|
||||
</Link>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<Table
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { type Metadata } from 'next';
|
||||
import { getAccessPagePermission } from '@/lib/get-access-data';
|
||||
import RoleForm from '../component/RoleForm';
|
||||
|
||||
export const metadata: Metadata = {
|
||||
@@ -6,6 +7,7 @@ export const metadata: Metadata = {
|
||||
description: "Create a new user role with custom menu permissions",
|
||||
};
|
||||
|
||||
export default function CreateRolePage() {
|
||||
export default async function CreateRolePage() {
|
||||
await getAccessPagePermission('/rbac/user-role', 'CREATE');
|
||||
return <RoleForm isUpdate={false} />;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { type Metadata } from 'next';
|
||||
import fetchData from '@/lib/fetchData';
|
||||
import { getAccessPagePermission } from '@/lib/get-access-data';
|
||||
import RoleForm from '../../component/RoleForm';
|
||||
import { type IRole } from '../../component/type';
|
||||
|
||||
@@ -34,6 +35,7 @@ async function getRoleData(id: string): Promise<IRole | undefined> {
|
||||
}
|
||||
|
||||
export default async function EditRolePage({ params }: Props) {
|
||||
await getAccessPagePermission('/rbac/user-role', 'UPDATE');
|
||||
const { id } = await params;
|
||||
const roleData = await getRoleData(id);
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Suspense, lazy } from 'react';
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import LazyLoading from '@/components/loader/LazyLoading';
|
||||
import fetchData from '@/lib/fetchData';
|
||||
import { getAccessPagePermission } from '@/lib/get-access-data';
|
||||
|
||||
const ComponentPage = lazy(() => import('./component'));
|
||||
|
||||
@@ -27,6 +28,7 @@ async function getRoles() {
|
||||
}
|
||||
|
||||
export default async function UserRolePage() {
|
||||
const actions = await getAccessPagePermission('/rbac/user-role');
|
||||
const roles = await getRoles();
|
||||
|
||||
return (
|
||||
@@ -42,7 +44,7 @@ export default async function UserRolePage() {
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Suspense fallback={<LazyLoading />}>
|
||||
<ComponentPage data={roles} />
|
||||
<ComponentPage data={roles} actions={actions} />
|
||||
</Suspense>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
Reference in New Issue
Block a user