Skip to content

Commit 1115a4b

Browse files
Refactor permission check redirect
1 parent e643efe commit 1115a4b

6 files changed

Lines changed: 43 additions & 17 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/templates/components/select-flow-template-dialog-content.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useAuthorization } from '@/app/common/hooks/authorization-hooks';
1+
import { useCheckAccessAndRedirect } from '@/app/common/hooks/authorization-hooks';
22
import { useTheme } from '@/app/common/providers/theme-provider';
33
import { OPENOPS_CONNECT_TEMPLATES_URL } from '@/app/constants/cloud';
44
import { ExpandedTemplate } from '@/app/features/templates/components/expanded-template';
@@ -97,8 +97,9 @@ const SelectFlowTemplateDialogContent = ({
9797
const ownerLogoUrl = useOwnerLogoUrl();
9898
const { createPollingInterval } = useUserInfoPolling();
9999
const { isCloudUser } = useShowTemplatesBanner();
100-
const { checkAccess } = useAuthorization();
101-
const hasWriteFlowPermission = checkAccess(Permission.WRITE_FLOW);
100+
const hasWriteFlowPermission = useCheckAccessAndRedirect(
101+
Permission.WRITE_FLOW,
102+
);
102103
const isFullCatalog = !isTemplatePreselected && isCloudUser;
103104

104105
const onExploreMoreClick = () => {

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: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { Navigate } from 'react-router-dom';
2-
3-
import { useAuthorization } from '@/app/common/hooks/authorization-hooks';
1+
import { useCheckAccessAndRedirect } from '@/app/common/hooks/authorization-hooks';
42
import { flagsHooks } from '@/app/common/hooks/flags-hooks';
53
import { useDefaultSidebarState } from '@/app/common/hooks/use-default-sidebar-state';
64
import { useCandu } from '@/app/features/extensions/candu/use-candu';
@@ -17,7 +15,7 @@ import { useEmbedDashboard } from './use-embed-dashboard';
1715

1816
const OpenOpsAnalyticsPage = () => {
1917
useDefaultSidebarState('minimized');
20-
const { checkAccess } = useAuthorization();
18+
const hasAccess = useCheckAccessAndRedirect(Permission.WRITE_ANALYTICS);
2119
const { isCanduEnabled, canduClientToken, canduUserId } = useCandu();
2220
const { data: analyticsPublicUrl } = flagsHooks.useFlag<string | undefined>(
2321
FlagId.ANALYTICS_PUBLIC_URL,
@@ -39,8 +37,8 @@ const OpenOpsAnalyticsPage = () => {
3937
canduUserId,
4038
});
4139

42-
if (!checkAccess(Permission.WRITE_ANALYTICS)) {
43-
return <Navigate to="/" replace />;
40+
if (!hasAccess) {
41+
return null;
4442
}
4543

4644
if (!analyticsPublicUrl) {

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

Lines changed: 5 additions & 5 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+
const hasAccess = useCheckAccessAndRedirect(Permission.WRITE_TABLE);
1313
const { isCanduEnabled, canduClientToken, canduUserId } = useCandu();
1414
const parentData = encodeURIComponent(
1515
JSON.stringify({ isCanduEnabled, userId: canduUserId, canduClientToken }),
@@ -24,8 +24,8 @@ const OpenOpsTablesPage = () => {
2424
FlagId.OPENOPS_TABLES_PUBLIC_URL,
2525
);
2626

27-
if (!checkAccess(Permission.WRITE_TABLE)) {
28-
return <Navigate to="/" replace />;
27+
if (!hasAccess) {
28+
return null;
2929
}
3030

3131
if (!openopsTablesUrl) {

0 commit comments

Comments
 (0)