mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:46:29 +00:00
Enforce action permissions across admin pages
Gate administration actions by role permissions, centralize cached session access for data fetching, and return forbidden responses for unauthorized pages. Also lazy-load user exports and adjust search field sizing.
This commit is contained in:
@@ -17,9 +17,10 @@ import { IArea } from './type';
|
||||
|
||||
type Props = {
|
||||
data: IArea[]
|
||||
actions: string[]
|
||||
};
|
||||
|
||||
const ComponentAreaPage: FC<Props> = ({ data }) => {
|
||||
const ComponentAreaPage: FC<Props> = ({ data, actions }) => {
|
||||
const [filterData, setFilterData] = useState<IArea[]>([])
|
||||
const [selectedData, setSelectedData] = useState<IArea | undefined>(undefined)
|
||||
const [showFormDialog, setShowFormDialog] = useState(false)
|
||||
@@ -32,7 +33,7 @@ const ComponentAreaPage: FC<Props> = ({ data }) => {
|
||||
}
|
||||
}, [data])
|
||||
|
||||
const handleDelete = (areaId: string | number) => {
|
||||
const handleDelete = async (areaId: string | number) => {
|
||||
return axios.delete(`/area/${areaId}`).then(res => res.data)
|
||||
}
|
||||
|
||||
@@ -114,7 +115,7 @@ const ComponentAreaPage: FC<Props> = ({ data }) => {
|
||||
{
|
||||
title: 'Action',
|
||||
key: 'action',
|
||||
render: (_, record) => (
|
||||
render: (_, record) => actions.includes('UPDATE') || actions.includes('DELETE') && (
|
||||
<AlertDialog>
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
@@ -124,10 +125,14 @@ const ComponentAreaPage: 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') &&
|
||||
<DropdownMenuItem onClick={() => { setShowFormDialog(true); setSelectedData(record) }} >Edit</DropdownMenuItem>
|
||||
}
|
||||
{actions.includes('DELETE') &&
|
||||
<AlertDialogTrigger asChild>
|
||||
<DropdownMenuItem>Delete</DropdownMenuItem>
|
||||
</AlertDialogTrigger>
|
||||
}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
<ConfirmAsync
|
||||
@@ -159,12 +164,14 @@ const ComponentAreaPage: 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 company..." className="h-8 w-full sm:w-[250px]" onChange={handleSearch} />
|
||||
<Input placeholder="Search company..." className="h-8 w-full sm:w-62.5 lg:w-72" onChange={handleSearch} />
|
||||
</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 Area
|
||||
</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 Area
|
||||
</Button>
|
||||
}
|
||||
</div>
|
||||
<Table
|
||||
{...tableProps}
|
||||
|
||||
@@ -1,49 +1,21 @@
|
||||
import { type Metadata } from 'next';
|
||||
import { FC, Suspense, lazy } from 'react';
|
||||
import { getServerSession } from 'next-auth';
|
||||
import { Suspense, lazy } from 'react';
|
||||
import { Download } from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { type ResponseData } from '@/types/types';
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import LazyLoading from '@/components/loader/LazyLoading';
|
||||
import { authOptions } from '@/lib/auth';
|
||||
import { getAccessPagePermission } from '@/lib/get-access-data';
|
||||
import fetchData from '@/lib/fetchData';
|
||||
|
||||
const ComponentPage = lazy(() => import('./component'));
|
||||
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: "Admin | Area",
|
||||
}
|
||||
|
||||
async function getData() {
|
||||
const { BASE_URL_API } = process.env
|
||||
const session = await getServerSession(authOptions)
|
||||
|
||||
const res = await fetch(BASE_URL_API + '/area', {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${session?.user.access_token}`,
|
||||
},
|
||||
cache: 'no-cache',
|
||||
next: {
|
||||
tags: ['area']
|
||||
}
|
||||
})
|
||||
|
||||
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 AreaPage() {
|
||||
const data = await getData()
|
||||
const actions = await getAccessPagePermission('/administration/area')
|
||||
const data = await fetchData('/area', 'no-cache', { tags: ['area'] })
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -54,15 +26,15 @@ export default async function AreaPage() {
|
||||
<CardTitle>Area</CardTitle>
|
||||
<CardDescription>Here's a list of areas 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.area}
|
||||
/>
|
||||
<ComponentPage data={data.area} actions={actions} />
|
||||
</Suspense>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
@@ -5,7 +5,7 @@ import { useSession } from 'next-auth/react';
|
||||
import { MoreHorizontalIcon } from 'lucide-react';
|
||||
import { Table } from 'antd';
|
||||
import { TableProps, type ColumnsType } from 'antd/es/table';
|
||||
import { MixerHorizontalIcon, PlusCircledIcon } from '@radix-ui/react-icons';
|
||||
import { PlusCircledIcon } from '@radix-ui/react-icons';
|
||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { AlertDialog, AlertDialogTrigger } from '@/components/ui/alert-dialog';
|
||||
@@ -17,9 +17,10 @@ import { type ICompany } from './type';
|
||||
|
||||
type Props = {
|
||||
data: ICompany[]
|
||||
actions: String[]
|
||||
};
|
||||
|
||||
const ComponentCompanyPage: FC<Props> = ({ data }) => {
|
||||
const ComponentCompanyPage: FC<Props> = ({ data, actions }) => {
|
||||
const [filterData, setFilterData] = useState<ICompany[]>([])
|
||||
const [selectedData, setSelectedData] = useState<ICompany | undefined>(undefined)
|
||||
const [showFormDialog, setShowFormDialog] = useState(false)
|
||||
@@ -33,7 +34,7 @@ const ComponentCompanyPage: FC<Props> = ({ data }) => {
|
||||
}
|
||||
}, [data])
|
||||
|
||||
const handleDelete = (companyId: string | number) => {
|
||||
const handleDelete = async (companyId: string | number) => {
|
||||
// return new Promise((resolve, reject) => {
|
||||
// if (Math.random() > 0.5) {
|
||||
// setTimeout(() => {
|
||||
@@ -128,7 +129,7 @@ const ComponentCompanyPage: FC<Props> = ({ data }) => {
|
||||
{
|
||||
title: 'Action',
|
||||
key: 'action',
|
||||
render: (_, record) => (
|
||||
render: (_, record) => actions.includes('EDIT') || actions.includes('DELETE') && (
|
||||
<AlertDialog>
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
@@ -138,10 +139,14 @@ const ComponentCompanyPage: FC<Props> = ({ data }) => {
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
<DropdownMenuItem onClick={() => { setShowFormDialog(true); setSelectedData(record) }} >Edit</DropdownMenuItem>
|
||||
<AlertDialogTrigger asChild>
|
||||
<DropdownMenuItem>Delete</DropdownMenuItem>
|
||||
</AlertDialogTrigger>
|
||||
{actions.includes('EDIT') &&
|
||||
<DropdownMenuItem onClick={() => { setShowFormDialog(true); setSelectedData(record) }} >Edit</DropdownMenuItem>
|
||||
}
|
||||
{actions.includes('DELETE') &&
|
||||
<AlertDialogTrigger asChild>
|
||||
<DropdownMenuItem>Delete</DropdownMenuItem>
|
||||
</AlertDialogTrigger>
|
||||
}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
<ConfirmAsync
|
||||
@@ -183,12 +188,14 @@ const ComponentCompanyPage: 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 company..." className="h-8 w-full sm:w-[250px]" onChange={handleSearch} />
|
||||
<Input placeholder="Search company..." className="h-8 w-full sm:w-62.5 lg:w-72" onChange={handleSearch} />
|
||||
</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 Compnay
|
||||
</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 Compnay
|
||||
</Button> : null
|
||||
}
|
||||
</div>
|
||||
<Table
|
||||
{...tableProps}
|
||||
|
||||
@@ -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 { type ResponseData } from '@/types/types';
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
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 | Company",
|
||||
}
|
||||
|
||||
async function getData() {
|
||||
const { BASE_URL_API } = process.env
|
||||
const session = await getServerSession(authOptions)
|
||||
|
||||
const res = await fetch(BASE_URL_API + '/company', {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${session?.user.access_token}`,
|
||||
},
|
||||
cache: 'no-cache',
|
||||
next: {
|
||||
tags: ['company']
|
||||
}
|
||||
})
|
||||
|
||||
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 CompanyPage() {
|
||||
const data = await getData()
|
||||
const actions = await getAccessPagePermission('/administration/company')
|
||||
const data = await fetchData('/company', 'no-cache', { tags: ['company'] })
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -60,9 +33,7 @@ export default async function CompanyPage() {
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Suspense fallback={<LazyLoading />}>
|
||||
<ComponentPage
|
||||
data={data.company}
|
||||
/>
|
||||
<ComponentPage data={data.company} actions={actions} />
|
||||
</Suspense>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
@@ -17,9 +17,10 @@ import useAxiosAuth from '@/lib/axios/useAxiosAuth';
|
||||
|
||||
type Props = {
|
||||
data: IDivision[]
|
||||
actions: string[]
|
||||
};
|
||||
|
||||
const ComponentPage: FC<Props> = ({ data }) => {
|
||||
const ComponentPage: FC<Props> = ({ data, actions }) => {
|
||||
const [filterData, setFilterData] = useState<IDivision[]>([])
|
||||
const [selectedData, setSelectedData] = useState<IDivision | undefined>(undefined)
|
||||
const [showFormDialog, setShowFormDialog] = useState(false)
|
||||
@@ -32,7 +33,7 @@ const ComponentPage: FC<Props> = ({ data }) => {
|
||||
}
|
||||
}, [data])
|
||||
|
||||
const handleDelete = (divisionId: string | number) => {
|
||||
const handleDelete = async (divisionId: string | number) => {
|
||||
return axios.delete(`/division/${divisionId}`).then(res => res.data)
|
||||
}
|
||||
|
||||
@@ -111,7 +112,7 @@ const ComponentPage: FC<Props> = ({ data }) => {
|
||||
{
|
||||
title: 'Action',
|
||||
key: 'action',
|
||||
render: (_, record) => (
|
||||
render: (_, record) => actions.includes('UPDATE') || actions.includes('DELETE') && (
|
||||
<AlertDialog>
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
@@ -121,10 +122,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') &&
|
||||
<DropdownMenuItem onClick={() => { setShowFormDialog(true); setSelectedData(record) }} >Edit</DropdownMenuItem>
|
||||
}
|
||||
{actions.includes('DELETE') &&
|
||||
<AlertDialogTrigger asChild>
|
||||
<DropdownMenuItem>Delete</DropdownMenuItem>
|
||||
</AlertDialogTrigger>
|
||||
}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
<ConfirmAsync
|
||||
@@ -156,12 +161,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 department..." className="h-8 w-full sm:w-[250px]" onChange={handleSearch} />
|
||||
<Input placeholder="Search department..." className="h-8 w-full sm:w-62.5 lg:w-72" onChange={handleSearch} />
|
||||
</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 Department
|
||||
</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 Department
|
||||
</Button> : null
|
||||
}
|
||||
</div>
|
||||
<Table
|
||||
{...tableProps}
|
||||
|
||||
@@ -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 { type ResponseData } from '@/types/types';
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import LazyLoading from '@/components/loader/LazyLoading';
|
||||
import { authOptions } from '@/lib/auth';
|
||||
import { getAccessPagePermission } from '@/lib/get-access-data';
|
||||
import fetchData from '@/lib/fetchData';
|
||||
|
||||
const ComponentPage = lazy(() => import('./component'));
|
||||
|
||||
@@ -15,35 +14,9 @@ export const metadata: Metadata = {
|
||||
title: "Admin | Division",
|
||||
}
|
||||
|
||||
async function getData() {
|
||||
const { BASE_URL_API } = process.env
|
||||
const session = await getServerSession(authOptions)
|
||||
|
||||
const res = await fetch(BASE_URL_API + '/division', {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${session?.user.access_token}`,
|
||||
},
|
||||
cache: 'no-cache',
|
||||
next: {
|
||||
tags: ['division']
|
||||
}
|
||||
})
|
||||
|
||||
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()
|
||||
const actions = await getAccessPagePermission('/administration/division')
|
||||
const data = await fetchData('/division', 'no-cache', { tags: ['division'] })
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -60,9 +33,7 @@ export default async function DivisionPage() {
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Suspense fallback={<LazyLoading />}>
|
||||
<ComponentPage
|
||||
data={data.division}
|
||||
/>
|
||||
<ComponentPage data={data.division} actions={actions} />
|
||||
</Suspense>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
@@ -6,6 +6,7 @@ import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/com
|
||||
import fetchData from '@/lib/fetchData';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { type IUser } from '../component/type';
|
||||
import { getAccessPagePermission } from '@/lib/get-access-data';
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: "Admin | User Detail",
|
||||
@@ -16,8 +17,9 @@ type Props = {
|
||||
};
|
||||
|
||||
const UserDetailPage: FC<Props> = async ({ params }) => {
|
||||
const id = (await params).id
|
||||
const actions = await getAccessPagePermission('/administration/user', 'VIEW')
|
||||
|
||||
const id = (await params).id
|
||||
const data: IUser = await fetchData(`/user/${id}`)
|
||||
|
||||
return (
|
||||
@@ -35,7 +37,7 @@ const UserDetailPage: FC<Props> = async ({ params }) => {
|
||||
<CardDescription>Detail of user id {id}</CardDescription>
|
||||
</div>
|
||||
</div>
|
||||
{data.status.code === 'A' &&
|
||||
{actions.includes('UPDATE') && data.status.code === 'A' &&
|
||||
<Link href={`create?is_update=true&id=${id}`} className='w-full sm:w-max'>
|
||||
<Button variant='secondary' className='w-full'>
|
||||
Edit <i className="ti ti-edit ml-2 text-lg"></i>
|
||||
|
||||
@@ -22,9 +22,10 @@ import ResetPassword from './ResetPassword';
|
||||
|
||||
type Props = {
|
||||
data: IUser[]
|
||||
actions: string[]
|
||||
};
|
||||
|
||||
const ComponentPage: FC<Props> = ({ data }) => {
|
||||
const ComponentPage: FC<Props> = ({ data, actions }) => {
|
||||
const [filterData, setFilterData] = useState<IUser[]>([])
|
||||
const [selectedData, setSelectedData] = useState<IUser | undefined>(undefined)
|
||||
const [showFormChangePass, setShowFormChangePass] = useState(false)
|
||||
@@ -155,12 +156,20 @@ const ComponentPage: FC<Props> = ({ data }) => {
|
||||
{record.status.code === 'A' ?
|
||||
<DropdownMenuContent align="end" onClick={e => e?.stopPropagation()}>
|
||||
<DropdownMenuItem><Link href={`user/${record.id}`} className='w-full'>View</Link></DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => { setShowFormChangePass(true); setSelectedData(record) }}>Change password</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => { setShowResetPass(true), setSelectedData(record) }}>Reset Password</DropdownMenuItem>
|
||||
<DropdownMenuItem><Link href={`user/create?is_update=true&id=${record.id}`} className='w-full'>Edit</Link></DropdownMenuItem>
|
||||
<AlertDialogTrigger asChild>
|
||||
<DropdownMenuItem>Delete</DropdownMenuItem>
|
||||
</AlertDialogTrigger>
|
||||
{actions.includes('CHANGE_PASSWORD') &&
|
||||
<DropdownMenuItem onClick={() => { setShowFormChangePass(true); setSelectedData(record) }}>Change password</DropdownMenuItem>
|
||||
}
|
||||
{actions.includes('RESET_PASSWORD') &&
|
||||
<DropdownMenuItem onClick={() => { setShowResetPass(true), setSelectedData(record) }}>Reset Password</DropdownMenuItem>
|
||||
}
|
||||
{actions.includes('UPDATE') &&
|
||||
<DropdownMenuItem><Link href={`user/create?is_update=true&id=${record.id}`} className='w-full'>Edit</Link></DropdownMenuItem>
|
||||
}
|
||||
{actions.includes('DELETE') &&
|
||||
<AlertDialogTrigger asChild>
|
||||
<DropdownMenuItem>Delete</DropdownMenuItem>
|
||||
</AlertDialogTrigger>
|
||||
}
|
||||
</DropdownMenuContent>
|
||||
:
|
||||
<DropdownMenuContent align="end">
|
||||
@@ -197,14 +206,16 @@ 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 user..." className="h-8 w-full sm:w-[250px]" onChange={handleSearch} />
|
||||
<Input placeholder="Search user..." className="h-8 w-full sm:w-62.5 lg:w-72" onChange={handleSearch} />
|
||||
</div>
|
||||
<Link href='user/create'>
|
||||
<Button size="sm" className="ml-auto h-8 w-full sm:w-max text-his" variant='ghost'>
|
||||
<PlusCircledIcon className="mr-2 h-4 w-4" />
|
||||
Create New User
|
||||
</Button>
|
||||
</Link>
|
||||
{actions.includes('CREATE') &&
|
||||
<Link href='user/create'>
|
||||
<Button size="sm" className="ml-auto h-8 w-full sm:w-max text-his" variant='ghost'>
|
||||
<PlusCircledIcon className="mr-2 h-4 w-4" />
|
||||
Create New User
|
||||
</Button>
|
||||
</Link>
|
||||
}
|
||||
</div>
|
||||
<Table
|
||||
{...tableProps}
|
||||
|
||||
@@ -1,50 +1,22 @@
|
||||
import { type Metadata } from 'next';
|
||||
import { Suspense, lazy } from 'react';
|
||||
import { getServerSession } from 'next-auth';
|
||||
import { type ResponseData } from '@/types/types';
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import LazyLoading from '@/components/loader/LazyLoading';
|
||||
import ExportComponent from './component/Export';
|
||||
import { authOptions } from '@/lib/auth';
|
||||
import { getAccessPagePermission } from '@/lib/get-access-data';
|
||||
import fetchData from '@/lib/fetchData';
|
||||
|
||||
const ExportComponent = lazy(() => import('./component/Export'));
|
||||
const ComponentPage = lazy(() => import('./component'));
|
||||
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: "Admin | User",
|
||||
}
|
||||
|
||||
async function getData() {
|
||||
const { BASE_URL_API } = process.env
|
||||
const session = await getServerSession(authOptions)
|
||||
console.log("SESSION ADMIN USER PAGE :", session)
|
||||
|
||||
const res = await fetch(BASE_URL_API + '/user', {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${session?.user.access_token}`,
|
||||
},
|
||||
cache: 'no-cache',
|
||||
next: {
|
||||
tags: ['user']
|
||||
}
|
||||
})
|
||||
|
||||
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 UserPage() {
|
||||
const data = await getData()
|
||||
const actions = await getAccessPagePermission('/administration/user', 'VIEW')
|
||||
|
||||
console.log("DATA USER LENGTH :", data?.user)
|
||||
const data = await fetchData('/user', 'no-cache', { tags: ['user'] })
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -55,13 +27,15 @@ export default async function UserPage() {
|
||||
<CardTitle>User</CardTitle>
|
||||
<CardDescription className='mt-1'>Here's a list of employees that have been registered!</CardDescription>
|
||||
</div>
|
||||
<ExportComponent data={data.user} />
|
||||
{actions.includes('EXPORT') &&
|
||||
<Suspense fallback={<LazyLoading />}>
|
||||
<ExportComponent data={data.user} />
|
||||
</Suspense>
|
||||
}
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Suspense fallback={<LazyLoading />}>
|
||||
<ComponentPage
|
||||
data={data.user}
|
||||
/>
|
||||
<ComponentPage data={data.user} actions={actions} />
|
||||
</Suspense>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
@@ -16,7 +16,7 @@ import { Skeleton } from '@/components/ui/skeleton';
|
||||
|
||||
|
||||
const SidebarItem = dynamic(() => import("./SidebarItem"), {
|
||||
loading: () => <Skeleton className='w-full h-7 2xl:h-8 mx-6 mb-5' />,
|
||||
loading: () => <Skeleton className='w-full h-7 2xl:h-8 mb-5' />,
|
||||
ssr: false
|
||||
})
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import { FC, 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 ImportExcel = lazy(() => import('./component/ImportExcel'));
|
||||
const ComponentPage = lazy(() => import('./component'));
|
||||
@@ -14,6 +15,8 @@ export const metadata: Metadata = {
|
||||
type Props = {};
|
||||
|
||||
const ManualPresencePage: FC<Props> = async () => {
|
||||
const actions = await getAccessPagePermission('/manual/manual-presence', 'CREATE')
|
||||
|
||||
const fetchUser = fetchData('/user')
|
||||
const fetchStatus = fetchData('/presence/list-status')
|
||||
|
||||
@@ -23,18 +26,16 @@ const ManualPresencePage: FC<Props> = async () => {
|
||||
<>
|
||||
<div className="flex flex-col">
|
||||
<Card>
|
||||
{/* <CardHeader>
|
||||
<CardTitle>Manual Attendance</CardTitle>
|
||||
<CardDescription>Only create a manual attendance <strong>if the FR system has problems !</strong></CardDescription>
|
||||
</CardHeader> */}
|
||||
<CardHeader className='flex flex-col md:flex-row justify-between'>
|
||||
<div className='space-y-1'>
|
||||
<CardTitle>Manual Attendance</CardTitle>
|
||||
<CardDescription>Only create a manual attendance <strong>if the FR system has problems !</strong></CardDescription>
|
||||
</div>
|
||||
<Suspense fallback={<LazyLoading />}>
|
||||
<ImportExcel />
|
||||
</Suspense>
|
||||
{actions.includes('IMPORT') &&
|
||||
<Suspense fallback={<LazyLoading />}>
|
||||
<ImportExcel />
|
||||
</Suspense>
|
||||
}
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Suspense fallback={<LazyLoading />}>
|
||||
|
||||
@@ -285,7 +285,7 @@ export default function MenuForm({ data, menuList, isUpdate, onCancel }: Props)
|
||||
<Form.Item name="action" label="Action Permissions"
|
||||
tooltip="Select action permissions available on this menu"
|
||||
className="w-full"
|
||||
rules={[{ required: true, message: 'Please add aleast one action!' }]}
|
||||
// rules={[{ required: true, message: 'Please add aleast one action!' }]}
|
||||
>
|
||||
<Select mode="multiple" allowClear loading={permissionLoading} options={availablePermissions.map(p => ({ label: p, value: p }))}
|
||||
placeholder={permissionLoading ? "Loading permissions..." : "Select permissions..."}
|
||||
|
||||
+3
-4
@@ -1,12 +1,11 @@
|
||||
import { getServerSession } from "next-auth"
|
||||
import { redirect } from "next/navigation"
|
||||
import { ResponseData } from "@/types/types"
|
||||
import { logger } from "./logger"
|
||||
import { authOptions } from "./auth"
|
||||
import { getCacheServerSession } from "./get-access-data"
|
||||
|
||||
export default async function fetchData(url: string = '/', cache: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" = 'default', next?: NextFetchRequestConfig) {
|
||||
const { BASE_URL_API } = process.env
|
||||
const session = await getServerSession(authOptions)
|
||||
const session = await getCacheServerSession()
|
||||
|
||||
const res = await fetch(BASE_URL_API + url, {
|
||||
headers: {
|
||||
@@ -34,7 +33,7 @@ export default async function fetchData(url: string = '/', cache: "default" | "f
|
||||
|
||||
export async function fetchDataWithPost(url: string = '/', payload: any = {}, cache: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" = 'default', next?: NextFetchRequestConfig) {
|
||||
const { BASE_URL_API } = process.env
|
||||
const session = await getServerSession(authOptions)
|
||||
const session = await getCacheServerSession()
|
||||
|
||||
const res = await fetch(BASE_URL_API + url, {
|
||||
headers: {
|
||||
|
||||
+39
-9
@@ -3,13 +3,19 @@
|
||||
|
||||
import "server-only";
|
||||
import { cache } from "react";
|
||||
import { getServerSession } from "next-auth";
|
||||
import { getServerSession, type Session } from "next-auth";
|
||||
import { ResponseData } from "@/types/types";
|
||||
import { redirect } from "next/navigation";
|
||||
import { forbidden, redirect } from "next/navigation";
|
||||
import { authOptions } from "./auth";
|
||||
|
||||
|
||||
export const getCacheServerSession = cache(async (): Promise<Session | null> => {
|
||||
return getServerSession(authOptions)
|
||||
})
|
||||
|
||||
|
||||
export const getAccessData = cache(async () => {
|
||||
const session = await getServerSession(authOptions)
|
||||
const session = await getCacheServerSession()
|
||||
|
||||
// Validate session first
|
||||
if (!session?.user?.detail?.role?.id) {
|
||||
@@ -37,20 +43,44 @@ export const getAccessData = cache(async () => {
|
||||
return data.data
|
||||
});
|
||||
|
||||
export async function reqAccessPagePermission(path: string, action: string = "view", redirectPage: "/forbidden") {
|
||||
|
||||
export async function getAccessPagePermission(path: string, action: string = "view") {
|
||||
const accessData = await getAccessData();
|
||||
|
||||
// Check whether the current page is registered
|
||||
const menu = accessData?.role_detail?.find((item: any) => item.menu?.path_url === path);
|
||||
|
||||
if (!menu) {
|
||||
redirect(redirectPage);
|
||||
if (!menu?.menu) {
|
||||
// redirect(redirectPage);
|
||||
forbidden()
|
||||
}
|
||||
|
||||
// Check whether the user has permission
|
||||
if (!menu.action?.includes(action)) {
|
||||
redirect(redirectPage);
|
||||
if (!menu?.menu?.action?.includes(action.toUpperCase())) {
|
||||
// redirect(redirectPage);
|
||||
forbidden()
|
||||
}
|
||||
|
||||
return menu;
|
||||
return menu?.menu?.action;
|
||||
}
|
||||
|
||||
|
||||
export async function getNonAdminAccessPagePermission(path: string, action: string = "view") {
|
||||
const session = await getCacheServerSession()
|
||||
|
||||
if (!session?.user?.detail?.role?.role_detail) {
|
||||
redirect("/auth?error=Session%20error%20or%20has%20expired");
|
||||
}
|
||||
|
||||
const menu = session.user.detail?.role?.role_detail?.find(item => item.menu.path_url === path)
|
||||
|
||||
if (!menu) {
|
||||
forbidden()
|
||||
}
|
||||
|
||||
if (!menu.action?.includes(action.toUpperCase())) {
|
||||
forbidden()
|
||||
}
|
||||
|
||||
return menu.action
|
||||
}
|
||||
Reference in New Issue
Block a user