From ec7a73bbc4f92cf0952d85390847b89c5ca14f43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maren=20S=C3=BCwer?= Date: Fri, 5 Oct 2018 13:48:21 +0200 Subject: [PATCH] only show entries in navigaton that user is allowed to --- .../src/navigation/PrimaryNavigation.js | 72 ++++++++++++------- scm-ui/src/containers/App.js | 50 +++++++++++-- scm-ui/src/modules/indexResource.js | 26 +++---- scm-ui/src/modules/indexResource.test.js | 6 +- 4 files changed, 109 insertions(+), 45 deletions(-) diff --git a/scm-ui-components/packages/ui-components/src/navigation/PrimaryNavigation.js b/scm-ui-components/packages/ui-components/src/navigation/PrimaryNavigation.js index dc81c4a4fc..bb79c3d2b7 100644 --- a/scm-ui-components/packages/ui-components/src/navigation/PrimaryNavigation.js +++ b/scm-ui-components/packages/ui-components/src/navigation/PrimaryNavigation.js @@ -2,40 +2,62 @@ import React from "react"; import { translate } from "react-i18next"; import PrimaryNavigationLink from "./PrimaryNavigationLink"; +import type {Link} from "@scm-manager/ui-types"; type Props = { - t: string => string + t: string => string, + repositoriesLink: Link, + usersLink: Link, + groupsLink: Link, + configLink: Link, + logoutLink: Link }; class PrimaryNavigation extends React.Component { render() { - const { t } = this.props; + const { t, repositoriesLink, usersLink, groupsLink, configLink, logoutLink } = this.props; + + const _repositoriesLink = repositoriesLink ? ( + ): null; + + const _usersLink = usersLink ? ( + ) : null; + + const _groupsLink = groupsLink ? ( + ) : null; + + const _configLink = configLink ? ( + ) : null; + + const _logoutLink = logoutLink ? ( + ) : null; + return ( ); diff --git a/scm-ui/src/containers/App.js b/scm-ui/src/containers/App.js index 86de8bb90f..3155e06117 100644 --- a/scm-ui/src/containers/App.js +++ b/scm-ui/src/containers/App.js @@ -19,10 +19,15 @@ import { Footer, Header } from "@scm-manager/ui-components"; -import type { Me, IndexResources } from "@scm-manager/ui-types"; +import type { Me, Link } from "@scm-manager/ui-types"; import { fetchIndexResources, + getConfigLink, getFetchIndexResourcesFailure, + getGroupsLink, + getLogoutLink, + getRepositoriesLink, + getUsersLink, isFetchIndexResourcesPending } from "../modules/indexResource"; @@ -31,7 +36,11 @@ type Props = { authenticated: boolean, error: Error, loading: boolean, - indexResources: IndexResources, + repositoriesLink: Link, + usersLink: Link, + groupsLink: Link, + configLink: Link, + logoutLink: Link, // dispatcher functions fetchMe: () => void, @@ -48,10 +57,31 @@ class App extends Component { } render() { - const { me, loading, error, authenticated, t } = this.props; + const { + me, + loading, + error, + authenticated, + t, + repositoriesLink, + usersLink, + groupsLink, + configLink, + logoutLink + } = this.props; let content; - const navigation = authenticated ? : ""; + const navigation = authenticated ? ( + + ) : ( + "" + ); if (loading) { content = ; @@ -90,11 +120,21 @@ const mapStateToProps = state => { isFetchMePending(state) || isFetchIndexResourcesPending(state); const error = getFetchMeFailure(state) || getFetchIndexResourcesFailure(state); + const repositoriesLink = getRepositoriesLink(state); + const usersLink = getUsersLink(state); + const groupsLink = getGroupsLink(state); + const configLink = getConfigLink(state); + const logoutLink = getLogoutLink(state); return { authenticated, me, loading, - error + error, + repositoriesLink, + usersLink, + groupsLink, + configLink, + logoutLink }; }; diff --git a/scm-ui/src/modules/indexResource.js b/scm-ui/src/modules/indexResource.js index e9bdcc452a..c511c4668e 100644 --- a/scm-ui/src/modules/indexResource.js +++ b/scm-ui/src/modules/indexResource.js @@ -86,70 +86,72 @@ export function getFetchIndexResourcesFailure(state: Object) { return getFailure(state, FETCH_INDEXRESOURCES); } -export function getLinks(state: Object){ +export function getLinks(state: Object) { return state.indexResources.links; } export function getUiPluginsLink(state: Object) { - return state.indexResources.links["uiPlugins"].href; + if (state.indexResources.links && state.indexResources.links["uiPlugins"]) + return state.indexResources.links["uiPlugins"].href; + return undefined; } export function getMeLink(state: Object) { - if (state.indexResources.links["me"]) + if (state.indexResources.links && state.indexResources.links["me"]) return state.indexResources.links["me"].href; return undefined; } export function getLogoutLink(state: Object) { - if (state.indexResources.links["logout"]) + if (state.indexResources.links && state.indexResources.links["logout"]) return state.indexResources.links["logout"].href; return undefined; } export function getLoginLink(state: Object) { - if (state.indexResources.links["login"]) + if (state.indexResources.links && state.indexResources.links["login"]) return state.indexResources.links["login"].href; return undefined; } export function getUsersLink(state: Object) { - if (state.indexResources.links["users"]) + if (state.indexResources.links && state.indexResources.links["users"]) return state.indexResources.links["users"].href; return undefined; } export function getGroupsLink(state: Object) { - if (state.indexResources.links["groups"]) + if (state.indexResources.links && state.indexResources.links["groups"]) return state.indexResources.links["groups"].href; return undefined; } export function getConfigLink(state: Object) { - if (state.indexResources.links["config"]) + if (state.indexResources.links && state.indexResources.links["config"]) return state.indexResources.links["config"].href; return undefined; } export function getRepositoriesLink(state: Object) { - if (state.indexResources.links["repositories"]) + if (state.indexResources.links && state.indexResources.links["repositories"]) return state.indexResources.links["repositories"].href; return undefined; } export function getHgConfigLink(state: Object) { - if (state.indexResources.links["hgConfig"]) + if (state.indexResources.links && state.indexResources.links["hgConfig"]) return state.indexResources.links["hgConfig"].href; return undefined; } export function getGitConfigLink(state: Object) { - if (state.indexResources.links["gitConfig"]) + if (state.indexResources.links && state.indexResources.links["gitConfig"]) return state.indexResources.links["gitConfig"].href; return undefined; } export function getSvnConfigLink(state: Object) { - if (state.indexResources.links["svnConfig"]) + if (state.indexResources.links && state.indexResources.links["svnConfig"]) return state.indexResources.links["svnConfig"].href; return undefined; } diff --git a/scm-ui/src/modules/indexResource.test.js b/scm-ui/src/modules/indexResource.test.js index 956fe0c33f..2199da8290 100644 --- a/scm-ui/src/modules/indexResource.test.js +++ b/scm-ui/src/modules/indexResource.test.js @@ -20,7 +20,7 @@ import reducer, { getHgConfigLink, getGitConfigLink, getSvnConfigLink, - getLinks + getLinks, getGroupsLink } from "./indexResource"; const indexResourcesUnauthenticated = { @@ -307,7 +307,7 @@ describe("index resources selectors", () => { links: indexResourcesAuthenticated._links } }; - expect(getUsersLink(state)).toBe("http://localhost:8081/scm/api/v2/users/"); + expect(getGroupsLink(state)).toBe("http://localhost:8081/scm/api/v2/groups/"); }); it("should return undefined for groups link when unauthenticated or has not permission to see it", () => { @@ -316,7 +316,7 @@ describe("index resources selectors", () => { links: indexResourcesUnauthenticated._links } }; - expect(getUsersLink(state)).toBe(undefined); + expect(getGroupsLink(state)).toBe(undefined); }); // config link