From 05623d59c14896da5ac1b2527e93d4af50ec87b6 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 4 May 2022 18:02:01 -0700 Subject: Handle user permissions --- modern/src/settings/ComputedAttributesPage.js | 19 +++++++++---------- modern/src/settings/OptionsLayout.js | 23 +++++++++++++++-------- 2 files changed, 24 insertions(+), 18 deletions(-) (limited to 'modern/src/settings') diff --git a/modern/src/settings/ComputedAttributesPage.js b/modern/src/settings/ComputedAttributesPage.js index b989b43e..78842e33 100644 --- a/modern/src/settings/ComputedAttributesPage.js +++ b/modern/src/settings/ComputedAttributesPage.js @@ -3,11 +3,11 @@ import { TableContainer, Table, TableRow, TableCell, TableHead, TableBody, makeStyles, IconButton, } from '@material-ui/core'; import MoreVertIcon from '@material-ui/icons/MoreVert'; -import { useSelector } from 'react-redux'; import { useEffectAsync } from '../reactHelper'; import EditCollectionView from '../EditCollectionView'; import OptionsLayout from './OptionsLayout'; import { useTranslation } from '../LocalizationProvider'; +import { useAdministrator } from '../common/permissions'; const useStyles = makeStyles((theme) => ({ columnAction: { @@ -21,7 +21,7 @@ const ComputedAttributeView = ({ updateTimestamp, onMenuClick }) => { const t = useTranslation(); const [items, setItems] = useState([]); - const adminEnabled = useSelector((state) => state.session.user && state.session.user.administrator); + const administrator = useAdministrator(); useEffectAsync(async () => { const response = await fetch('/api/attributes/computed'); @@ -35,7 +35,7 @@ const ComputedAttributeView = ({ updateTimestamp, onMenuClick }) => { - {adminEnabled && } + {administrator && } {t('sharedDescription')} {t('sharedAttribute')} {t('sharedExpression')} @@ -45,13 +45,12 @@ const ComputedAttributeView = ({ updateTimestamp, onMenuClick }) => { {items.map((item) => ( - {adminEnabled - && ( - - onMenuClick(event.currentTarget, item.id)}> - - - + {administrator && ( + + onMenuClick(event.currentTarget, item.id)}> + + + )} {item.description} {item.attribute} diff --git a/modern/src/settings/OptionsLayout.js b/modern/src/settings/OptionsLayout.js index 6ba636c2..3a78929b 100644 --- a/modern/src/settings/OptionsLayout.js +++ b/modern/src/settings/OptionsLayout.js @@ -26,6 +26,7 @@ import ExitToAppIcon from '@material-ui/icons/ExitToApp'; import SideNav from '../components/SideNav'; import NavBar from '../components/NavBar'; import { useTranslation } from '../LocalizationProvider'; +import { useAdministrator, useReadonly } from '../common/permissions'; const useStyles = makeStyles((theme) => ({ root: { @@ -70,18 +71,15 @@ const OptionsLayout = ({ children }) => { const [openDrawer, setOpenDrawer] = useState(false); const [optionTitle, setOptionTitle] = useState(); - const admin = useSelector((state) => state.session.user?.administrator); + const readonly = useReadonly(); + const admin = useAdministrator(); const userId = useSelector((state) => state.session.user?.id); - const adminRoutes = useMemo(() => [ - { subheader: t('userAdmin') }, - { name: t('settingsServer'), href: '/admin/server', icon: }, - { name: t('settingsUsers'), href: '/admin/users', icon: }, - { name: t('statisticsTitle'), href: '/admin/statistics', icon: }, + const readonlyRoutes = useMemo(() => [ + { name: t('sharedPreferences'), href: '/settings/preferences', icon: }, ], [t]); const mainRoutes = useMemo(() => [ - { name: t('sharedPreferences'), href: '/settings/preferences', icon: }, { name: t('sharedNotifications'), href: '/settings/notifications', icon: }, { name: t('settingsUser'), href: `/user/${userId}`, icon: }, { name: t('sharedGeofences'), href: '/geofences', icon: }, @@ -93,7 +91,16 @@ const OptionsLayout = ({ children }) => { { name: t('sharedSavedCommands'), href: '/settings/commands', icon: }, ], [t, userId]); - const routes = useMemo(() => [...mainRoutes, ...(admin ? adminRoutes : [])], [mainRoutes, admin, adminRoutes]); + const adminRoutes = useMemo(() => [ + { subheader: t('userAdmin') }, + { name: t('settingsServer'), href: '/admin/server', icon: }, + { name: t('settingsUsers'), href: '/admin/users', icon: }, + { name: t('statisticsTitle'), href: '/admin/statistics', icon: }, + ], [t]); + + const routes = useMemo(() => ( + [ ...readonlyRoutes, ...(!readonly ? mainRoutes : []), ...(admin ? adminRoutes : [])] + ), [readonlyRoutes, readonly, mainRoutes, admin, adminRoutes]); useEffect(() => { const activeRoute = routes.find((route) => route.href && location.pathname.includes(route.href)); -- cgit v1.2.3