Skip to content

Commit 8086591

Browse files
Refactor permission check redirect (#2109)
<!-- Ensure the title clearly reflects what was changed. Provide a clear and concise description of the changes made. The PR should only contain the changes related to the issue, and no other unrelated changes. --> Fixes OPS-3916
1 parent 8a3eeba commit 8086591

7 files changed

Lines changed: 41 additions & 19 deletions

File tree

packages/react-ui/src/app/common/hooks/authorization-hooks.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
2+
import { useNavigate } from 'react-router-dom';
23

34
import { authenticationSession } from '@/app/lib/authentication-session';
45
import { Permission } from '@openops/shared';
@@ -12,3 +13,17 @@ export const useAuthorization = () => {
1213

1314
return { checkAccess, role };
1415
};
16+
17+
export const useCheckAccessAndRedirect = (permission: Permission) => {
18+
const { checkAccess } = useAuthorization();
19+
const navigate = useNavigate();
20+
const hasAccess = checkAccess(permission);
21+
22+
useEffect(() => {
23+
if (!hasAccess) {
24+
navigate('/', { replace: true });
25+
}
26+
}, [hasAccess, navigate]);
27+
28+
return hasAccess;
29+
};

packages/react-ui/src/app/features/home/components/home-onboarding-view.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ const HomeOnboardingView = ({
112112
return (
113113
<div className="flex flex-col gap-6 flex-1">
114114
{isFinOpsBenchmarkEnabled && (
115-
<PermissionGuard permission={Permission.WRITE_FLOW}>
115+
<PermissionGuard
116+
permission={[Permission.WRITE_FLOW, Permission.READ_APP_CONNECTION]}
117+
>
116118
<FinOpsBenchmarkBanner
117119
variation={benchmarkVariation}
118120
provider={benchmarkProvider}

packages/react-ui/src/app/features/home/components/home-operational-view.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ const HomeOperationalView = ({
103103
)}
104104

105105
{isFinOpsBenchmarkEnabled && (
106-
<PermissionGuard permission={Permission.WRITE_FLOW}>
106+
<PermissionGuard
107+
permission={[Permission.WRITE_FLOW, Permission.READ_APP_CONNECTION]}
108+
>
107109
<FinOpsBenchmarkBanner
108110
variation={benchmarkVariation}
109111
provider={benchmarkProvider}

packages/react-ui/src/app/routes/flows/id/index.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { PopulatedFlow } from '@openops/shared';
1+
import { Permission, PopulatedFlow } from '@openops/shared';
22
import { useQuery } from '@tanstack/react-query';
33
import { useEffect } from 'react';
44
import { Navigate, useParams, useSearchParams } from 'react-router-dom';
55

66
import { FullPageSpinner } from '@/app/common/components/full-page-spinner';
7+
import { useCheckAccessAndRedirect } from '@/app/common/hooks/authorization-hooks';
78
import { QueryKeys } from '@/app/constants/query-keys';
89
import { SEARCH_PARAMS } from '@/app/constants/search-params';
910
import { BuilderPage } from '@/app/features/builder';
@@ -15,6 +16,7 @@ import { flowsApi } from '@/app/features/flows/lib/flows-api';
1516
import { AxiosError } from 'axios';
1617

1718
const FlowBuilderPage = () => {
19+
const hasAccess = useCheckAccessAndRedirect(Permission.READ_FLOW);
1820
const { flowId } = useParams();
1921
const [searchParams, setSearchParams] = useSearchParams();
2022

@@ -51,6 +53,10 @@ const FlowBuilderPage = () => {
5153
refetchOnWindowFocus: 'always',
5254
});
5355

56+
if (!hasAccess) {
57+
return null;
58+
}
59+
5460
if (isError && (error as AxiosError).status === 404) {
5561
return <Navigate to="/404" />;
5662
}

packages/react-ui/src/app/routes/flows/index.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import qs from 'qs';
77
import { useCallback, useMemo, useState } from 'react';
88
import { useNavigate, useSearchParams } from 'react-router-dom';
99

10+
import { useCheckAccessAndRedirect } from '@/app/common/hooks/authorization-hooks';
1011
import { useDefaultSidebarState } from '@/app/common/hooks/use-default-sidebar-state';
1112
import { isModifierOrMiddleClick } from '@/app/common/navigation/table-navigation-helper';
1213
import {
@@ -16,10 +17,11 @@ import {
1617
import { flowsApi } from '@/app/features/flows/lib/flows-api';
1718
import { FlowsFolderBreadcrumbsManager } from '@/app/features/folders/component/folder-breadcrumbs-manager';
1819
import { FLOWS_TABLE_FILTERS } from '@/app/features/folders/lib/flows-table-filters';
19-
import { FlowStatus, FlowVersionState } from '@openops/shared';
20+
import { FlowStatus, FlowVersionState, Permission } from '@openops/shared';
2021

2122
const FlowsPage = () => {
2223
useDefaultSidebarState('expanded');
24+
const hasAccess = useCheckAccessAndRedirect(Permission.READ_FLOW);
2325
const navigate = useNavigate();
2426
const [tableRefresh, setTableRefresh] = useState(false);
2527
const onTableRefresh = useCallback(
@@ -58,6 +60,10 @@ const FlowsPage = () => {
5860
[],
5961
);
6062

63+
if (!hasAccess) {
64+
return null;
65+
}
66+
6167
return (
6268
<div className="flex flex-col w-full">
6369
<div className="px-7">

packages/react-ui/src/app/routes/openops-analytics/index.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { Navigate } from 'react-router-dom';
1+
import { useCheckAccessAndRedirect } from '@/app/common/hooks/authorization-hooks';
22

3-
import { useAuthorization } from '@/app/common/hooks/authorization-hooks';
43
import { flagsHooks } from '@/app/common/hooks/flags-hooks';
54
import { useDefaultSidebarState } from '@/app/common/hooks/use-default-sidebar-state';
65
import { useCandu } from '@/app/features/extensions/candu/use-candu';
@@ -17,7 +16,7 @@ import { useEmbedDashboard } from './use-embed-dashboard';
1716

1817
const OpenOpsAnalyticsPage = () => {
1918
useDefaultSidebarState('minimized');
20-
const { checkAccess } = useAuthorization();
19+
useCheckAccessAndRedirect(Permission.WRITE_ANALYTICS);
2120
const { isCanduEnabled, canduClientToken, canduUserId } = useCandu();
2221
const { data: analyticsPublicUrl } = flagsHooks.useFlag<string | undefined>(
2322
FlagId.ANALYTICS_PUBLIC_URL,
@@ -39,10 +38,6 @@ const OpenOpsAnalyticsPage = () => {
3938
canduUserId,
4039
});
4140

42-
if (!checkAccess(Permission.WRITE_ANALYTICS)) {
43-
return <Navigate to="/" replace />;
44-
}
45-
4641
if (!analyticsPublicUrl) {
4742
console.error('OpenOps Analytics URL is not defined');
4843
return null;

packages/react-ui/src/app/routes/openops-tables/index.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { t } from 'i18next';
2-
import { Navigate, useLocation } from 'react-router-dom';
2+
import { useLocation } from 'react-router-dom';
33

4-
import { useAuthorization } from '@/app/common/hooks/authorization-hooks';
4+
import { useCheckAccessAndRedirect } from '@/app/common/hooks/authorization-hooks';
55
import { flagsHooks } from '@/app/common/hooks/flags-hooks';
66
import { useDefaultSidebarState } from '@/app/common/hooks/use-default-sidebar-state';
77
import { useCandu } from '@/app/features/extensions/candu/use-candu';
88
import { FlagId, Permission } from '@openops/shared';
99

1010
const OpenOpsTablesPage = () => {
1111
useDefaultSidebarState('minimized');
12-
const { checkAccess } = useAuthorization();
12+
useCheckAccessAndRedirect(Permission.WRITE_TABLE);
1313
const { isCanduEnabled, canduClientToken, canduUserId } = useCandu();
1414
const parentData = encodeURIComponent(
1515
JSON.stringify({ isCanduEnabled, userId: canduUserId, canduClientToken }),
@@ -24,10 +24,6 @@ const OpenOpsTablesPage = () => {
2424
FlagId.OPENOPS_TABLES_PUBLIC_URL,
2525
);
2626

27-
if (!checkAccess(Permission.WRITE_TABLE)) {
28-
return <Navigate to="/" replace />;
29-
}
30-
3127
if (!openopsTablesUrl) {
3228
console.error('OpenOps Tables URL is not defined');
3329
return null;

0 commit comments

Comments
 (0)