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
@@ -17,9 +17,10 @@ import { type IDeviceType } from './type';
type Props = {
data: IDeviceType[]
actions?: string[]
};
const ComponentPage: FC<Props> = ({ data }) => {
const ComponentPage: FC<Props> = ({ data, actions }) => {
const [filterData, setFilterData] = useState<IDeviceType[]>([])
const [selectedData, setSelectedData] = useState<IDeviceType | undefined>(undefined)
const [showFormDialog, setShowFormDialog] = useState(false)
@@ -32,7 +33,7 @@ const ComponentPage: FC<Props> = ({ data }) => {
}
}, [data])
const handleDelete = (deviceTypeId: string | number) => {
const handleDelete = async (deviceTypeId: string | number) => {
return axios.delete(`/device-type/${deviceTypeId}`).then(res => res.data)
}
@@ -107,7 +108,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>
@@ -117,10 +118,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
@@ -162,12 +167,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 device type..." className="h-8 w-full sm:w-[250px]" onChange={handleSearch} />
<Input placeholder="Search device type..." 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 Device Type
</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 Device Type
</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 LazyLoading from '@/components/loader/LazyLoading';
import { type ResponseData } from '@/types/types';
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 | Device Type",
}
async function getData() {
const { BASE_URL_API } = process.env
const session = await getServerSession(authOptions)
const res = await fetch(BASE_URL_API + '/device-type', {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${session?.user.access_token}`,
},
cache: 'no-cache',
next: {
tags: ['device-type']
}
})
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 DeviceTypePage() {
const data = await getData()
const actions = await getAccessPagePermission('/device/device-type')
const data = await fetchData('/device-type', 'no-cache', { tags: ['device-type'] })
return (
<>
@@ -54,14 +27,17 @@ export default async function DeviceTypePage() {
<CardTitle>Device Type</CardTitle>
<CardDescription>Here&apos;s a list of device types 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.device_type}
actions={actions}
/>
</Suspense>
</CardContent>
+18 -11
View File
@@ -17,9 +17,10 @@ import { IDevice } from './type';
type Props = {
data: IDevice[]
actions?: string[]
};
const ComponentPage: FC<Props> = ({ data }) => {
const ComponentPage: FC<Props> = ({ data, actions }) => {
const [filterData, setFilterData] = useState<IDevice[]>([])
const [selectedData, setSelectedData] = useState<IDevice | undefined>(undefined)
const [showFormDialog, setShowFormDialog] = useState(false)
@@ -32,7 +33,7 @@ const ComponentPage: FC<Props> = ({ data }) => {
}
}, [data])
const handleDelete = (deviceId: string | number) => {
const handleDelete = async (deviceId: string | number) => {
return axios.delete(`/device/${deviceId}`).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
@@ -168,10 +173,12 @@ const ComponentPage: FC<Props> = ({ data }) => {
Filter
</Button>
</div>
<Button variant='ghost' size="sm" 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 Device
</Button>
{(actions?.includes('CREATE')) && (
<Button variant='ghost' size="sm" 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 Device
</Button>
)}
</div>
<Table
{...tableProps}
+11 -35
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 LazyLoading from '@/components/loader/LazyLoading';
import { type ResponseData } from '@/types/types';
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 | Device",
}
async function getData() {
const { BASE_URL_API } = process.env
const session = await getServerSession(authOptions)
const res = await fetch(BASE_URL_API + '/device', {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${session?.user.access_token}`,
},
cache: 'no-cache',
next: {
tags: ['device']
}
})
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 DivisionPage() {
const data = await getData()
export default async function DevicePage() {
const actions = await getAccessPagePermission('/device/device')
const data = await fetchData('/device', 'no-cache', { tags: ['device'] })
return (
<>
@@ -54,14 +27,17 @@ export default async function DivisionPage() {
<CardTitle>Device</CardTitle>
<CardDescription>Here&apos;s a list of devices that have been registered!</CardDescription>
</div>
<div className="flex items-center space-x-2">
<Button className="sm:hidden" size={'icon'}><Download size={20} /></Button>
</div>
{actions.includes('EXPORT') &&
<div className="flex items-center space-x-2">
<Button className="sm:hidden" size={'icon'}><Download size={20} /></Button>
</div>
}
</CardHeader>
<CardContent>
<Suspense fallback={<LazyLoading />}>
<ComponentPage
data={data.device}
actions={actions}
/>
</Suspense>
</CardContent>