keycloak-scim/src/App.tsx
Stan Silvert 49284a0f11
Implement basic access control. (#183)
* Implement basic access control.

* Fix formatting
2020-10-21 07:31:41 -04:00

67 lines
2.1 KiB
TypeScript

import React, { ReactNode } from "react";
import { Page } from "@patternfly/react-core";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import { Header } from "./PageHeader";
import { PageNav } from "./PageNav";
import { Help } from "./components/help-enabler/HelpHeader";
import { RealmContextProvider } from "./context/realm-context/RealmContext";
import { WhoAmIContextProvider } from "./context/whoami/WhoAmI";
import { ServerInfoProvider } from "./context/server-info/ServerInfoProvider";
import { AlertProvider } from "./components/alert/Alerts";
import { AccessContextProvider, useAccess } from "./context/access/Access";
import { routes, RouteDef } from "./route-config";
import { PageBreadCrumbs } from "./components/bread-crumb/PageBreadCrumbs";
import { ForbiddenSection } from "./ForbiddenSection";
const AppContexts = ({ children }: { children: ReactNode }) => (
<WhoAmIContextProvider>
<RealmContextProvider>
<AccessContextProvider>
<Help>
<AlertProvider>
<ServerInfoProvider>{children}</ServerInfoProvider>
</AlertProvider>
</Help>
</AccessContextProvider>
</RealmContextProvider>
</WhoAmIContextProvider>
);
// 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();
if (hasAccess(route.access)) return <route.component />;
return <ForbiddenSection />;
};
export const App = () => {
return (
<AppContexts>
<Router>
<Page
header={<Header />}
isManagedSidebar
sidebar={<PageNav />}
breadcrumb={<PageBreadCrumbs />}
>
<Switch>
{routes(() => {}).map((route, i) => (
<Route
key={i}
path={route.path}
component={() => <SecuredRoute route={route} />}
/>
))}
</Switch>
</Page>
</Router>
</AppContexts>
);
};