diff --git a/jsapp/js/router/allRoutes.es6 b/jsapp/js/router/allRoutes.es6 index 1aad3a04d2..6d863fdad8 100644 --- a/jsapp/js/router/allRoutes.es6 +++ b/jsapp/js/router/allRoutes.es6 @@ -163,7 +163,7 @@ const AllRoutes = class AllRoutes extends React.Component { path={ROUTES.LIBRARY_ITEM} element={ } @@ -172,7 +172,7 @@ const AllRoutes = class AllRoutes extends React.Component { path={ROUTES.EDIT_LIBRARY_ITEM} element={ } @@ -181,7 +181,7 @@ const AllRoutes = class AllRoutes extends React.Component { path={ROUTES.NEW_LIBRARY_CHILD} element={ } @@ -190,7 +190,7 @@ const AllRoutes = class AllRoutes extends React.Component { path={ROUTES.LIBRARY_ITEM_JSON} element={ } @@ -199,7 +199,7 @@ const AllRoutes = class AllRoutes extends React.Component { path={ROUTES.LIBRARY_ITEM_XFORM} element={ } @@ -224,9 +224,9 @@ const AllRoutes = class AllRoutes extends React.Component { path={ROUTES.FORM_SUMMARY} element={ } @@ -236,9 +236,9 @@ const AllRoutes = class AllRoutes extends React.Component { path={ROUTES.FORM_LANDING} element={ } @@ -253,9 +253,9 @@ const AllRoutes = class AllRoutes extends React.Component { path={ROUTES.FORM_REPORT} element={ } @@ -265,9 +265,9 @@ const AllRoutes = class AllRoutes extends React.Component { element={ } /> @@ -276,9 +276,9 @@ const AllRoutes = class AllRoutes extends React.Component { element={ } /> @@ -287,9 +287,9 @@ const AllRoutes = class AllRoutes extends React.Component { element={ } /> @@ -298,9 +298,9 @@ const AllRoutes = class AllRoutes extends React.Component { element={ } /> @@ -309,9 +309,9 @@ const AllRoutes = class AllRoutes extends React.Component { element={ } /> @@ -320,9 +320,9 @@ const AllRoutes = class AllRoutes extends React.Component { element={ } /> @@ -330,7 +330,7 @@ const AllRoutes = class AllRoutes extends React.Component { path={ROUTES.FORM_PROCESSING} element={ } @@ -343,7 +343,10 @@ const AllRoutes = class AllRoutes extends React.Component { element={ } /> @@ -352,7 +355,7 @@ const AllRoutes = class AllRoutes extends React.Component { element={ } /> @@ -361,7 +364,7 @@ const AllRoutes = class AllRoutes extends React.Component { element={ } /> @@ -370,7 +373,7 @@ const AllRoutes = class AllRoutes extends React.Component { element={ } /> @@ -379,7 +382,8 @@ const AllRoutes = class AllRoutes extends React.Component { element={ } /> @@ -388,7 +392,7 @@ const AllRoutes = class AllRoutes extends React.Component { element={ } /> @@ -397,7 +401,7 @@ const AllRoutes = class AllRoutes extends React.Component { element={ } /> @@ -408,7 +412,7 @@ const AllRoutes = class AllRoutes extends React.Component { element={ } /> @@ -417,7 +421,7 @@ const AllRoutes = class AllRoutes extends React.Component { element={ } /> @@ -426,7 +430,7 @@ const AllRoutes = class AllRoutes extends React.Component { element={ } /> @@ -442,9 +446,9 @@ const AllRoutes = class AllRoutes extends React.Component { element={ } /> diff --git a/jsapp/js/router/permProtectedRoute.es6 b/jsapp/js/router/permProtectedRoute.es6 index 528ce8ff21..731894cc6f 100644 --- a/jsapp/js/router/permProtectedRoute.es6 +++ b/jsapp/js/router/permProtectedRoute.es6 @@ -13,7 +13,8 @@ import {withRouter} from './legacy'; * @prop {string} path - one of PATHS * @prop {object} route * @prop {object} route.protectedComponent - the target route commponent that should be displayed for authenticateed user - * @prop {object} route.requiredPermission - the permission needed to be able to see the route + * @prop {array} route.requiredPermissions - the list of permissions needed to be able to see the route + * @prop {boolean} route.requireAll - toggle whether all permissions of `requiredPermissions` are required or only one of them */ class PermProtectedRoute extends React.Component { constructor(props) { @@ -27,7 +28,7 @@ class PermProtectedRoute extends React.Component { return { // Whether loadAsset call was made and ended, regardless of success or failure isLoadAssetFinished: false, - userHasRequiredPermission: null, + userHasRequiredPermissions: null, errorMessage: null, asset: null, }; @@ -52,13 +53,15 @@ class PermProtectedRoute extends React.Component { this.setState(this.getInitialState()); actions.resources.loadAsset({id: nextProps.params.uid}); } else if ( - this.props.requiredPermission !== nextProps.requiredPermission || + this.props.requiredPermissions !== nextProps.requiredPermissions || + this.props.requireAll !== nextProps.requireAll || this.props.protectedComponent !== nextProps.protectedComponent ) { this.setState({ - userHasRequiredPermission: this.getUserHasRequiredPermission( + userHasRequiredPermissions: this.getUserHasRequiredPermissions( this.state.asset, - nextProps.requiredPermission + nextProps.requiredPermissions, + nextProps.requireAll ), }); } @@ -72,9 +75,10 @@ class PermProtectedRoute extends React.Component { this.setState({ asset: asset, isLoadAssetFinished: true, - userHasRequiredPermission: this.getUserHasRequiredPermission( + userHasRequiredPermissions: this.getUserHasRequiredPermissions( asset, - this.props.requiredPermission + this.props.requiredPermissions, + this.props.requireAll ), }); } @@ -83,7 +87,7 @@ class PermProtectedRoute extends React.Component { if (response.status >= 400) { this.setState({ isLoadAssetFinished: true, - userHasRequiredPermission: false, + userHasRequiredPermissions: false, errorMessage: `${response.status.toString()}: ${ response.responseJSON?.detail || response.statusText }`, @@ -99,10 +103,18 @@ class PermProtectedRoute extends React.Component { ); } + getUserHasRequiredPermissions(asset, requiredPermissions, all = false) { + if (all) { + return requiredPermissions.every((perm) => this.getUserHasRequiredPermission(asset, perm)); + } else { + return requiredPermissions.some((perm) => this.getUserHasRequiredPermission(asset, perm)); + } + } + render() { if (!this.state.isLoadAssetFinished) { return ; - } else if (this.state.userHasRequiredPermission) { + } else if (this.state.userHasRequiredPermissions) { return ( }>