diff --git a/src/actions/RolesActions.js b/src/actions/RolesActions.js index 74eeaa003..93ee01c42 100644 --- a/src/actions/RolesActions.js +++ b/src/actions/RolesActions.js @@ -1,5 +1,5 @@ import { createThunk } from "."; -import { getPolygons } from "./TiamatActions"; +import { getPolygons, getUserPermissions } from "./TiamatActions"; import * as types from "./Types"; const getAdministrativeZoneIds = (roles) => { @@ -52,3 +52,7 @@ export const updateAllowNewStopsEverywhere = export const updateAuth = (auth) => (dispatch) => { dispatch(createThunk(types.UPDATED_AUTH, auth)); }; + +export const fetchUserPermissions = () => (dispatch, getState) => { + dispatch(getUserPermissions()); +}; diff --git a/src/actions/TiamatActions.js b/src/actions/TiamatActions.js index c422c76eb..b5246d06e 100644 --- a/src/actions/TiamatActions.js +++ b/src/actions/TiamatActions.js @@ -55,6 +55,7 @@ import { topopGraphicalPlacesReportQuery, neighbourStopPlaceQuays, getLocationPermissions, + getUserPermissionsQuery, } from "../graphql/Tiamat/queries"; import mapToMutationVariables from "../modelUtils/mapToQueryVariables"; @@ -633,3 +634,13 @@ export const getLocationPermissionsForCoordinates = (longitude, latitude) => { })(dispatch); }; }; + +export const getUserPermissions = () => { + return async (dispatch, getState) => { + return handleQuery(getTiamatClient(), { + fetchPolicy: "no-cache", + query: getUserPermissionsQuery, + context: await getContext(getState().roles.auth), + })(dispatch); + }; +}; diff --git a/src/components/MainPage/SearchBox.js b/src/components/MainPage/SearchBox.js index 759913230..02f84a50f 100644 --- a/src/components/MainPage/SearchBox.js +++ b/src/components/MainPage/SearchBox.js @@ -52,7 +52,6 @@ import { Popover, } from "@mui/material"; import TextField from "@mui/material/TextField"; -import { isGuest } from "../../utils/roleUtils"; class SearchBox extends React.Component { constructor(props) { @@ -367,11 +366,11 @@ class SearchBox extends React.Component { topoiChips, topographicalPlaces, canEdit, - roleAssignments, lookupCoordinatesOpen, newStopIsMultiModal, dataSource, showFutureAndExpired, + isGuest, } = this.props; const { coordinatesDialogOpen, showMoreFilterOptions, loading } = this.state; @@ -686,7 +685,7 @@ class SearchBox extends React.Component { formatMessage={formatMessage} /> ) : null} - {!isGuest(roleAssignments) && ( + {!isGuest && (
{isCreatingNewStop ? ( { ["allowanceInfoSearchResult", "canEdit"], false, ), - roleAssignments: state.roles.auth.roleAssignments, lookupCoordinatesOpen: state.user.lookupCoordinatesOpen, newStopIsMultiModal: state.user.newStopIsMultiModal, showFutureAndExpired: state.user.searchFilters.showFutureAndExpired, + isGuest: state.roles.isGuest, }; }; diff --git a/src/containers/App.js b/src/containers/App.js index fb8dccd37..1e0c4502f 100644 --- a/src/containers/App.js +++ b/src/containers/App.js @@ -24,7 +24,11 @@ import { useDispatch } from "react-redux"; import Header from "../components/Header"; import { getTheme, getV0Theme } from "../config/themeConfig"; import SnackbarWrapper from "../components/SnackbarWrapper"; -import { fetchPolygons, updateAuth } from "../actions/RolesActions"; +import { + fetchPolygons, + fetchUserPermissions, + updateAuth, +} from "../actions/RolesActions"; import { useAppSelector } from "../store/hooks"; import configureLocalization from "../localization/localization"; import { UserActions } from "../actions"; @@ -49,9 +53,11 @@ const App = ({ children }) => { useEffect(() => { dispatch(updateAuth(auth)); - if (auth.isAuthenticated) { dispatch(fetchPolygons()); + dispatch(fetchUserPermissions()); + } else if (!auth.isLoading) { + dispatch(fetchUserPermissions()); } }, [auth]); diff --git a/src/graphql/Tiamat/queries.js b/src/graphql/Tiamat/queries.js index 37aaaf0b4..d87c12337 100644 --- a/src/graphql/Tiamat/queries.js +++ b/src/graphql/Tiamat/queries.js @@ -835,3 +835,12 @@ export const getLocationPermissions = gql` } } `; + +export const getUserPermissionsQuery = gql` + query getUserPermissions { + userPermissions { + allowNewStopEverywhere + isGuest + } + } +`; diff --git a/src/reducers/rolesReducer.js b/src/reducers/rolesReducer.js index 376b63fd3..56e6d0330 100644 --- a/src/reducers/rolesReducer.js +++ b/src/reducers/rolesReducer.js @@ -23,6 +23,7 @@ import { export const initialState = { auth: {}, fetchedPolygons: null, + isGuest: true, allowNewStopEverywhere: false, }; @@ -47,6 +48,12 @@ const rolesReducer = (state = initialState, action) => { action.result.data.locationPermissions, ), }); + } else if (action.operationName === "getUserPermissions") { + return Object.assign({}, state, { + isGuest: action.result.data.userPermissions.isGuest, + allowNewStopEverywhere: + action.result.data.userPermissions.allowNewStopEverywhere, + }); } else { return state; } diff --git a/src/utils/roleUtils.js b/src/utils/roleUtils.js index 57d7853d7..a66053823 100644 --- a/src/utils/roleUtils.js +++ b/src/utils/roleUtils.js @@ -215,8 +215,19 @@ const doesRoleGrantAccessToStop = ( return false; }; -export const isGuest = (roleAssignments) => { - return getEditStopRoles(roleAssignments).length === 0; +const getRolesFromTokenByType = (roleAssignments, type) => { + if (!roleAssignments) return []; + + let roles = []; + + roleAssignments.forEach((roleString) => { + let roleJSON = JSON.parse(roleString); + if (roleJSON.r === type) { + roles.push(roleJSON); + } + }); + + return roles; }; export const getStopPlacesForSubmodes = (legalSubmodes) => { let result = [];