From 08dd7f6d2a4694d902773f688ea125933e504eb1 Mon Sep 17 00:00:00 2001 From: Ali <83188384+testA113@users.noreply.github.com> Date: Mon, 12 Feb 2024 08:04:44 +1300 Subject: [PATCH] fix(auth): isAdmin redirect for wizard [EE-6669] (#11075) Co-authored-by: testa113 --- app/react/hooks/useAdminOnlyRedirect.ts | 28 +++++++++++++++++++ .../EndpointTypeView.tsx | 3 ++ .../EnvironmentsCreationView.tsx | 3 ++ .../environments/wizard/HomeView/HomeView.tsx | 3 ++ 4 files changed, 37 insertions(+) create mode 100644 app/react/hooks/useAdminOnlyRedirect.ts diff --git a/app/react/hooks/useAdminOnlyRedirect.ts b/app/react/hooks/useAdminOnlyRedirect.ts new file mode 100644 index 000000000..fdf787239 --- /dev/null +++ b/app/react/hooks/useAdminOnlyRedirect.ts @@ -0,0 +1,28 @@ +import { RawParams, useRouter } from '@uirouter/react'; +import { useEffect } from 'react'; + +import { useCurrentUser } from './useUser'; + +type RedirectOptions = { + to: string; + params?: RawParams; +}; + +/** + * Redirects to the given route if the user is not a Portainer admin. + * @param to The route to redirect to (default is `'portainer.home'`). + * @param params The params to pass to the route. + */ +export function useAdminOnlyRedirect( + { to, params }: RedirectOptions = { to: 'portainer.home' } +) { + const router = useRouter(); + + const { isAdmin } = useCurrentUser(); + + useEffect(() => { + if (!isAdmin) { + router.stateService.go(to, params); + } + }, [isAdmin, to, params, router.stateService]); +} diff --git a/app/react/portainer/environments/wizard/EnvironmentTypeSelectView/EndpointTypeView.tsx b/app/react/portainer/environments/wizard/EnvironmentTypeSelectView/EndpointTypeView.tsx index cee966a05..c15662ba5 100644 --- a/app/react/portainer/environments/wizard/EnvironmentTypeSelectView/EndpointTypeView.tsx +++ b/app/react/portainer/environments/wizard/EnvironmentTypeSelectView/EndpointTypeView.tsx @@ -4,6 +4,7 @@ import _ from 'lodash'; import { Wand2 } from 'lucide-react'; import { useAnalytics } from '@/react/hooks/useAnalytics'; +import { useAdminOnlyRedirect } from '@/react/hooks/useAdminOnlyRedirect'; import { Button } from '@@/buttons'; import { PageHeader } from '@@/PageHeader'; @@ -18,6 +19,8 @@ import { } from './environment-types'; export function EnvironmentTypeSelectView() { + // move this redirect logic to the router when migrating the router to react + useAdminOnlyRedirect(); const [types, setTypes] = useState([]); const { trackEvent } = useAnalytics(); const router = useRouter(); diff --git a/app/react/portainer/environments/wizard/EnvironmentsCreationView/EnvironmentsCreationView.tsx b/app/react/portainer/environments/wizard/EnvironmentsCreationView/EnvironmentsCreationView.tsx index 427d4eb44..e6b39890e 100644 --- a/app/react/portainer/environments/wizard/EnvironmentsCreationView/EnvironmentsCreationView.tsx +++ b/app/react/portainer/environments/wizard/EnvironmentsCreationView/EnvironmentsCreationView.tsx @@ -10,6 +10,7 @@ import { EnvironmentId, } from '@/react/portainer/environments/types'; import { useAnalytics } from '@/react/hooks/useAnalytics'; +import { useAdminOnlyRedirect } from '@/react/hooks/useAdminOnlyRedirect'; import { Stepper } from '@@/Stepper'; import { Widget, WidgetBody, WidgetTitle } from '@@/Widget'; @@ -32,6 +33,8 @@ import styles from './EnvironmentsCreationView.module.css'; import { WizardEndpointsList } from './WizardEndpointsList'; export function EnvironmentCreationView() { + // move this redirect logic to the router when migrating the router to react + useAdminOnlyRedirect(); const { params: { localEndpointId: localEndpointIdParam }, } = useCurrentStateAndParams(); diff --git a/app/react/portainer/environments/wizard/HomeView/HomeView.tsx b/app/react/portainer/environments/wizard/HomeView/HomeView.tsx index 28ddb5698..be3c510bc 100644 --- a/app/react/portainer/environments/wizard/HomeView/HomeView.tsx +++ b/app/react/portainer/environments/wizard/HomeView/HomeView.tsx @@ -4,6 +4,7 @@ import { EnvironmentType } from '@/react/portainer/environments/types'; import { useAnalytics } from '@/react/hooks/useAnalytics'; import DockerIcon from '@/assets/ico/vendor/docker-icon.svg?c'; import Kube from '@/assets/ico/kube.svg?c'; +import { useAdminOnlyRedirect } from '@/react/hooks/useAdminOnlyRedirect'; import { PageHeader } from '@@/PageHeader'; import { Widget, WidgetBody, WidgetTitle } from '@@/Widget'; @@ -15,6 +16,8 @@ import { useConnectLocalEnvironment } from './useFetchOrCreateLocalEnvironment'; import styles from './HomeView.module.css'; export function HomeView() { + // move this redirect logic to the router when migrating the router to react + useAdminOnlyRedirect(); const localEnvironmentAdded = useConnectLocalEnvironment(); const { trackEvent } = useAnalytics(); return (