Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add permissions for routes in frontend #637

Merged
merged 7 commits into from
Jul 14, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 70 additions & 10 deletions frontend/src/AppRoutes.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Route, Routes } from 'react-router-dom';
import { Route, Routes, Navigate } from 'react-router-dom';
import {
AboutPage,
AdminPage,
Expand Down Expand Up @@ -45,10 +45,61 @@ import { AdminLayout } from './PagesAdmin/AdminLayout/AdminLayout';
import { RecruitmentFormAdminPage } from './PagesAdmin/RecruitmentFormAdminPage';
import { SaksdokumentAdminPage } from './PagesAdmin/SaksdokumentAdminPage';
import { ROUTES } from './routes';
import { PERM } from './permissions';
import { useAuthContext } from './AuthContext';
import { UserDto } from './dto';
import { hasPerm } from './utils';
import { ElementType } from 'react';

/**
* Router component, to be used inside element of a route, and page that is requested
* Allows for setting up routes that requires authentication, permissions, and staff
* @param {ElementType} Page - The page to be rendered
* @param {UserDto | undefined} user - the current user, undefined if not logged in
* @param {string[] | undefined} permissions - list of permissions needed to access page, undefined if not needed
* @param {string} redirectPath - redirect to specific page if wanted
* @param {boolean} requiresStaff: - if staff permissions are required for page
*/

const ProtectedRoute = (
Page: ElementType,
user?: UserDto | undefined,
permissions?: string[] | undefined,
emilte marked this conversation as resolved.
Show resolved Hide resolved
emilte marked this conversation as resolved.
Show resolved Hide resolved
redirectPath: string = ROUTES.frontend.home,
requiresStaff = true,
) => {
// const { user } = useAuthContext(); // test for if this is the best level for fetching user
console.log(user);

if (!user) {
// All Protected routes need a user, redirects to login
//
//TODO improve to keep attempted route
return <Navigate to={ROUTES.frontend.login} replace />;
}
if (requiresStaff && !user?.is_staff) {
return <Navigate to={redirectPath} replace />;
}
if (permissions) {
for (const permission of permissions) {
console.log('perm', permission);
console.log(permissions);
console.log(hasPerm({ permission: permission, user: user }));
if (!hasPerm({ permission: permission, user: user })) {
return <Navigate to={redirectPath} replace />;
emilte marked this conversation as resolved.
Show resolved Hide resolved
}
}
emilte marked this conversation as resolved.
Show resolved Hide resolved
}
console.log(permissions);
console.log(user.permissions);
return <Page />;
};

export function AppRoutes() {
// Must be called within <BrowserRouter> because it uses hook useLocation().
useGoatCounter();
const { user } = useAuthContext();
emilte marked this conversation as resolved.
Show resolved Hide resolved
//const isStaff = user?.is_staff;

return (
<Routes>
Expand Down Expand Up @@ -76,12 +127,21 @@ export function AppRoutes() {
{/*
ADMIN ROUTES
*/}
<Route element={<AdminLayout />}>
<Route path={ROUTES.frontend.admin} element={<AdminPage />} />
<Route element={<ProtectedRoute Page={AdminLayout} />}>
<Route path={ROUTES.frontend.admin} element={<ProtectedRoute user={user} Page={AdminPage} />} />
{/* Gangs */}
<Route path={ROUTES.frontend.admin_gangs} element={<GangsAdminPage />} />
<Route path={ROUTES.frontend.admin_gangs_create} element={<GangsFormAdminPage />} />
<Route path={ROUTES.frontend.admin_gangs_edit} element={<GangsFormAdminPage />} />
<Route
path={ROUTES.frontend.admin_gangs}
element={<ProtectedRoute permissions={[PERM.SAMFUNDET_VIEW_GANG]} user={user} Page={GangsAdminPage} />}
/>
<Route
path={ROUTES.frontend.admin_gangs_create}
element={<ProtectedRoute permissions={[PERM.SAMFUNDET_ADD_GANG]} user={user} Page={GangsFormAdminPage} />}
/>
<Route
path={ROUTES.frontend.admin_gangs_edit}
element={<ProtectedRoute permissions={[PERM.SAMFUNDET_ADD_GANG]} user={user} Page={GangsFormAdminPage} />}
/>
{/* Events */}
<Route path={ROUTES.frontend.admin_events} element={<EventsAdminPage />} />
<Route path={ROUTES.frontend.admin_events_create} element={<EventCreatorAdminPage />} />
Expand Down Expand Up @@ -118,13 +178,13 @@ export function AppRoutes() {
path={ROUTES.frontend.admin_recruitment_gang_position_edit}
element={<RecruitmentPositionFormAdminPage />}
/>
</Route>
{/*
{/*
Info pages
Custom layout for edit/create
*/}
<Route path={ROUTES.frontend.admin_information_create} element={<InformationFormAdminPage />} />
<Route path={ROUTES.frontend.admin_information_edit} element={<InformationFormAdminPage />} />
<Route path={ROUTES.frontend.admin_information_create} element={<InformationFormAdminPage />} />
<Route path={ROUTES.frontend.admin_information_edit} element={<InformationFormAdminPage />} />
</Route>
{/*
SULTEN ROUTES
*/}
Expand Down