Nav bar: Tabs render by role (#845)

* Nav tabs render based on permissionUtils global role

Co-authored by: @gillespi314
This commit is contained in:
RachelElysia 2021-05-25 11:10:13 -04:00 committed by GitHub
parent 15b81824f5
commit daa244ee00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 9 deletions

View File

@ -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) => {

View File

@ -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;

View File

@ -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,
};