Skip to content

Commit 3c5721f

Browse files
Merge pull request #7034 from hotosm/fix/6906-pages-access-through-url
prevent accessing unauthorized pages through url
2 parents d0acb60 + f833d7d commit 3c5721f

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

frontend/src/views/management.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { useEffect } from 'react';
1+
import { useEffect, useMemo } from 'react';
22
import { useSelector } from 'react-redux';
33
import { FormattedMessage } from 'react-intl';
4-
import { Outlet, useLocation, useNavigate } from 'react-router-dom';
4+
import { Outlet, useLocation, useNavigate, useParams } from 'react-router-dom';
55

66
import messages from './messages';
77
import { useFetch } from '../hooks/UseFetch';
@@ -38,8 +38,23 @@ export function ManagementPageIndex() {
3838
</>
3939
);
4040
}
41+
const adminOnlyAccessRoutes = [
42+
'/manage/campaigns',
43+
'/manage/partners',
44+
'/manage/categories',
45+
'/manage/users',
46+
'/manage/licenses',
47+
];
48+
49+
const orgAdminOnlyAccessRoutes = [
50+
'/manage/projects',
51+
'/manage/organizations',
52+
'/manage/teams',
53+
'/manage/stats',
54+
];
4155

4256
export const ManagementSection = (props) => {
57+
const { id } = useParams();
4358
const location = useLocation();
4459
const navigate = useNavigate();
4560
const userDetails = useSelector((state) => state.auth.userDetails);
@@ -58,12 +73,29 @@ export const ManagementSection = (props) => {
5873
}
5974
}, [location.pathname, navigate, token]);
6075

76+
const isAdminRoute = useMemo(
77+
() =>
78+
[...adminOnlyAccessRoutes, ...orgAdminOnlyAccessRoutes].some(
79+
(route) => location.pathname.startsWith(route) || location.pathname === '/manage',
80+
),
81+
[location.pathname],
82+
);
83+
84+
const isOrgAdminRoute = useMemo(
85+
() =>
86+
orgAdminOnlyAccessRoutes.some((route) => location.pathname.startsWith(route)) ||
87+
location.pathname === '/manage',
88+
[location.pathname],
89+
);
90+
// access this page from here and restrictd on the page itslf if it has no edit access
91+
const isProjectEditRoute = location.pathname.startsWith('/manage/projects') && id;
92+
6193
return (
6294
<>
63-
{isOrgManager ||
64-
userDetails.role === 'ADMIN' ||
65-
location.pathname.startsWith('/manage/teams/') ||
66-
location.pathname.startsWith('/manage/projects/') ? (
95+
{isProjectEditRoute ||
96+
!(isAdminRoute || isOrgAdminRoute) ||
97+
(isAdminRoute && userDetails?.role === 'ADMIN') ||
98+
(isOrgAdminRoute && isOrgManager) ? (
6799
<div className="w-100 ph5-l pb5-l pb2-m ph2-m cf bg-tan blue-dark">
68100
{(isOrgManager || userDetails.role === 'ADMIN') && (
69101
<ManagementMenu isAdmin={userDetails && userDetails.role === 'ADMIN'} />

0 commit comments

Comments
 (0)