2021-08-11 11:23:59 +00:00
|
|
|
import React, { FunctionComponent } from "react";
|
2020-09-25 17:11:25 +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";
|
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-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";
|
|
|
|
import type KeycloakAdminClient from "keycloak-admin";
|
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>
|
|
|
|
<RealmContextProvider>
|
|
|
|
<AccessContextProvider>
|
|
|
|
<Help>
|
|
|
|
<AlertProvider>
|
|
|
|
<ServerInfoProvider>
|
|
|
|
<SubGroups>{children}</SubGroups>
|
|
|
|
</ServerInfoProvider>
|
|
|
|
</AlertProvider>
|
|
|
|
</Help>
|
|
|
|
</AccessContextProvider>
|
|
|
|
</RealmContextProvider>
|
|
|
|
</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);
|
|
|
|
|
|
|
|
if (accessAllowed) return <route.component />;
|
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}
|
|
|
|
onReset={() => window.location.reload()}
|
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
|
|
|
);
|
|
|
|
};
|