mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:46:29 +00:00
Enforce page and action-based permissions
Use RBAC permissions to guard dashboard pages and control approval and export actions. Reuse cached sessions and refine permission badges and available icons.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import { Suspense, lazy } from "react"
|
||||
import Link from "next/link"
|
||||
import { getServerSession } from "next-auth"
|
||||
import dayjs from "dayjs"
|
||||
|
||||
import { generateHourMinutes, titleCase } from "@/lib/helper"
|
||||
@@ -11,7 +10,7 @@ import { Button } from "@/components/ui/button"
|
||||
import { Card900, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
|
||||
|
||||
import LazyLoading from "@/components/loader/LazyLoading"
|
||||
import { authOptions } from "@/lib/auth"
|
||||
import { getAccessPagePermission, getCacheServerSession } from "@/lib/get-access-data"
|
||||
|
||||
// const LastPresence = lazy(() => import("./components/LastPresence"))
|
||||
const WorkingHours = lazy(() => import("../components/WorkingHours"))
|
||||
@@ -29,7 +28,9 @@ const ApprovalLeaveSick = lazy(() => import("../components/ApprovalLeaveSick"))
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
export default async function DashboardPage() {
|
||||
const session = await getServerSession(authOptions)
|
||||
const actions = await getAccessPagePermission('/', 'VIEW', false)
|
||||
|
||||
const session = await getCacheServerSession()
|
||||
|
||||
const today = fetchDataWithPost('/presence/history/today', { nik: session?.user.detail.nik })
|
||||
const all = fetchDataWithPost('/presence/history', { nik: session?.user.detail.nik, start_date: dayjs().subtract(1, 'week'), end_date: dayjs().format('YYYY-MM-DD') })
|
||||
@@ -207,7 +208,7 @@ export default async function DashboardPage() {
|
||||
</Suspense>
|
||||
</CardContent>
|
||||
</Card900>
|
||||
{(session?.user.detail.role.code === 'MNG' || session?.user.detail.role.code === 'TL') &&
|
||||
{actions.includes("VIEW_APPROVAL_ATTENDANCE") &&
|
||||
<Card900>
|
||||
<CardHeader>
|
||||
<CardTitle>Need to be Approve</CardTitle>
|
||||
@@ -215,12 +216,12 @@ export default async function DashboardPage() {
|
||||
</CardHeader>
|
||||
<CardContent className="px-0">
|
||||
<Suspense fallback={<LazyLoading />}>
|
||||
<NeedApproval data={dataDashboard.presence?.need_to_be_approved} />
|
||||
<NeedApproval data={dataDashboard.presence?.need_to_be_approved} isAllowApprove={actions.includes('APPROVE_ATTENDANCE')} />
|
||||
</Suspense>
|
||||
</CardContent>
|
||||
</Card900>
|
||||
}
|
||||
{(session?.user.detail.role.code === 'MNG' || session?.user.detail.role.code === 'TL') && dataDashboard.presence?.need_to_be_approved_auto_checkout?.total > 0 &&
|
||||
{actions.includes('VIEW_AUTOCHECKOUT') &&
|
||||
<Card900>
|
||||
<CardHeader>
|
||||
<CardTitle>Adjustment Autocheckout</CardTitle>
|
||||
@@ -228,12 +229,12 @@ export default async function DashboardPage() {
|
||||
</CardHeader>
|
||||
<CardContent className="px-0">
|
||||
<Suspense fallback={<LazyLoading />}>
|
||||
<AdjustmentAutoCheckout data={dataDashboard.presence?.need_to_be_approved_auto_checkout} />
|
||||
<AdjustmentAutoCheckout data={dataDashboard.presence?.need_to_be_approved_auto_checkout} isAllowApprove={actions.includes('APPROVE_AUTOCHECKOUT')} />
|
||||
</Suspense>
|
||||
</CardContent>
|
||||
</Card900>
|
||||
}
|
||||
{(session?.user.detail.role.code === 'MNG' || session?.user.detail.role.code === 'TL') && dataDashboard.presence?.need_to_be_approved_application_form?.total > 0 &&
|
||||
{actions.includes("VIEW_LEAVE") &&
|
||||
<Card900>
|
||||
<CardHeader>
|
||||
<CardTitle>Work Leave and Sick Approval</CardTitle>
|
||||
@@ -241,7 +242,7 @@ export default async function DashboardPage() {
|
||||
</CardHeader>
|
||||
<CardContent className="px-0">
|
||||
<Suspense fallback={<LazyLoading />}>
|
||||
<ApprovalLeaveSick data={dataDashboard.presence?.need_to_be_approved_application_form} />
|
||||
<ApprovalLeaveSick data={dataDashboard.presence?.need_to_be_approved_application_form} isAllowApprove={actions.includes('APPROVE_LEAVE')} />
|
||||
</Suspense>
|
||||
</CardContent>
|
||||
</Card900>
|
||||
@@ -268,7 +269,7 @@ export default async function DashboardPage() {
|
||||
</Suspense>
|
||||
</CardContent>
|
||||
</Card900>
|
||||
{session?.user.detail.role.code === 'MNG' &&
|
||||
{actions.includes('VEIW_CONTRACT_EXPIRATION') &&
|
||||
<Card900>
|
||||
<CardHeader>
|
||||
<CardTitle>Employee Contract Expiration</CardTitle>
|
||||
|
||||
@@ -23,13 +23,14 @@ const Avatar = lazy(() => import('@/components/ViewAvatar').then(mod => ({ defau
|
||||
|
||||
type Props = {
|
||||
data: any
|
||||
isAllowApprove?: boolean
|
||||
className?: string
|
||||
};
|
||||
|
||||
const sortBy = 'presence_date'
|
||||
const filterBy = ['fullname', 'position_name', 'working_status']
|
||||
|
||||
const AdjusmentAutoCheckout: FC<Props> = ({ data, className }) => {
|
||||
const AdjusmentAutoCheckout: FC<Props> = ({ data, isAllowApprove, className }) => {
|
||||
const [isLoading, setIsLoading] = useState(false)
|
||||
const [isFullLoading, setIsFullLoading] = useState(false)
|
||||
const [showDialog, setShowDialog] = useState(false)
|
||||
@@ -287,12 +288,14 @@ const AdjusmentAutoCheckout: FC<Props> = ({ data, className }) => {
|
||||
<AlertDialogCancel disabled={isLoading} onClick={() => { setShowDialog(false); setDataPresence(null) }} >
|
||||
Cancel
|
||||
</AlertDialogCancel>
|
||||
<AlertDialogAction disabled={isLoading} onClick={() => { setShowConfirm(true) }}>
|
||||
{isLoading && (
|
||||
<Icons.spinner className="mr-1 h-4 w-4 animate-spin" />
|
||||
)}
|
||||
Approve AutoCheckout
|
||||
</AlertDialogAction>
|
||||
{isAllowApprove &&
|
||||
<AlertDialogAction disabled={isLoading} onClick={() => { setShowConfirm(true) }}>
|
||||
{isLoading && (
|
||||
<Icons.spinner className="mr-1 h-4 w-4 animate-spin" />
|
||||
)}
|
||||
Approve AutoCheckout
|
||||
</AlertDialogAction>
|
||||
}
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
|
||||
@@ -23,13 +23,14 @@ const Avatar = lazy(() => import('@/components/ViewAvatar').then(mod => ({ defau
|
||||
|
||||
type Props = {
|
||||
data: any
|
||||
isAllowApprove?: boolean
|
||||
className?: string
|
||||
};
|
||||
|
||||
const sortBy = 'start_date'
|
||||
const filterBy = ['fullname', 'position_name', 'category']
|
||||
|
||||
const ApprovalLeaveSick: FC<Props> = ({ data, className }) => {
|
||||
const ApprovalLeaveSick: FC<Props> = ({ data, className, isAllowApprove }) => {
|
||||
const [isLoading, setIsLoading] = useState(false)
|
||||
const [isFullLoading, setIsFullLoading] = useState(false)
|
||||
const [showDialog, setShowDialog] = useState(false)
|
||||
@@ -252,18 +253,20 @@ const ApprovalLeaveSick: FC<Props> = ({ data, className }) => {
|
||||
<AlertDialogCancel disabled={isLoading} onClick={() => { setIsApproval(false); setShowConfirm(true) }} className='text-red-500 font-semibold'>
|
||||
Decline
|
||||
</AlertDialogCancel>
|
||||
<AlertDialogAction disabled={isLoading} onClick={() => { setIsApproval(true); setShowConfirm(true) }}>
|
||||
{isLoading && (
|
||||
<Icons.spinner className="mr-1 h-4 w-4 animate-spin" />
|
||||
)}
|
||||
Approve
|
||||
</AlertDialogAction>
|
||||
{isAllowApprove &&
|
||||
<AlertDialogAction disabled={isLoading} onClick={() => { setIsApproval(true); setShowConfirm(true) }}>
|
||||
{isLoading && (
|
||||
<Icons.spinner className="mr-1 h-4 w-4 animate-spin" />
|
||||
)}
|
||||
Approve
|
||||
</AlertDialogAction>
|
||||
}
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog >
|
||||
}
|
||||
{
|
||||
showConfirm &&
|
||||
|
||||
{showConfirm &&
|
||||
<AlertDialog open={true}>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
@@ -286,6 +289,7 @@ const ApprovalLeaveSick: FC<Props> = ({ data, className }) => {
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
}
|
||||
|
||||
{isFullLoading && <PageLoading className='bg-opacity-40 dark:bg-opacity-20 z-999' />}
|
||||
</>
|
||||
);
|
||||
|
||||
@@ -20,13 +20,14 @@ const Avatar = lazy(() => import('@/components/ViewAvatar').then(mod => ({ defau
|
||||
|
||||
type Props = {
|
||||
data: any
|
||||
isAllowApprove?: boolean
|
||||
className?: string
|
||||
};
|
||||
|
||||
const sortBy = 'presence_date'
|
||||
const filterBy = ['fullname', 'position_name', 'working_status']
|
||||
|
||||
const NeedApproval: FC<Props> = ({ data, className }) => {
|
||||
const NeedApproval: FC<Props> = ({ data, isAllowApprove, className }) => {
|
||||
const [isLoading, setIsLoading] = useState(false)
|
||||
const [isFullLoading, setIsFullLoading] = useState(false)
|
||||
const [showDialog, setShowDialog] = useState(false)
|
||||
@@ -242,17 +243,19 @@ const NeedApproval: FC<Props> = ({ data, className }) => {
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel disabled={isLoading} onClick={() => { setIsApproval(false); setShowConfirm(true) }} className='text-red-500 font-semibold'>
|
||||
Decline
|
||||
</AlertDialogCancel>
|
||||
<AlertDialogAction disabled={isLoading} onClick={() => { setIsApproval(true); setShowConfirm(true) }}>
|
||||
{isLoading && (
|
||||
<Icons.spinner className="mr-1 h-4 w-4 animate-spin" />
|
||||
)}
|
||||
Approve
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
{isAllowApprove &&
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel disabled={isLoading} onClick={() => { setIsApproval(false); setShowConfirm(true) }} className='text-red-500 font-semibold'>
|
||||
Decline
|
||||
</AlertDialogCancel>
|
||||
<AlertDialogAction disabled={isLoading} onClick={() => { setIsApproval(true); setShowConfirm(true) }}>
|
||||
{isLoading && (
|
||||
<Icons.spinner className="mr-1 h-4 w-4 animate-spin" />
|
||||
)}
|
||||
Approve
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
}
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
}
|
||||
|
||||
@@ -18,9 +18,10 @@ import { CircleCheck } from "lucide-react"
|
||||
|
||||
type Props = {
|
||||
data: IHistory[]
|
||||
actions?: string[]
|
||||
}
|
||||
|
||||
export default function DataTable({ data }: Props) {
|
||||
export default function DataTable({ data, actions }: Props) {
|
||||
const searchParams = useSearchParams();
|
||||
|
||||
const currentPage = Number(searchParams!.get('page') as string) || 1;
|
||||
@@ -62,7 +63,7 @@ export default function DataTable({ data }: Props) {
|
||||
// console.log(data)
|
||||
return (
|
||||
<div className="w-full pt-2">
|
||||
<SortFilter data={data} />
|
||||
<SortFilter data={data} actions={actions} />
|
||||
<div className="rounded-md border border-border mb-5">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
|
||||
@@ -10,9 +10,10 @@ const DataTable = lazy(() => import('./DataTable'));
|
||||
|
||||
type Props = {
|
||||
searchParams: Promise<{ [key: string]: string | string[] | undefined }>
|
||||
actions?: string[]
|
||||
};
|
||||
|
||||
export default async function AttendanceHistory({ searchParams }: Props) {
|
||||
export default async function AttendanceHistory({ searchParams, actions }: Props) {
|
||||
const start_date = (await searchParams)!.start_date
|
||||
const end_date = (await searchParams)!.end_date
|
||||
const search = (await searchParams)!.search
|
||||
@@ -40,7 +41,7 @@ export default async function AttendanceHistory({ searchParams }: Props) {
|
||||
|
||||
return (
|
||||
<Suspense fallback={<LazyLoading />}>
|
||||
<DataTable data={getFilterData()} />
|
||||
<DataTable data={getFilterData()} actions={actions} />
|
||||
</Suspense>
|
||||
);
|
||||
}
|
||||
@@ -7,9 +7,10 @@ import ExportComponent from './Export';
|
||||
|
||||
type Props = {
|
||||
data?: any[]
|
||||
actions?: string[]
|
||||
};
|
||||
|
||||
const SortFilter: FC<Props> = ({ data }) => {
|
||||
const SortFilter: FC<Props> = ({ data, actions }) => {
|
||||
const searchParams = useSearchParams();
|
||||
const router = useRouter()
|
||||
const path = usePathname()
|
||||
@@ -64,7 +65,9 @@ const SortFilter: FC<Props> = ({ data }) => {
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
<ExportComponent data={data!} />
|
||||
{actions?.includes('EXPORT') &&
|
||||
<ExportComponent data={data!} />
|
||||
}
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
|
||||
@@ -28,9 +28,10 @@ dayjs.extend(customParseFormat);
|
||||
|
||||
type Props = {
|
||||
data: ILeaveSickPermit[]
|
||||
actions?: string[]
|
||||
}
|
||||
|
||||
export default function DataTable({ data }: Props) {
|
||||
export default function DataTable({ data, actions }: Props) {
|
||||
const searchParams = useSearchParams();
|
||||
const [isPreviewLoading, setIsPreviewLoading] = useState(false)
|
||||
const [isDownLoading, setIsDownLoading] = useState(false)
|
||||
@@ -183,7 +184,7 @@ export default function DataTable({ data }: Props) {
|
||||
|
||||
return (
|
||||
<div className="w-full pt-2">
|
||||
<SortFilter data={data} />
|
||||
<SortFilter data={data} actions={actions} />
|
||||
<div className="rounded-md border border-border mb-5">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
|
||||
@@ -10,9 +10,10 @@ const DataTable = lazy(() => import('./DataTable'));
|
||||
|
||||
type Props = {
|
||||
searchParams: Promise<{ [key: string]: string | string[] | undefined }>
|
||||
actions?: string[]
|
||||
};
|
||||
|
||||
export default async function LeavePermitHistory({ searchParams }: Props) {
|
||||
export default async function LeavePermitHistory({ searchParams, actions }: Props) {
|
||||
const start_date = (await searchParams)!.start_date
|
||||
const end_date = (await searchParams)!.end_date
|
||||
const search = (await searchParams)!.search
|
||||
|
||||
@@ -5,15 +5,14 @@ import { Card, CardContent } from "@/components/ui/card";
|
||||
import { Suspense, lazy } from "react";
|
||||
import SkeletonTable from "@/components/skeleton/SkeletonTable";
|
||||
import TabsComponent from "./components/Tabs";
|
||||
import { getServerSession } from "next-auth";
|
||||
import { authOptions } from "@/lib/auth";
|
||||
import { getAccessPagePermission, getCacheServerSession } from "@/lib/get-access-data";
|
||||
|
||||
const AttendanceHistory = lazy(() => import("./attendance"));
|
||||
const LeavePermitHistory = lazy(() => import("./leave-permit"));
|
||||
|
||||
|
||||
export async function generateMetadata(): Promise<Metadata> {
|
||||
const session = await getServerSession(authOptions)
|
||||
const session = await getCacheServerSession()
|
||||
|
||||
return {
|
||||
title: `${session?.user?.detail.fullname || 'User'}'s History - HIS Attendance Task System (H.A.T.s)`,
|
||||
@@ -25,6 +24,8 @@ type Props = {
|
||||
};
|
||||
|
||||
export default async function HistoryPage({ searchParams }: Props) {
|
||||
const actions = await getAccessPagePermission('/history', 'VIEW', false)
|
||||
|
||||
const tab = (await searchParams)!.tab
|
||||
|
||||
return (
|
||||
@@ -46,9 +47,9 @@ export default async function HistoryPage({ searchParams }: Props) {
|
||||
<CardContent>
|
||||
<Suspense fallback={<SkeletonTable />} key={tab as string}>
|
||||
{tab === 'leave-permit' ?
|
||||
<LeavePermitHistory searchParams={searchParams} />
|
||||
<LeavePermitHistory searchParams={searchParams} actions={actions} />
|
||||
:
|
||||
<AttendanceHistory searchParams={searchParams} />
|
||||
<AttendanceHistory searchParams={searchParams} actions={actions} />
|
||||
}
|
||||
</Suspense>
|
||||
</CardContent>
|
||||
|
||||
@@ -4,9 +4,8 @@ import { Separator } from '@/components/ui/separator';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import PageLoading from '@/components/loader/PageLoading';
|
||||
import { fetchDataWithPost } from '@/lib/fetchData';
|
||||
import { getServerSession } from 'next-auth';
|
||||
import { redirect } from 'next/navigation';
|
||||
import { authOptions } from '@/lib/auth';
|
||||
import { getAccessPagePermission, getCacheServerSession } from '@/lib/get-access-data';
|
||||
|
||||
const FormComponent = lazy(() => import('./components/FormComponent'))
|
||||
|
||||
@@ -17,7 +16,9 @@ export const metadata: Metadata = {
|
||||
type Props = {};
|
||||
|
||||
const IamHerePage: FC<Props> = async () => {
|
||||
const session = await getServerSession(authOptions)
|
||||
await getAccessPagePermission('/iam-here', 'CREATE', false)
|
||||
|
||||
const session = await getCacheServerSession()
|
||||
const res = await fetchDataWithPost('/presence/last-status/today', { nik: session?.user.detail.nik })
|
||||
|
||||
if (res && res.is_checkin && res.device_code === '11001') {
|
||||
|
||||
@@ -4,6 +4,7 @@ import { Separator } from '@/components/ui/separator';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import PageLoading from '@/components/loader/PageLoading';
|
||||
import { fetchDataWithPost } from '@/lib/fetchData';
|
||||
import { getAccessPagePermission } from '@/lib/get-access-data';
|
||||
|
||||
const FormComponent = lazy(() => import('./components'))
|
||||
|
||||
@@ -12,6 +13,8 @@ export const metadata: Metadata = {
|
||||
}
|
||||
|
||||
const WorkLeave = async () => {
|
||||
await getAccessPagePermission('/leave', 'CREATE', false)
|
||||
|
||||
const res = await fetchDataWithPost('/user/position', { position_code: 'P02' })
|
||||
|
||||
return (
|
||||
|
||||
@@ -4,6 +4,7 @@ import { Separator } from '@/components/ui/separator';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import PageLoading from '@/components/loader/PageLoading';
|
||||
import fetchData from '@/lib/fetchData';
|
||||
import { getAccessPagePermission } from '@/lib/get-access-data';
|
||||
|
||||
const FormComponent = lazy(() => import('./components/FormComponent'))
|
||||
|
||||
@@ -21,6 +22,8 @@ type Props = {
|
||||
};
|
||||
|
||||
const PresencePage: FC<Props> = async ({ searchParams }) => {
|
||||
await getAccessPagePermission('/presence-v2', 'CREATE', false)
|
||||
|
||||
const checkout = (await searchParams)?.checkout
|
||||
|
||||
const res = await fetchData('/presence/list-status')
|
||||
|
||||
@@ -9,8 +9,7 @@ import LazyLoading from '@/components/loader/LazyLoading';
|
||||
|
||||
import { type IProfile } from './components/type';
|
||||
import { type IUser } from '@/app/admin/administration/user/component/type';
|
||||
import { getServerSession } from 'next-auth';
|
||||
import { authOptions } from '@/lib/auth';
|
||||
import { getAccessPagePermission, getCacheServerSession, getNonAdminAccessPagePermission } from '@/lib/get-access-data';
|
||||
|
||||
const ForgotPassword = lazy(() => import('./components/ForgotPassword'));
|
||||
const ChangeTheme = lazy(() => import("./components/ChangeTheme"))
|
||||
@@ -19,7 +18,7 @@ const EditProfile = lazy(() => import("./components/EditProfile"))
|
||||
const ChangeAvatar = lazy(() => import("./components/ChangeAvatar"))
|
||||
|
||||
export async function generateMetadata(): Promise<Metadata> {
|
||||
const session = await getServerSession(authOptions)
|
||||
const session = await getCacheServerSession()
|
||||
|
||||
return {
|
||||
title: `${session?.user?.detail.fullname || 'User'}'s Profile - H.A.T.s`,
|
||||
@@ -27,7 +26,9 @@ export async function generateMetadata(): Promise<Metadata> {
|
||||
}
|
||||
|
||||
export default async function ProfilePage() {
|
||||
const session = await getServerSession(authOptions)
|
||||
await getAccessPagePermission('/profile', 'VIEW', false)
|
||||
|
||||
const session = await getCacheServerSession()
|
||||
const data: IProfile = await fetchDataWithPost('/user/profile', {}, 'default', { tags: ['profile-data'] })
|
||||
|
||||
return (
|
||||
|
||||
@@ -4,6 +4,7 @@ import { Separator } from '@/components/ui/separator';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import PageLoading from '@/components/loader/PageLoading';
|
||||
import { fetchDataWithPost } from '@/lib/fetchData';
|
||||
import { getAccessPagePermission } from '@/lib/get-access-data';
|
||||
|
||||
const FormComponent = lazy(() => import('./components'))
|
||||
|
||||
@@ -12,6 +13,8 @@ export const metadata: Metadata = {
|
||||
}
|
||||
|
||||
const WorkPermit = async () => {
|
||||
await getAccessPagePermission('/work-permit', 'CREATE', false)
|
||||
|
||||
const res = await fetchDataWithPost('/user/position', { position_code: 'P02' })
|
||||
|
||||
return (
|
||||
|
||||
@@ -28,7 +28,7 @@ async function getPermissions() {
|
||||
}
|
||||
|
||||
export default async function PermissionPage() {
|
||||
const actions = await getAccessPagePermission(['/rbac/action-permissions', '/rbac/action-pemission']);
|
||||
const actions = await getAccessPagePermission('/rbac/action-permissions');
|
||||
const permissions = await getPermissions();
|
||||
|
||||
return (
|
||||
|
||||
@@ -25,6 +25,13 @@ export function getPermissionBadgeClass(permission: string): string {
|
||||
if (upper.includes('PASSWORD')) {
|
||||
return 'bg-amber-500/10 text-amber-600 border-amber-500/20 dark:bg-amber-500/20 dark:text-amber-300 dark:border-amber-500/30';
|
||||
}
|
||||
if (upper.includes('MANAGE')) {
|
||||
return 'bg-lime-500/10 text-lime-600 border-lime-500/20 dark:bg-lime-500/20 dark:text-lime-300 dark:border-lime-500/30';
|
||||
}
|
||||
if (upper.includes('VIEW') || upper.includes('READ')) {
|
||||
// Primary / HIS theme color (#EA512C)
|
||||
return 'bg-his/10 text-his border-his/20 dark:bg-his/20 dark:text-orange-300 dark:border-his/30';
|
||||
}
|
||||
if (upper.includes('APPROVE') || upper.includes('APPROVAL')) {
|
||||
return 'bg-fuchsia-500/10 text-fuchsia-600 border-fuchsia-500/20 dark:bg-fuchsia-500/20 dark:text-fuchsia-300 dark:border-fuchsia-500/30';
|
||||
}
|
||||
@@ -40,20 +47,12 @@ export function getPermissionBadgeClass(permission: string): string {
|
||||
if (upper.includes('CREATE') || upper.includes('ADD')) {
|
||||
return 'bg-sky-500/10 text-sky-600 border-sky-500/20 dark:bg-sky-500/20 dark:text-sky-300 dark:border-sky-500/30';
|
||||
}
|
||||
if (upper.includes('VIEW') || upper.includes('READ')) {
|
||||
// Primary / HIS theme color (#EA512C)
|
||||
return 'bg-his/10 text-his border-his/20 dark:bg-his/20 dark:text-orange-300 dark:border-his/30';
|
||||
}
|
||||
|
||||
// Default / Other: Cyan
|
||||
return 'bg-cyan-500/10 text-cyan-700 border-cyan-500/20 dark:bg-cyan-500/20 dark:text-cyan-300 dark:border-cyan-500/30';
|
||||
}
|
||||
|
||||
export const RbacBadge: React.FC<RbacBadgeProps> = ({
|
||||
permission,
|
||||
className,
|
||||
size = 'sm',
|
||||
}) => {
|
||||
export const RbacBadge: React.FC<RbacBadgeProps> = ({ permission, className, size = 'sm' }) => {
|
||||
if (!permission) return null;
|
||||
|
||||
const sizeClasses = size === 'sm' ? 'px-2 py-0.5 text-[11px] leading-4' : 'px-2.5 py-1 text-xs leading-4';
|
||||
|
||||
+17
-15
@@ -44,33 +44,35 @@ export const getAccessData = cache(async () => {
|
||||
});
|
||||
|
||||
|
||||
export async function getAccessPagePermission(path: string | string[], action: string = "view") {
|
||||
export async function getAccessPagePermission(path: string, action: string = "VIEW", isAdminMenu: boolean = true) {
|
||||
const accessData = await getAccessData();
|
||||
|
||||
const normalize = (p: string) => (p || '').trim().replace(/^\/+|\/+$/g, '').toLowerCase();
|
||||
const targetPaths = (Array.isArray(path) ? path : [path]).map(normalize);
|
||||
const normalizePath = (value?: string) => (value ?? "").trim().replace(/\/+/g, "/").replace(/\/$/, "").replace(/^$/, "/").toLowerCase();
|
||||
|
||||
const targetPath = normalizePath(path);
|
||||
|
||||
// Check whether the current page is registered
|
||||
const menu = accessData?.role_detail?.find((item: any) => {
|
||||
const itemPath = normalize(item.menu?.path_url || '');
|
||||
if (!itemPath) return false;
|
||||
if (targetPaths.includes(itemPath)) return true;
|
||||
if (targetPaths.includes('rbac/action-permissions') && itemPath === 'rbac/action-pemission') return true;
|
||||
if (targetPaths.includes('rbac/action-pemission') && itemPath === 'rbac/action-permissions') return true;
|
||||
return false;
|
||||
const menuPath = normalizePath(item.menu?.path_url);
|
||||
|
||||
return (
|
||||
menuPath === targetPath &&
|
||||
item.menu?.is_admin_menu === isAdminMenu
|
||||
);
|
||||
});
|
||||
|
||||
if (!menu?.menu) {
|
||||
forbidden();
|
||||
}
|
||||
|
||||
// Role-specific assigned actions take precedence; fallback to menu actions if role actions are absent
|
||||
const permittedActions: string[] = (Array.isArray(menu?.action) && menu.action.length > 0)
|
||||
? menu.action
|
||||
: (Array.isArray(menu?.menu?.action) ? menu.menu.action : []);
|
||||
const permittedActions: string[] =
|
||||
Array.isArray(menu.action) && menu.action.length > 0
|
||||
? menu.action
|
||||
: Array.isArray(menu.menu.action)
|
||||
? menu.menu.action
|
||||
: [];
|
||||
|
||||
// Check whether the user has permission
|
||||
const hasPermission = permittedActions.some((act: string) => act.toUpperCase() === action.toUpperCase());
|
||||
|
||||
if (!hasPermission) {
|
||||
forbidden();
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ export const TABLER_ICON_OPTIONS: ITablerIconOption[] = [
|
||||
{ label: 'Overview', value: 'ti ti-layers-linked', category: 'General' },
|
||||
{ label: 'Dashboard', value: 'ti ti-layout-dashboard', category: 'General' },
|
||||
{ label: 'Home', value: 'ti ti-home', category: 'General' },
|
||||
{ label: 'Home', value: 'ti ti-home-stats', category: 'General' },
|
||||
{ label: 'Menu', value: 'ti ti-menu-2', category: 'General' },
|
||||
{ label: 'Settings', value: 'ti ti-settings', category: 'General' },
|
||||
{ label: 'Adjustments', value: 'ti ti-adjustments', category: 'General' },
|
||||
@@ -25,6 +26,7 @@ export const TABLER_ICON_OPTIONS: ITablerIconOption[] = [
|
||||
{ label: 'Users', value: 'ti ti-users', category: 'Administration' },
|
||||
{ label: 'User Shield', value: 'ti ti-user-shield', category: 'Administration' },
|
||||
{ label: 'User Check', value: 'ti ti-user-check', category: 'Administration' },
|
||||
{ label: 'User Circle', value: 'ti ti-user-circle', category: 'Administration' },
|
||||
{ label: 'Shield Lock', value: 'ti ti-shield-lock', category: 'Administration' },
|
||||
{ label: 'Key', value: 'ti ti-key', category: 'Administration' },
|
||||
{ label: 'Lock', value: 'ti ti-lock', category: 'Administration' },
|
||||
@@ -52,6 +54,7 @@ export const TABLER_ICON_OPTIONS: ITablerIconOption[] = [
|
||||
{ label: 'Calendar Off', value: 'ti ti-calendar-x', category: 'Operations' },
|
||||
{ label: 'Clock', value: 'ti ti-clock', category: 'Operations' },
|
||||
{ label: 'Checklist', value: 'ti ti-checklist', category: 'Operations' },
|
||||
{ label: 'History', value: 'ti ti-history-toggle', category: 'Operations' },
|
||||
|
||||
// Reports & Analytics
|
||||
{ label: 'Chart Bar', value: 'ti ti-chart-bar', category: 'Reports' },
|
||||
|
||||
Reference in New Issue
Block a user