Skip to content

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

Block access to resources when permissions don't match

dominik-zeglen224d6ef501cf
diff --git a/saleor/static/dashboard-next/AppRoot.tsx b/saleor/static/dashboard-next/AppRoot.tsx
index f3018d1c34..a6f1410354 100644
--- a/saleor/static/dashboard-next/AppRoot.tsx
+++ b/saleor/static/dashboard-next/AppRoot.tsx
@@ -21,11 +21,12 @@ import SVG from "react-inlinesvg";
import { appMountPoint } from ".";
import * as saleorLogo from "../images/logo.svg";
import { UserContext } from "./auth";
+import { User } from "./auth/types/User";
import { categoryListUrl } from "./categories";
import MenuToggle from "./components/MenuToggle";
import Navigator from "./components/Navigator";
import Toggle from "./components/Toggle";
-import { configurationMenuUrl } from "./configuration";
+import { configurationMenu, configurationMenuUrl } from "./configuration";
import i18n from "./i18n";
import ArrowDropdown from "./icons/ArrowDropdown";
import Home from "./icons/Home";
@@ -33,7 +34,6 @@ import Shop from "./icons/Shop";
import Truck from "./icons/Truck";
import { removeDoubleSlashes } from "./misc";
import { productListUrl } from "./products";
-import { User } from "./auth/types/User";
const drawerWidth = 256;
const navigationBarHeight = 64;
@@ -472,23 +472,29 @@ export const AppRoot = decorate(
onMenuItemClick={handleMenuItemClick}
/>
<div className={classes.spacer} />
- <a
- className={classes.menuListItem}
- href={removeDoubleSlashes(
- appMountPoint + configurationMenuUrl
- )}
- onClick={event =>
- handleMenuItemClick(configurationMenuUrl, event)
- }
- >
- <SettingsIcon />
- <Typography
- aria-label="configure"
- className={classes.menuListItemText}
+ {configurationMenu.filter(menuItem =>
+ user.permissions
+ .map(perm => perm.code)
+ .includes(menuItem.resource)
+ ).length > 0 && (
+ <a
+ className={classes.menuListItem}
+ href={removeDoubleSlashes(
+ appMountPoint + configurationMenuUrl
+ )}
+ onClick={event =>
+ handleMenuItemClick(configurationMenuUrl, event)
+ }
>
- {i18n.t("Configure")}
- </Typography>
- </a>
+ <SettingsIcon />
+ <Typography
+ aria-label="configure"
+ className={classes.menuListItemText}
+ >
+ {i18n.t("Configure")}
+ </Typography>
+ </a>
+ )}
</div>
</ResponsiveDrawer>
<main className={classes.content}>{children}</main>
diff --git a/saleor/static/dashboard-next/NotFound.tsx b/saleor/static/dashboard-next/NotFound.tsx
new file mode 100644
index 0000000000..080205b81a
--- /dev/null
+++ b/saleor/static/dashboard-next/NotFound.tsx
@@ -0,0 +1,11 @@
+import * as React from "react";
+
+import Navigator from "./components/Navigator";
+import NotFoundPage from "./components/NotFoundPage";
+
+export const NotFound: React.StatelessComponent = () => (
+ <Navigator>
+ {navigate => <NotFoundPage onBack={() => navigate("/")} />}
+ </Navigator>
+);
+export default NotFound;
diff --git a/saleor/static/dashboard-next/auth/AuthProvider.tsx b/saleor/static/dashboard-next/auth/AuthProvider.tsx
index 1b50c17f3f..07bc1023ba 100644
--- a/saleor/static/dashboard-next/auth/AuthProvider.tsx
+++ b/saleor/static/dashboard-next/auth/AuthProvider.tsx
@@ -19,6 +19,7 @@ interface AuthProviderOperationsProps {
isAuthenticated: boolean;
tokenAuthLoading: boolean;
tokenVerifyLoading: boolean;
+ user: User;
}
) => React.ReactElement<any>)
| React.ReactNode;
@@ -50,6 +51,7 @@ interface AuthProviderProps {
isAuthenticated: boolean;
tokenAuthLoading: boolean;
tokenVerifyLoading: boolean;
+ user: User;
}
) => React.ReactElement<any>)
| React.ReactNode;
@@ -125,7 +127,8 @@ class AuthProvider extends React.Component<
hasToken: !!getAuthToken(),
isAuthenticated,
tokenAuthLoading: tokenAuth.loading,
- tokenVerifyLoading: tokenVerify.loading
+ tokenVerifyLoading: tokenVerify.loading,
+ user
})
: children}
</UserContext.Provider>
diff --git a/saleor/static/dashboard-next/auth/components/SectionRoute.tsx b/saleor/static/dashboard-next/auth/components/SectionRoute.tsx
index 81d2cb35a6..3cec9b4296 100644
--- a/saleor/static/dashboard-next/auth/components/SectionRoute.tsx
+++ b/saleor/static/dashboard-next/auth/components/SectionRoute.tsx
@@ -3,6 +3,7 @@ import { Route, RouteProps } from "react-router-dom";
import { UserContext } from "..";
import AppRoot from "../../AppRoot";
+import NotFound from "../../NotFound";
interface SectionRouteProps extends RouteProps {
resource?: string;
@@ -20,7 +21,7 @@ export const SectionRoute: React.StatelessComponent<SectionRouteProps> = ({
<Route {...props} />
</AppRoot>
) : (
- <>404</>
+ <NotFound />
)
) : (
<AppRoot>
diff --git a/saleor/static/dashboard-next/configuration/ConfigurationPage.tsx b/saleor/static/dashboard-next/configuration/ConfigurationPage.tsx
index e7deecba89..4ef479ce82 100644
--- a/saleor/static/dashboard-next/configuration/ConfigurationPage.tsx
+++ b/saleor/static/dashboard-next/configuration/ConfigurationPage.tsx
@@ -5,17 +5,22 @@ import Typography from "@material-ui/core/Typography";
import * as React from "react";
import { IconProps } from "@material-ui/core/Icon";
+import { User } from "../auth/types/User";
import Container from "../components/Container";
import PageHeader from "../components/PageHeader";
import i18n from "../i18n";
+export interface MenuItem {
+ description: string;
+ icon: React.ReactElement<IconProps>;
+ resource: string;
+ title: string;
+ url?: string;
+}
+
interface ConfigurationPageProps {
- menu: Array<{
- description: string;
- icon: React.ReactElement<IconProps>;
- title: string;
- url?: string;
- }>;
+ menu: MenuItem[];
+ user: User;
onSectionClick: (sectionName: string) => void;
}
@@ -62,29 +67,33 @@ const decorate = withStyles(theme => ({
}
}));
export const ConfigurationPage = decorate<ConfigurationPageProps>(
- ({ classes, menu, onSectionClick }) => (
+ ({ classes, menu, user, onSectionClick }) => (
<Container width="md">
<PageHeader title={i18n.t("Configure")} />
<div className={classes.root}>
- {menu.map((menuItem, menuItemIndex) => (
- <Card
- className={menuItem.url ? classes.card : classes.cardDisabled}
- onClick={() => onSectionClick(menuItem.url)}
- key={menuItemIndex}
- >
- <CardContent className={classes.cardContent}>
- <div className={classes.icon}>{menuItem.icon}</div>
- <div>
- <Typography className={classes.sectionTitle} color="primary">
- {menuItem.title}
- </Typography>
- <Typography className={classes.sectionDescription}>
- {menuItem.description}
- </Typography>
- </div>
- </CardContent>
- </Card>
- ))}
+ {menu
+ .filter(menuItem =>
+ user.permissions.map(perm => perm.code).includes(menuItem.resource)
+ )
+ .map((menuItem, menuItemIndex) => (
+ <Card
+ className={menuItem.url ? classes.card : classes.cardDisabled}
+ onClick={() => onSectionClick(menuItem.url)}
+ key={menuItemIndex}
+ >
+ <CardContent className={classes.cardContent}>
+ <div className={classes.icon}>{menuItem.icon}</div>
+ <div>
+ <Typography className={classes.sectionTitle} color="primary">
+ {menuItem.title}
+ </Typography>
+ <Typography className={classes.sectionDescription}>
+ {menuItem.description}
+ </Typography>
+ </div>
+ </CardContent>
+ </Card>
+ ))}
</div>
</Container>
)
diff --git a/saleor/static/dashboard-next/configuration/index.tsx b/saleor/static/dashboard-next/configuration/index.tsx
index 1901a2e611..2abcd3b4d4 100644
--- a/saleor/static/dashboard-next/configuration/index.tsx
+++ b/saleor/static/dashboard-next/configuration/index.tsx
@@ -1,6 +1,7 @@
import * as React from "react";
import Navigator from "../components/Navigator";
+import { UserContext } from "../auth";
import i18n from "../i18n";
import AccountCircle from "../icons/AccountCircle";
import Ballot from "../icons/Ballot";
@@ -12,56 +13,57 @@ import Pages from "../icons/Pages";
import StoreMall from "../icons/StoreMall";
import { productTypeListUrl } from "../productTypes";
import { staffListUrl } from "../staff";
-import ConfigurationPage from "./ConfigurationPage";
+import ConfigurationPage, { MenuItem } from "./ConfigurationPage";
-export const configurationMenu = [
+export const configurationMenu: MenuItem[] = [
{
description: i18n.t("Define types of products you sell"),
icon: <Folder fontSize="inherit" />,
+ resource: "product.manage_products",
title: i18n.t("Product Types"),
url: productTypeListUrl
},
{
description: i18n.t("Define attributes of products yousell"),
- disabled: true,
icon: <Ballot fontSize="inherit" />,
+ resource: "product.manage_products",
title: i18n.t("Attributes")
},
{
description: i18n.t("Manage your employees and their permissions"),
- disabled: true,
icon: <AccountCircle fontSize="inherit" />,
+ resource: "account.manage_staff",
title: i18n.t("Staff Members"),
url: staffListUrl
},
{
description: i18n.t("Manage how you ship out orders."),
- disabled: true,
icon: <LocalShipping fontSize="inherit" />,
+ resource: "shipping.manage_shipping",
title: i18n.t("Shipping Methods")
},
{
description: i18n.t("Manage how your store charges tax"),
- disabled: true,
icon: <Monetization fontSize="inherit" />,
+ resource: "product.manage_products",
title: i18n.t("Taxes")
},
{
description: i18n.t("Define how users can navigate through your store"),
- disabled: true,
icon: <Navigation fontSize="inherit" />,
+ resource: "menu.manage_menus",
title: i18n.t("Navigation")
},
{
description: i18n.t("View and update your site settings"),
- disabled: true,
icon: <StoreMall fontSize="inherit" />,
+ resource: "site.manage_settings",
title: i18n.t("Site Settings")
},
{
description: i18n.t("Manage and add additional pages"),
- disabled: false,
icon: <Pages fontSize="inherit" />,
+ resource: "page.manage_pages",
title: i18n.t("Pages")
}
];
@@ -69,10 +71,18 @@ export const configurationMenu = [
export const configurationMenuUrl = "/configuration/";
export const ConfigurationSection: React.StatelessComponent = () => (
- <Navigator>
- {navigate => (
- <ConfigurationPage menu={configurationMenu} onSectionClick={navigate} />
+ <UserContext.Consumer>
+ {({ user }) => (
+ <Navigator>
+ {navigate => (
+ <ConfigurationPage
+ menu={configurationMenu}
+ user={user}
+ onSectionClick={navigate}
+ />
+ )}
+ </Navigator>
)}
- </Navigator>
+ </UserContext.Consumer>
);
export default ConfigurationSection;
diff --git a/saleor/static/dashboard-next/index.tsx b/saleor/static/dashboard-next/index.tsx
index de810dae6b..0a9a224f1b 100644
--- a/saleor/static/dashboard-next/index.tsx
+++ b/saleor/static/dashboard-next/index.tsx
@@ -19,9 +19,10 @@ 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, { configurationMenu } from "./configuration";
import HomePage from "./home";
import "./i18n";
+import { NotFound } from "./NotFound";
import OrdersSection from "./orders";
import PageSection from "./pages";
import ProductSection from "./products";
@@ -83,9 +84,12 @@ render(
hasToken,
isAuthenticated,
tokenAuthLoading,
- tokenVerifyLoading
+ tokenVerifyLoading,
+ user
}) => {
- return isAuthenticated ? (
+ return isAuthenticated &&
+ !tokenAuthLoading &&
+ !tokenVerifyLoading ? (
<Switch>
<SectionRoute exact path="/" component={HomePage} />
<SectionRoute
@@ -118,12 +122,18 @@ render(
path="/staff"
component={StaffSection}
/>
- <SectionRoute
- exact
- path="/configuration"
- component={ConfigurationSection}
- />
- <Route component={() => <>404</>} />
+ {configurationMenu.filter(menuItem =>
+ user.permissions
+ .map(perm => perm.code)
+ .includes(menuItem.resource)
+ ).length > 0 && (
+ <SectionRoute
+ exact
+ path="/configuration"
+ component={ConfigurationSection}
+ />
+ )}
+ <Route component={NotFound} />
</Switch>
) : hasToken && tokenVerifyLoading ? (
<LoginLoading />