web: people: Added restricted prop to PrivateRoute (to prevent direct movement to Admin's functional)

This commit is contained in:
Alexey Safronov 2019-08-14 17:52:03 +03:00
parent d6e09f19e4
commit 1ecb618bfb
3 changed files with 26 additions and 10 deletions

View File

@ -8,6 +8,8 @@ import PrivateRoute from "./helpers/privateRoute";
import Profile from './components/pages/Profile';
import ProfileAction from './components/pages/ProfileAction';
import GroupAction from './components/pages/GroupAction';
import { isAdmin } from "./store/auth/selectors";
import { Error404 } from "./components/pages/Error";
/*const Profile = lazy(() => import("./components/pages/Profile"));
const ProfileAction = lazy(() => import("./components/pages/ProfileAction"));
@ -30,26 +32,24 @@ const App = ({ settings }) => {
<PrivateRoute
path={`${homepage}/edit/:userId`}
component={ProfileAction}
restricted
/>
<PrivateRoute
path={`${homepage}/create/:type`}
component={ProfileAction}
restricted
/>
<PrivateRoute
path={`${homepage}/group/edit/:groupId`}
component={GroupAction}
restricted
/>
<PrivateRoute
path={`${homepage}/group/create`}
component={GroupAction}
restricted
/>
<PrivateRoute
component={() => (
<ErrorContainer>
Sorry, the resource cannot be found.
</ErrorContainer>
)}
/>
<PrivateRoute component={Error404} />
</Switch>
</Suspense>
</PeopleLayout>

View File

@ -0,0 +1,8 @@
import React from 'react';
import { ErrorContainer } from 'asc-web-components';
export const Error404 = () => (
<ErrorContainer>
Sorry, the resource cannot be found.
</ErrorContainer>
);

View File

@ -4,9 +4,11 @@ import PropTypes from "prop-types";
import { Route } from "react-router-dom";
import ExternalRedirect from '../helpers/externalRedirect';
import { PageLayout, Loader } from "asc-web-components";
import { isAdmin } from "../store/auth/selectors";
import { Error404 } from "../components/pages/Error";
const PrivateRoute = ({ component: Component, ...rest }) => {
const { isAuthenticated, isLoaded } = rest;
const { isAuthenticated, isLoaded, isAdmin, restricted } = rest;
return (
<Route
{...rest}
@ -18,7 +20,12 @@ const PrivateRoute = ({ component: Component, ...rest }) => {
}
/>
) : isAuthenticated ? (
<Component {...props} />
restricted
? (isAdmin
? <Component {...props} />
: <Error404 />
)
: <Component {...props} />
) : (
<ExternalRedirect to="/login" />
)
@ -34,7 +41,8 @@ PrivateRoute.propTypes = {
function mapStateToProps(state) {
return {
isAuthenticated: state.auth.isAuthenticated,
isLoaded: state.auth.isLoaded
isLoaded: state.auth.isLoaded,
isAdmin: isAdmin(state.auth.user)
};
}