From daa244ee0095981646479c0f043ada41bdb3301f Mon Sep 17 00:00:00 2001 From: RachelElysia <71795832+RachelElysia@users.noreply.github.com> Date: Tue, 25 May 2021 11:10:13 -0400 Subject: [PATCH] Nav bar: Tabs render by role (#845) * Nav tabs render based on permissionUtils global role Co-authored by: @gillespi314 --- .../SiteNavSidePanel/SiteNavSidePanel.jsx | 6 ++-- .../side_panels/SiteNavSidePanel/navItems.js | 14 ++++++-- frontend/utilities/permissions/permissions.ts | 33 +++++++++++++++++-- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/frontend/components/side_panels/SiteNavSidePanel/SiteNavSidePanel.jsx b/frontend/components/side_panels/SiteNavSidePanel/SiteNavSidePanel.jsx index 17e81c703..19189b3ea 100644 --- a/frontend/components/side_panels/SiteNavSidePanel/SiteNavSidePanel.jsx +++ b/frontend/components/side_panels/SiteNavSidePanel/SiteNavSidePanel.jsx @@ -23,11 +23,9 @@ class SiteNavSidePanel extends Component { constructor(props) { super(props); - const { - user: { global_role }, - } = this.props; + const { user: currentUser } = this.props; - this.userNavItems = navItems(global_role === "admin"); + this.userNavItems = navItems(currentUser); } renderNavItem = (navItem) => { diff --git a/frontend/components/side_panels/SiteNavSidePanel/navItems.js b/frontend/components/side_panels/SiteNavSidePanel/navItems.js index 9877cac12..60005614e 100644 --- a/frontend/components/side_panels/SiteNavSidePanel/navItems.js +++ b/frontend/components/side_panels/SiteNavSidePanel/navItems.js @@ -1,7 +1,8 @@ import PATHS from "router/paths"; import URL_PREFIX from "router/url_prefix"; +import permissionUtils from "utilities/permissions"; -export default (admin) => { +export default (currentUser) => { const adminNavItems = [ { icon: "settings", @@ -33,6 +34,9 @@ export default (admin) => { pathname: PATHS.MANAGE_QUERIES, }, }, + ]; + + const globalMaintainerNavItems = [ { icon: "packs", name: "Packs", @@ -44,8 +48,12 @@ export default (admin) => { }, ]; - if (admin) { - return [...userNavItems, ...adminNavItems]; + if (permissionUtils.isGlobalAdmin(currentUser)) { + return [...userNavItems, ...globalMaintainerNavItems, ...adminNavItems]; + } + + if (permissionUtils.isGlobalMaintainer(currentUser)) { + return [...userNavItems, ...globalMaintainerNavItems]; } return userNavItems; diff --git a/frontend/utilities/permissions/permissions.ts b/frontend/utilities/permissions/permissions.ts index 593f35c9e..91dd96a7a 100644 --- a/frontend/utilities/permissions/permissions.ts +++ b/frontend/utilities/permissions/permissions.ts @@ -17,26 +17,55 @@ const isGlobalMaintainer = (user: IUser): boolean => { return user.global_role === "maintainer"; }; +const isGlobalObserver = (user: IUser): boolean => { + return user.global_role === "observer"; +}; + const isOnGlobalTeam = (user: IUser): boolean => { return user.global_role !== null; }; -const isTeamObserver = (user: IUser, teamId: number) => { +const isTeamObserver = (user: IUser, teamId: number): boolean => { const userTeamRole = user.teams.find((team) => team.id === teamId)?.role; return userTeamRole === "observer"; }; -const isTeamMaintainer = (user: IUser, teamId: number) => { +// This checks against a specific team +const isTeamMaintainer = (user: IUser, teamId: number): boolean => { const userTeamRole = user.teams.find((team) => team.id === teamId)?.role; return userTeamRole === "maintainer"; }; +// This checks against all teams +const isAnyTeamMaintainer = (user: IUser): boolean => { + if (!isOnGlobalTeam(user)) { + return user.teams.some((team) => team?.role === "maintainer"); + } + + return false; +}; + +const isOnlyObserver = (user: IUser): boolean => { + if (isGlobalObserver(user)) { + return true; + } + + if (!isOnGlobalTeam(user)) { + return !user.teams.some((team) => team?.role === "maintainer"); + } + + return false; +}; + export default { isCoreTier, isBasicTier, isGlobalAdmin, isGlobalMaintainer, + isGlobalObserver, isOnGlobalTeam, isTeamObserver, isTeamMaintainer, + isAnyTeamMaintainer, + isOnlyObserver, };