only show entries in navigaton that user is allowed to

This commit is contained in:
Maren Süwer
2018-10-05 13:48:21 +02:00
parent a5f4d9713e
commit ec7a73bbc4
4 changed files with 109 additions and 45 deletions

View File

@@ -2,40 +2,62 @@
import React from "react"; import React from "react";
import { translate } from "react-i18next"; import { translate } from "react-i18next";
import PrimaryNavigationLink from "./PrimaryNavigationLink"; import PrimaryNavigationLink from "./PrimaryNavigationLink";
import type {Link} from "@scm-manager/ui-types";
type Props = { type Props = {
t: string => string t: string => string,
repositoriesLink: Link,
usersLink: Link,
groupsLink: Link,
configLink: Link,
logoutLink: Link
}; };
class PrimaryNavigation extends React.Component<Props> { class PrimaryNavigation extends React.Component<Props> {
render() { render() {
const { t } = this.props; const { t, repositoriesLink, usersLink, groupsLink, configLink, logoutLink } = this.props;
return (
<nav className="tabs is-boxed"> const _repositoriesLink = repositoriesLink ? (
<ul>
<PrimaryNavigationLink <PrimaryNavigationLink
to="/repos" to="/repos"
match="/(repo|repos)" match="/(repo|repos)"
label={t("primary-navigation.repositories")} label={t("primary-navigation.repositories")}
/> />): null;
const _usersLink = usersLink ? (
<PrimaryNavigationLink <PrimaryNavigationLink
to="/users" to="/users"
match="/(user|users)" match="/(user|users)"
label={t("primary-navigation.users")} label={t("primary-navigation.users")}
/> />) : null;
const _groupsLink = groupsLink ? (
<PrimaryNavigationLink <PrimaryNavigationLink
to="/groups" to="/groups"
match="/(group|groups)" match="/(group|groups)"
label={t("primary-navigation.groups")} label={t("primary-navigation.groups")}
/> />) : null;
const _configLink = configLink ? (
<PrimaryNavigationLink <PrimaryNavigationLink
to="/config" to="/config"
label={t("primary-navigation.config")} label={t("primary-navigation.config")}
/> />) : null;
const _logoutLink = logoutLink ? (
<PrimaryNavigationLink <PrimaryNavigationLink
to="/logout" to="/logout"
label={t("primary-navigation.logout")} label={t("primary-navigation.logout")}
/> />) : null;
return (
<nav className="tabs is-boxed">
<ul>
{_repositoriesLink}
{_usersLink}
{_groupsLink}
{_configLink}
{_logoutLink}
</ul> </ul>
</nav> </nav>
); );

View File

@@ -19,10 +19,15 @@ import {
Footer, Footer,
Header Header
} from "@scm-manager/ui-components"; } from "@scm-manager/ui-components";
import type { Me, IndexResources } from "@scm-manager/ui-types"; import type { Me, Link } from "@scm-manager/ui-types";
import { import {
fetchIndexResources, fetchIndexResources,
getConfigLink,
getFetchIndexResourcesFailure, getFetchIndexResourcesFailure,
getGroupsLink,
getLogoutLink,
getRepositoriesLink,
getUsersLink,
isFetchIndexResourcesPending isFetchIndexResourcesPending
} from "../modules/indexResource"; } from "../modules/indexResource";
@@ -31,7 +36,11 @@ type Props = {
authenticated: boolean, authenticated: boolean,
error: Error, error: Error,
loading: boolean, loading: boolean,
indexResources: IndexResources, repositoriesLink: Link,
usersLink: Link,
groupsLink: Link,
configLink: Link,
logoutLink: Link,
// dispatcher functions // dispatcher functions
fetchMe: () => void, fetchMe: () => void,
@@ -48,10 +57,31 @@ class App extends Component<Props> {
} }
render() { render() {
const { me, loading, error, authenticated, t } = this.props; const {
me,
loading,
error,
authenticated,
t,
repositoriesLink,
usersLink,
groupsLink,
configLink,
logoutLink
} = this.props;
let content; let content;
const navigation = authenticated ? <PrimaryNavigation /> : ""; const navigation = authenticated ? (
<PrimaryNavigation
repositoriesLink={repositoriesLink}
usersLink={usersLink}
groupsLink={groupsLink}
configLink={configLink}
logoutLink={logoutLink}
/>
) : (
""
);
if (loading) { if (loading) {
content = <Loading />; content = <Loading />;
@@ -90,11 +120,21 @@ const mapStateToProps = state => {
isFetchMePending(state) || isFetchIndexResourcesPending(state); isFetchMePending(state) || isFetchIndexResourcesPending(state);
const error = const error =
getFetchMeFailure(state) || getFetchIndexResourcesFailure(state); 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 { return {
authenticated, authenticated,
me, me,
loading, loading,
error error,
repositoriesLink,
usersLink,
groupsLink,
configLink,
logoutLink
}; };
}; };

View File

@@ -86,70 +86,72 @@ export function getFetchIndexResourcesFailure(state: Object) {
return getFailure(state, FETCH_INDEXRESOURCES); return getFailure(state, FETCH_INDEXRESOURCES);
} }
export function getLinks(state: Object){ export function getLinks(state: Object) {
return state.indexResources.links; return state.indexResources.links;
} }
export function getUiPluginsLink(state: Object) { export function getUiPluginsLink(state: Object) {
if (state.indexResources.links && state.indexResources.links["uiPlugins"])
return state.indexResources.links["uiPlugins"].href; return state.indexResources.links["uiPlugins"].href;
return undefined;
} }
export function getMeLink(state: Object) { 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 state.indexResources.links["me"].href;
return undefined; return undefined;
} }
export function getLogoutLink(state: Object) { 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 state.indexResources.links["logout"].href;
return undefined; return undefined;
} }
export function getLoginLink(state: Object) { 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 state.indexResources.links["login"].href;
return undefined; return undefined;
} }
export function getUsersLink(state: Object) { 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 state.indexResources.links["users"].href;
return undefined; return undefined;
} }
export function getGroupsLink(state: Object) { 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 state.indexResources.links["groups"].href;
return undefined; return undefined;
} }
export function getConfigLink(state: Object) { 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 state.indexResources.links["config"].href;
return undefined; return undefined;
} }
export function getRepositoriesLink(state: Object) { 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 state.indexResources.links["repositories"].href;
return undefined; return undefined;
} }
export function getHgConfigLink(state: Object) { 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 state.indexResources.links["hgConfig"].href;
return undefined; return undefined;
} }
export function getGitConfigLink(state: Object) { 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 state.indexResources.links["gitConfig"].href;
return undefined; return undefined;
} }
export function getSvnConfigLink(state: Object) { 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 state.indexResources.links["svnConfig"].href;
return undefined; return undefined;
} }

View File

@@ -20,7 +20,7 @@ import reducer, {
getHgConfigLink, getHgConfigLink,
getGitConfigLink, getGitConfigLink,
getSvnConfigLink, getSvnConfigLink,
getLinks getLinks, getGroupsLink
} from "./indexResource"; } from "./indexResource";
const indexResourcesUnauthenticated = { const indexResourcesUnauthenticated = {
@@ -307,7 +307,7 @@ describe("index resources selectors", () => {
links: indexResourcesAuthenticated._links 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", () => { 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 links: indexResourcesUnauthenticated._links
} }
}; };
expect(getUsersLink(state)).toBe(undefined); expect(getGroupsLink(state)).toBe(undefined);
}); });
// config link // config link