Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Hide /:teams EditorNavMenu from demoUser role #3911

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions editor.planx.uk/src/components/EditorNavMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import TuneIcon from "@mui/icons-material/Tune";
import Box from "@mui/material/Box";
import IconButton from "@mui/material/IconButton";
import { styled } from "@mui/material/styles";
import Tooltip, { tooltipClasses, TooltipProps } from "@mui/material/Tooltip";
import Tooltip from "@mui/material/Tooltip";
import Typography from "@mui/material/Typography";
import { Role } from "@opensystemslab/planx-core/types";
import { useStore } from "pages/FlowEditor/lib/store";
Expand Down Expand Up @@ -95,13 +95,12 @@ function EditorNavMenu() {
const { navigate } = useNavigation();
const { url } = useCurrentRoute();
const isRouteLoading = useLoadingRoute();
const [teamSlug, flowSlug, user, canUserEditTeam, flowAnalyticsLink] =
const [teamSlug, flowSlug, flowAnalyticsLink, role] =
useStore((state) => [
state.teamSlug,
state.flowSlug,
state.user,
state.canUserEditTeam,
state.flowAnalyticsLink,
state.getUserRoleForCurrentTeam()
]);

const isActive = (route: string) => url.href.endsWith(route);
Expand All @@ -122,7 +121,7 @@ function EditorNavMenu() {
title: "Select a team",
Icon: FormatListBulletedIcon,
route: "/",
accessibleBy: ["platformAdmin", "teamEditor", "teamViewer"],
accessibleBy: ["platformAdmin", "teamEditor", "demoUser", "teamViewer"],
},
{
title: "Global settings",
Expand All @@ -143,7 +142,7 @@ function EditorNavMenu() {
title: "Services",
Icon: FormatListBulletedIcon,
route: `/${teamSlug}`,
accessibleBy: ["platformAdmin", "teamEditor", "teamViewer"],
accessibleBy: ["platformAdmin", "teamEditor", "demoUser", "teamViewer"],
},
{
title: "Settings",
Expand All @@ -170,25 +169,25 @@ function EditorNavMenu() {
title: "Editor",
Icon: EditorIcon,
route: `/${teamSlug}/${flowSlug}`,
accessibleBy: ["platformAdmin", "teamEditor", "teamViewer"],
accessibleBy: ["platformAdmin", "teamEditor", "demoUser", "teamViewer"],
},
{
title: "Service settings",
Icon: TuneIcon,
route: `/${teamSlug}/${flowSlug}/service`,
accessibleBy: ["platformAdmin", "teamEditor"],
accessibleBy: ["platformAdmin", "teamEditor", "demoUser"],
},
{
title: "Submissions log",
Icon: FactCheckIcon,
route: `/${teamSlug}/${flowSlug}/submissions-log`,
accessibleBy: ["platformAdmin", "teamEditor"],
accessibleBy: ["platformAdmin", "teamEditor", "demoUser"],
},
{
title: "Feedback",
Icon: RateReviewIcon,
route: `/${teamSlug}/${flowSlug}/feedback`,
accessibleBy: ["platformAdmin", "teamEditor"],
accessibleBy: ["platformAdmin", "teamEditor", "demoUser"],
},
];

Expand All @@ -198,15 +197,15 @@ function EditorNavMenu() {
title: "Analytics (external link)",
Icon: LeaderboardIcon,
route: flowAnalyticsLink,
accessibleBy: ["platformAdmin", "teamEditor"],
accessibleBy: ["platformAdmin", "teamEditor", "demoUser"],
},
]
: [
{
title: "Analytics page unavailable",
Icon: LeaderboardIcon,
route: "#",
accessibleBy: ["platformAdmin", "teamEditor"],
accessibleBy: ["platformAdmin", "teamEditor", "demoUser"],
disabled: true,
},
];
Expand Down Expand Up @@ -243,11 +242,7 @@ function EditorNavMenu() {

const { routes, compact } = getRoutesForUrl(url.href);

const visibleRoutes = routes.filter(({ accessibleBy }) => {
if (user?.isPlatformAdmin) return accessibleBy.includes("platformAdmin");
if (canUserEditTeam(teamSlug)) return accessibleBy.includes("teamEditor");
return accessibleBy.includes("teamViewer");
});
const visibleRoutes = routes.filter(({ accessibleBy }) => role && accessibleBy.includes(role));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is checking they can only see routes with "demoUser" in the accessibleBy rather than restricting the paths they shouldn't see?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that's right - this is just a visual control for the menu 👍


// Hide menu if the user does not have a selection of items
if (visibleRoutes.length < 2) return null;
Expand Down
18 changes: 16 additions & 2 deletions editor.planx.uk/src/pages/FlowEditor/lib/store/user.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { CoreDomainClient } from "@opensystemslab/planx-core";
import { Team, User, UserTeams } from "@opensystemslab/planx-core/types";
import { Role, Team, User, UserTeams } from "@opensystemslab/planx-core/types";
import axios from "axios";
import type { StateCreator } from "zustand";

import { EditorStore } from "./editor";
import { TeamStore } from "./team";

export interface UserStore {
user?: User;
Expand All @@ -13,10 +14,11 @@ export interface UserStore {
getUser: () => User | undefined;
canUserEditTeam: (teamSlug: Team["slug"]) => boolean;
initUserStore: () => Promise<void>;
getUserRoleForCurrentTeam: () => Role | undefined;
}

export const userStore: StateCreator<
UserStore & EditorStore,
UserStore & EditorStore & TeamStore,
[],
[],
UserStore
Expand Down Expand Up @@ -50,6 +52,18 @@ export const userStore: StateCreator<
const user = await getLoggedInUser();
setUser(user);
},

getUserRoleForCurrentTeam: () => {
const { user, teamSlug } = get();
if (!user || !teamSlug) return;

if (user.isPlatformAdmin) return "platformAdmin";

const currentUserTeam = user.teams.find(({ team: { slug } }) => slug === teamSlug );
if (!currentUserTeam) return;

return currentUserTeam.role;
}
});

const getLoggedInUser = async () => {
Expand Down
Loading