Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add section route component

dominik-zeglene86006048430
diff --git a/saleor/static/dashboard-next/auth/components/SectionRoute.tsx b/saleor/static/dashboard-next/auth/components/SectionRoute.tsx
new file mode 100644
index 0000000000..81d2cb35a6
--- /dev/null
+++ b/saleor/static/dashboard-next/auth/components/SectionRoute.tsx
@@ -0,0 +1,34 @@
+import * as React from "react";
+import { Route, RouteProps } from "react-router-dom";
+
+import { UserContext } from "..";
+import AppRoot from "../../AppRoot";
+
+interface SectionRouteProps extends RouteProps {
+ resource?: string;
+}
+
+export const SectionRoute: React.StatelessComponent<SectionRouteProps> = ({
+ resource,
+ ...props
+}) => (
+ <UserContext.Consumer>
+ {({ user }) =>
+ resource ? (
+ user.permissions.map(perm => perm.code).includes(resource) ? (
+ <AppRoot>
+ <Route {...props} />
+ </AppRoot>
+ ) : (
+ <>404</>
+ )
+ ) : (
+ <AppRoot>
+ <Route {...props} />
+ </AppRoot>
+ )
+ }
+ </UserContext.Consumer>
+);
+SectionRoute.displayName = "Route";
+export default SectionRoute;
diff --git a/saleor/static/dashboard-next/index.tsx b/saleor/static/dashboard-next/index.tsx
index 60df161c90..de810dae6b 100644
--- a/saleor/static/dashboard-next/index.tsx
+++ b/saleor/static/dashboard-next/index.tsx
@@ -11,15 +11,15 @@ import { render } from "react-dom";
import { BrowserRouter, Route, Switch } from "react-router-dom";
import * as Cookies from "universal-cookie";
-import AppRoot from "./AppRoot";
import Auth, { getAuthToken, removeAuthToken } from "./auth";
import AuthProvider from "./auth/AuthProvider";
import LoginLoading from "./auth/components/LoginLoading/LoginLoading";
+import SectionRoute from "./auth/components/SectionRoute";
import CategorySection from "./categories";
import { DateProvider } from "./components/DateFormatter";
import { LocaleProvider } from "./components/Locale";
import { MessageManager } from "./components/messages";
-import { ConfigurationSection } from "./configuration";
+import ConfigurationSection from "./configuration";
import HomePage from "./home";
import "./i18n";
import OrdersSection from "./orders";
@@ -86,25 +86,45 @@ render(
tokenVerifyLoading
}) => {
return isAuthenticated ? (
- <AppRoot>
- <Switch>
- <Route exact path="/" component={HomePage} />
- <Route path="/categories" component={CategorySection} />
- <Route path="/pages" component={PageSection} />
- <Route path="/orders" component={OrdersSection} />
- <Route path="/products" component={ProductSection} />
- <Route
- path="/productTypes"
- component={ProductTypesSection}
- />
- <Route path="/staff" component={StaffSection} />
- <Route
- exact
- path="/configuration"
- component={ConfigurationSection}
- />
- </Switch>
- </AppRoot>
+ <Switch>
+ <SectionRoute exact path="/" component={HomePage} />
+ <SectionRoute
+ resource="product.manage_products"
+ path="/categories"
+ component={CategorySection}
+ />
+ <SectionRoute
+ resource="page.manage_pages"
+ path="/pages"
+ component={PageSection}
+ />
+ <SectionRoute
+ resource="order.manage_orders"
+ path="/orders"
+ component={OrdersSection}
+ />
+ <SectionRoute
+ resource="product.manage_products"
+ path="/products"
+ component={ProductSection}
+ />
+ <SectionRoute
+ resource="product.manage_products"
+ path="/productTypes"
+ component={ProductTypesSection}
+ />
+ <SectionRoute
+ resource="account.manage_staff"
+ path="/staff"
+ component={StaffSection}
+ />
+ <SectionRoute
+ exact
+ path="/configuration"
+ component={ConfigurationSection}
+ />
+ <Route component={() => <>404</>} />
+ </Switch>
) : hasToken && tokenVerifyLoading ? (
<LoginLoading />
) : (