2021-10-27 14:15:30 +00:00
|
|
|
import React, { FunctionComponent, Suspense } from "react";
|
2021-11-16 11:10:10 +00:00
|
|
|
import { Page } from "@patternfly/react-core";
|
2021-08-11 11:23:59 +00:00
|
|
|
import { HashRouter as Router, Route, Switch } from "react-router-dom";
|
2021-05-03 16:44:47 +00:00
|
|
|
import { ErrorBoundary } from "react-error-boundary";
|
2020-09-25 17:11:25 +00:00
|
|
|
|
2020-08-27 12:09:36 +00:00
|
|
|
import { Header } from "./PageHeader";
|
|
|
|
import { PageNav } from "./PageNav";
|
2020-09-02 07:51:42 +00:00
|
|
|
import { Help } from "./components/help-enabler/HelpHeader";
|
2020-09-25 17:11:25 +00:00
|
|
|
|
2020-10-08 11:38:25 +00:00
|
|
|
import { ServerInfoProvider } from "./context/server-info/ServerInfoProvider";
|
2020-10-06 19:25:05 +00:00
|
|
|
import { AlertProvider } from "./components/alert/Alerts";
|
2020-10-08 11:38:25 +00:00
|
|
|
|
2020-10-21 11:31:41 +00:00
|
|
|
import { AccessContextProvider, useAccess } from "./context/access/Access";
|
|
|
|
import { routes, RouteDef } from "./route-config";
|
2020-10-01 14:25:29 +00:00
|
|
|
import { PageBreadCrumbs } from "./components/bread-crumb/PageBreadCrumbs";
|
2021-11-16 11:10:10 +00:00
|
|
|
import { KeycloakSpinner } from "./components/keycloak-spinner/KeycloakSpinner";
|
2020-10-21 11:31:41 +00:00
|
|
|
import { ForbiddenSection } from "./ForbiddenSection";
|
2021-03-16 12:37:57 +00:00
|
|
|
import { SubGroups } from "./groups/SubGroupsContext";
|
2021-11-11 16:04:04 +00:00
|
|
|
import { RealmsProvider } from "./context/RealmsContext";
|
2021-08-11 11:23:59 +00:00
|
|
|
import { RealmContextProvider } from "./context/realm-context/RealmContext";
|
2021-02-17 21:12:25 +00:00
|
|
|
import { ErrorRenderer } from "./components/error/ErrorRenderer";
|
2021-08-11 11:23:59 +00:00
|
|
|
import { AdminClient } from "./context/auth/AdminClient";
|
|
|
|
import { WhoAmIContextProvider } from "./context/whoami/WhoAmI";
|
2021-08-26 08:39:35 +00:00
|
|
|
import type KeycloakAdminClient from "@keycloak/keycloak-admin-client";
|
2020-10-21 11:31:41 +00:00
|
|
|
|
2021-03-17 13:40:19 +00:00
|
|
|
export const mainPageContentId = "kc-main-content-page-container";
|
2020-12-11 17:34:18 +00:00
|
|
|
|
2021-08-11 11:23:59 +00:00
|
|
|
export type AdminClientProps = {
|
|
|
|
adminClient: KeycloakAdminClient;
|
2021-01-07 14:56:14 +00:00
|
|
|
};
|
|
|
|
|
2021-08-11 11:23:59 +00:00
|
|
|
const AppContexts: FunctionComponent<AdminClientProps> = ({
|
|
|
|
children,
|
|
|
|
adminClient,
|
|
|
|
}) => (
|
|
|
|
<Router>
|
|
|
|
<AdminClient.Provider value={adminClient}>
|
|
|
|
<WhoAmIContextProvider>
|
2021-11-11 16:04:04 +00:00
|
|
|
<RealmsProvider>
|
|
|
|
<RealmContextProvider>
|
|
|
|
<AccessContextProvider>
|
|
|
|
<Help>
|
|
|
|
<AlertProvider>
|
|
|
|
<ServerInfoProvider>
|
|
|
|
<SubGroups>{children}</SubGroups>
|
|
|
|
</ServerInfoProvider>
|
|
|
|
</AlertProvider>
|
|
|
|
</Help>
|
|
|
|
</AccessContextProvider>
|
|
|
|
</RealmContextProvider>
|
|
|
|
</RealmsProvider>
|
2021-08-11 11:23:59 +00:00
|
|
|
</WhoAmIContextProvider>
|
|
|
|
</AdminClient.Provider>
|
|
|
|
</Router>
|
|
|
|
);
|
|
|
|
|
2020-10-21 11:31:41 +00:00
|
|
|
// If someone tries to go directly to a route they don't
|
|
|
|
// have access to, show forbidden page.
|
|
|
|
type SecuredRouteProps = { route: RouteDef };
|
|
|
|
const SecuredRoute = ({ route }: SecuredRouteProps) => {
|
|
|
|
const { hasAccess } = useAccess();
|
2021-08-09 20:20:49 +00:00
|
|
|
const accessAllowed =
|
|
|
|
route.access instanceof Array
|
|
|
|
? hasAccess(...route.access)
|
|
|
|
: hasAccess(route.access);
|
|
|
|
|
2021-11-01 12:29:52 +00:00
|
|
|
if (accessAllowed)
|
2021-10-27 14:15:30 +00:00
|
|
|
return (
|
2021-11-16 11:10:10 +00:00
|
|
|
<Suspense fallback={<KeycloakSpinner />}>
|
2021-10-27 14:15:30 +00:00
|
|
|
<route.component />
|
|
|
|
</Suspense>
|
|
|
|
);
|
2020-10-21 11:31:41 +00:00
|
|
|
|
|
|
|
return <ForbiddenSection />;
|
|
|
|
};
|
|
|
|
|
2021-08-11 11:23:59 +00:00
|
|
|
export const App = ({ adminClient }: AdminClientProps) => {
|
2020-08-04 12:59:41 +00:00
|
|
|
return (
|
2021-08-11 11:23:59 +00:00
|
|
|
<AppContexts adminClient={adminClient}>
|
|
|
|
<Page
|
|
|
|
header={<Header />}
|
|
|
|
isManagedSidebar
|
|
|
|
sidebar={<PageNav />}
|
|
|
|
breadcrumb={<PageBreadCrumbs />}
|
|
|
|
mainContainerId={mainPageContentId}
|
|
|
|
>
|
|
|
|
<ErrorBoundary
|
|
|
|
FallbackComponent={ErrorRenderer}
|
2021-09-30 08:58:48 +00:00
|
|
|
onReset={() =>
|
|
|
|
(window.location.href =
|
|
|
|
window.location.origin + window.location.pathname)
|
|
|
|
}
|
2020-10-08 11:38:25 +00:00
|
|
|
>
|
2021-08-11 11:23:59 +00:00
|
|
|
<Switch>
|
|
|
|
{routes.map((route, i) => (
|
|
|
|
<Route
|
|
|
|
exact={route.matchOptions?.exact ?? true}
|
|
|
|
key={i}
|
|
|
|
path={route.path}
|
|
|
|
component={() => <SecuredRoute route={route} />}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</Switch>
|
|
|
|
</ErrorBoundary>
|
|
|
|
</Page>
|
2020-10-08 11:38:25 +00:00
|
|
|
</AppContexts>
|
2020-08-04 12:59:41 +00:00
|
|
|
);
|
|
|
|
};
|