mirror of https://github.com/portainer/portainer
fix(auth): isAdmin redirect for wizard [EE-6669] (#11075)
Co-authored-by: testa113 <testa113>pull/11103/head
parent
ce4b0e759c
commit
08dd7f6d2a
|
@ -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]);
|
||||
}
|
|
@ -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<EnvironmentOptionValue[]>([]);
|
||||
const { trackEvent } = useAnalytics();
|
||||
const router = useRouter();
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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 (
|
||||
|
|
Loading…
Reference in New Issue