mirror of
https://github.com/portainer/portainer.git
synced 2025-08-05 05:45:22 +02:00
feat(namespace): migrate create ns to react [EE-2226] (#10377)
This commit is contained in:
parent
31bcba96c6
commit
7218eb0892
83 changed files with 1869 additions and 358 deletions
48
app/react/hooks/useUnauthorizedRedirect.ts
Normal file
48
app/react/hooks/useUnauthorizedRedirect.ts
Normal file
|
@ -0,0 +1,48 @@
|
|||
import { useRouter } from '@uirouter/react';
|
||||
import { useEffect } from 'react';
|
||||
|
||||
import { EnvironmentId } from '../portainer/environments/types';
|
||||
|
||||
import { useAuthorizations } from './useUser';
|
||||
|
||||
type AuthorizationOptions = {
|
||||
authorizations: string | string[];
|
||||
forceEnvironmentId?: EnvironmentId;
|
||||
adminOnlyCE?: boolean;
|
||||
};
|
||||
|
||||
type RedirectOptions = {
|
||||
to: string;
|
||||
params: Record<string, unknown>;
|
||||
};
|
||||
|
||||
/**
|
||||
* Redirects to the given route if the user is not authorized.
|
||||
* @param authorizations The authorizations to check.
|
||||
* @param forceEnvironmentId The environment id to use for the check.
|
||||
* @param adminOnlyCE Whether to check only for admin authorizations in CE.
|
||||
* @param to The route to redirect to.
|
||||
* @param params The params to pass to the route.
|
||||
*/
|
||||
export function useUnauthorizedRedirect(
|
||||
{
|
||||
authorizations,
|
||||
forceEnvironmentId,
|
||||
adminOnlyCE = false,
|
||||
}: AuthorizationOptions,
|
||||
{ to, params }: RedirectOptions
|
||||
) {
|
||||
const router = useRouter();
|
||||
|
||||
const isAuthorized = useAuthorizations(
|
||||
authorizations,
|
||||
forceEnvironmentId,
|
||||
adminOnlyCE
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isAuthorized) {
|
||||
router.stateService.go(to, params);
|
||||
}
|
||||
}, [isAuthorized, params, to, router.stateService]);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue