Skip to content

Commit

Permalink
Display a 403 error page when a non-admin user try to access the user…
Browse files Browse the repository at this point in the history
… list view or the user-groups list view
  • Loading branch information
monsieurswag committed Apr 11, 2024
1 parent 52e25d4 commit 71661df
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
7 changes: 7 additions & 0 deletions frontend/src/lib/components/SideBar/navData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,10 @@ export const navData = {
}
]
};

export const modelNavData = navData.items.reduce((acc, navMenu) => {
return [...acc,...navMenu.items];
}, []).reduce((acc, item) => {
acc[item.href.substring(1)] = item;
return acc;
}, {});
16 changes: 14 additions & 2 deletions frontend/src/routes/(app)/[model=urlmodel]/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import { BASE_API_URL } from '$lib/utils/constants';
import { listViewFields } from '$lib/utils/table';
import { tableSourceMapper, type TableSource } from '@skeletonlabs/skeleton';

import { modelNavData } from '$lib/components/SideBar/navData';
import { error } from '@sveltejs/kit';
import { CUSTOM_MODEL_FETCH_MAP } from '$lib/utils/crud';
import type { urlModel } from '$lib/utils/types';
import type { LayoutServerLoad } from './$types';

export const load = (async ({ fetch, params }) => {
export const load = (async ({ fetch, params, locals }) => {
const modelData = modelNavData[params.model];

if (locals.user && modelData.user_groups) {
const user_groups = new Set(locals.user.user_groups.map(user_group => user_group[0]));
if (!modelData.user_groups.some(
user_group => user_groups.has(user_group)
)) {
return error(403, "You are not allowed to access this page.");
}
}

let data = null;
if (Object.prototype.hasOwnProperty.call(CUSTOM_MODEL_FETCH_MAP, params.model)) {
const fetch_function = CUSTOM_MODEL_FETCH_MAP[params.model];
Expand Down

0 comments on commit 71661df

Please sign in to comment.