mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 22:36:29 +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:
+22
-8
@@ -44,24 +44,38 @@ export const getAccessData = cache(async () => {
|
||||
});
|
||||
|
||||
|
||||
export async function getAccessPagePermission(path: string, action: string = "view") {
|
||||
export async function getAccessPagePermission(path: string | string[], action: string = "view") {
|
||||
const accessData = await getAccessData();
|
||||
|
||||
const normalize = (p: string) => (p || '').trim().replace(/^\/+|\/+$/g, '').toLowerCase();
|
||||
const targetPaths = (Array.isArray(path) ? path : [path]).map(normalize);
|
||||
|
||||
// Check whether the current page is registered
|
||||
const menu = accessData?.role_detail?.find((item: any) => item.menu?.path_url === path);
|
||||
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;
|
||||
});
|
||||
|
||||
if (!menu?.menu) {
|
||||
// redirect(redirectPage);
|
||||
forbidden()
|
||||
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 : []);
|
||||
|
||||
// Check whether the user has permission
|
||||
if (!menu?.menu?.action?.includes(action.toUpperCase())) {
|
||||
// redirect(redirectPage);
|
||||
forbidden()
|
||||
const hasPermission = permittedActions.some((act: string) => act.toUpperCase() === action.toUpperCase());
|
||||
if (!hasPermission) {
|
||||
forbidden();
|
||||
}
|
||||
|
||||
return menu?.menu?.action;
|
||||
return permittedActions;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user