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:
Ichsan Kurniawan
2026-11-09 17:32:31 +07:00
parent 1b37ff65f5
commit 279b642a7a
69 changed files with 1070 additions and 1167 deletions
+19 -12
View File
@@ -17,9 +17,10 @@ import { type IGlobalParameter } from './type';
type Props = {
data: IGlobalParameter[]
actions?: string[]
};
const ComponentPage: FC<Props> = ({ data }) => {
const ComponentPage: FC<Props> = ({ data, actions }) => {
const [filterData, setFilterData] = useState<IGlobalParameter[]>([])
const [selectedData, setSelectedData] = useState<IGlobalParameter | undefined>(undefined)
const [showFormDialog, setShowFormDialog] = useState(false)
@@ -32,7 +33,7 @@ const ComponentPage: FC<Props> = ({ data }) => {
}
}, [data])
const handleDelete = (id: string | number) => {
const handleDelete = async (id: string | number) => {
return axios.delete(`/global-parameter/${id}`).then(res => res.data)
}
@@ -117,7 +118,7 @@ const ComponentPage: FC<Props> = ({ data }) => {
{
title: 'Action',
key: 'action',
render: (_, record) => (
render: (_, record) => (actions?.includes('UPDATE') || actions?.includes('EDIT') || actions?.includes('DELETE')) && (
<AlertDialog>
<DropdownMenu>
<DropdownMenuTrigger asChild>
@@ -127,10 +128,14 @@ const ComponentPage: FC<Props> = ({ data }) => {
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem onClick={() => { setShowFormDialog(true); setSelectedData(record) }} >Edit</DropdownMenuItem>
<AlertDialogTrigger asChild>
<DropdownMenuItem>Delete</DropdownMenuItem>
</AlertDialogTrigger>
{(actions.includes('UPDATE') || actions.includes('EDIT')) && (
<DropdownMenuItem onClick={() => { setShowFormDialog(true); setSelectedData(record) }} >Edit</DropdownMenuItem>
)}
{(actions.includes('DELETE')) && (
<AlertDialogTrigger asChild>
<DropdownMenuItem>Delete</DropdownMenuItem>
</AlertDialogTrigger>
)}
</DropdownMenuContent>
</DropdownMenu>
<ConfirmAsync
@@ -171,12 +176,14 @@ const ComponentPage: FC<Props> = ({ data }) => {
<div className='flex flex-col space-y-4'>
<div className="flex flex-col sm:flex-row items-start sm:items-center justify-between space-y-2 sm:space-y-0">
<div className="flex flex-1 items-center space-x-2">
<Input placeholder="Search parameters..." className="h-8 w-full sm:w-[250px]" onChange={handleSearch} />
<Input placeholder="Search parameters..." className="h-8 w-full sm:w-62.5 lg:w-72" onChange={handleSearch} />
</div>
<Button size="sm" variant='ghost' className="ml-auto h-8 w-full sm:w-max text-his" onClick={() => setShowFormDialog(true)}>
<PlusCircledIcon className="mr-2 h-4 w-4" />
Create New
</Button>
{(actions?.includes('CREATE')) && (
<Button size="sm" variant='ghost' className="ml-auto h-8 w-full sm:w-max text-his" onClick={() => setShowFormDialog(true)}>
<PlusCircledIcon className="mr-2 h-4 w-4" />
Create New
</Button>
)}
</div>
<Table
{...tableProps}
+10 -34
View File
@@ -1,12 +1,11 @@
import { type Metadata } from 'next';
import { Suspense, lazy } from 'react';
import { getServerSession } from 'next-auth';
import { Download } from 'lucide-react'
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { type ResponseData } from '@/types/types';
import LazyLoading from '@/components/loader/LazyLoading';
import { authOptions } from '@/lib/auth';
import fetchData from '@/lib/fetchData';
import { getAccessPagePermission } from '@/lib/get-access-data';
const ComponentPage = lazy(() => import('./component'));
@@ -15,35 +14,9 @@ export const metadata: Metadata = {
title: "Admin | Global Parameter",
}
async function getData() {
const { BASE_URL_API } = process.env
const session = await getServerSession(authOptions)
const res = await fetch(BASE_URL_API + '/global-parameter', {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${session?.user.access_token}`,
},
cache: 'no-cache',
next: {
tags: ['global-parameter']
}
})
const data: ResponseData = await res.json()
// Recommendation: handle errors
if (!res.ok) {
// This will activate the closest `error.js` Error Boundary
throw new Error(`Failed to fetch data, ${data.code || res.status} ${data.message || res.statusText}`)
}
return data.data
}
export default async function GlobalParameterPage() {
const data = await getData()
const actions = await getAccessPagePermission('/global-parameter')
const data = await fetchData('/global-parameter', 'no-cache', { tags: ['global-parameter'] })
return (
<>
@@ -54,14 +27,17 @@ export default async function GlobalParameterPage() {
<CardTitle>Global Parameter</CardTitle>
<CardDescription>Here&apos;s a list of parameters that have been registered!</CardDescription>
</div>
<div>
<Button className="sm:hidden" size={'icon'}><Download size={20} /></Button>
</div>
{actions.includes('EXPORT') &&
<div>
<Button className="sm:hidden" size={'icon'}><Download size={20} /></Button>
</div>
}
</CardHeader>
<CardContent>
<Suspense fallback={<LazyLoading />}>
<ComponentPage
data={data.global_parameter}
actions={actions}
/>
</Suspense>
</CardContent>