From 555c7dd62475cac4e96bfa9ffbed6dd523ea8ad1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gr=C3=A9goire=20Aubert?= Date: Thu, 11 May 2017 12:24:44 +0200 Subject: [PATCH] SONAR-8822 Show the administration menu only to authorized users --- .../main/js/app/components/AdminContainer.js | 15 ++-- .../components/nav/global/GlobalNavMenu.js | 3 +- .../global/__tests__/GlobalNavMenu-test.js | 18 ++++- .../__snapshots__/GlobalNavMenu-test.js.snap | 74 +++++++++++++++++++ .../organizations/UserOrganizations.js | 10 +-- server/sonar-web/src/main/js/helpers/users.js | 27 ------- 6 files changed, 103 insertions(+), 44 deletions(-) delete mode 100644 server/sonar-web/src/main/js/helpers/users.js diff --git a/server/sonar-web/src/main/js/app/components/AdminContainer.js b/server/sonar-web/src/main/js/app/components/AdminContainer.js index da8c9376dbc..86d867a2c5e 100644 --- a/server/sonar-web/src/main/js/app/components/AdminContainer.js +++ b/server/sonar-web/src/main/js/app/components/AdminContainer.js @@ -21,8 +21,7 @@ import React from 'react'; import Helmet from 'react-helmet'; import { connect } from 'react-redux'; import SettingsNav from './nav/settings/SettingsNav'; -import { getCurrentUser, getAppState } from '../../store/rootReducer'; -import { isUserAdmin } from '../../helpers/users'; +import { getAppState } from '../../store/rootReducer'; import { onFail } from '../../store/rootActions'; import { getSettingsNavigation } from '../../api/nav'; import { setAdminPages } from '../../store/appState/duck'; @@ -30,7 +29,7 @@ import { translate } from '../../helpers/l10n'; class AdminContainer extends React.PureComponent { componentDidMount() { - if (!isUserAdmin(this.props.currentUser)) { + if (!this.props.appState.canAdmin) { // workaround cyclic dependencies const handleRequiredAuthorization = require('../utils/handleRequiredAuthorization').default; handleRequiredAuthorization(); @@ -46,14 +45,17 @@ class AdminContainer extends React.PureComponent { } render() { - if (!isUserAdmin(this.props.currentUser) || !this.props.adminPages) { + const { adminPages } = this.props.appState; + + // Check that the adminPages are loaded + if (!adminPages) { return null; } return (
- + {this.props.children}
); @@ -61,8 +63,7 @@ class AdminContainer extends React.PureComponent { } const mapStateToProps = state => ({ - adminPages: getAppState(state).adminPages, - currentUser: getCurrentUser(state) + appState: getAppState(state) }); const mapDispatchToProps = { setAdminPages }; diff --git a/server/sonar-web/src/main/js/app/components/nav/global/GlobalNavMenu.js b/server/sonar-web/src/main/js/app/components/nav/global/GlobalNavMenu.js index 1cedcf94211..0a88701f045 100644 --- a/server/sonar-web/src/main/js/app/components/nav/global/GlobalNavMenu.js +++ b/server/sonar-web/src/main/js/app/components/nav/global/GlobalNavMenu.js @@ -20,7 +20,6 @@ import React from 'react'; import { Link } from 'react-router'; import { translate } from '../../../../helpers/l10n'; -import { isUserAdmin } from '../../../../helpers/users'; import { isMySet } from '../../../../apps/issues/utils'; export default class GlobalNavMenu extends React.PureComponent { @@ -106,7 +105,7 @@ export default class GlobalNavMenu extends React.PureComponent { } renderAdministrationLink() { - if (!isUserAdmin(this.props.currentUser)) { + if (!this.props.appState.canAdmin) { return null; } return ( diff --git a/server/sonar-web/src/main/js/app/components/nav/global/__tests__/GlobalNavMenu-test.js b/server/sonar-web/src/main/js/app/components/nav/global/__tests__/GlobalNavMenu-test.js index 99b97c82c4f..33546be3731 100644 --- a/server/sonar-web/src/main/js/app/components/nav/global/__tests__/GlobalNavMenu-test.js +++ b/server/sonar-web/src/main/js/app/components/nav/global/__tests__/GlobalNavMenu-test.js @@ -27,8 +27,22 @@ it('should work with extensions', () => { qualifiers: ['TRK'] }; const currentUser = { - isLoggedIn: false, - permissions: { global: [] } + isLoggedIn: false + }; + const wrapper = shallow( + + ); + expect(wrapper).toMatchSnapshot(); +}); + +it('should show administration menu if the user has the rights', () => { + const appState = { + canAdmin: true, + globalPages: [], + qualifiers: ['TRK'] + }; + const currentUser = { + isLoggedIn: false }; const wrapper = shallow( diff --git a/server/sonar-web/src/main/js/app/components/nav/global/__tests__/__snapshots__/GlobalNavMenu-test.js.snap b/server/sonar-web/src/main/js/app/components/nav/global/__tests__/__snapshots__/GlobalNavMenu-test.js.snap index bc69019f7a0..bb21d44b23e 100644 --- a/server/sonar-web/src/main/js/app/components/nav/global/__tests__/__snapshots__/GlobalNavMenu-test.js.snap +++ b/server/sonar-web/src/main/js/app/components/nav/global/__tests__/__snapshots__/GlobalNavMenu-test.js.snap @@ -1,5 +1,79 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`should show administration menu if the user has the rights 1`] = ` +
    +
  • + + projects.page + +
  • +
  • + + issues.page + +
  • +
  • + + coding_rules.page + +
  • +
  • + + quality_profiles.page + +
  • +
  • + + quality_gates.page + +
  • +
  • + + layout.settings + +
  • +
+`; + exports[`should work with extensions 1`] = `
    , organizations: Array, fetchIfAnyoneCanCreateOrganizations: () => Promise<*>, @@ -65,8 +64,7 @@ class UserOrganizations extends React.PureComponent { const anyoneCanCreate = this.props.anyoneCanCreate != null && this.props.anyoneCanCreate.value === 'true'; - const canCreateOrganizations = - !this.state.loading && (anyoneCanCreate || isUserAdmin(this.props.currentUser)); + const canCreateOrganizations = !this.state.loading && (anyoneCanCreate || this.props.canAdmin); return (
    @@ -101,7 +99,7 @@ class UserOrganizations extends React.PureComponent { const mapStateToProps = state => ({ anyoneCanCreate: getSettingValue(state, 'sonar.organizations.anyoneCanCreate'), - currentUser: getCurrentUser(state), + canAdmin: getAppState(state).canAdmin, organizations: getMyOrganizations(state) }); diff --git a/server/sonar-web/src/main/js/helpers/users.js b/server/sonar-web/src/main/js/helpers/users.js deleted file mode 100644 index cebcbf556b4..00000000000 --- a/server/sonar-web/src/main/js/helpers/users.js +++ /dev/null @@ -1,27 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -// @flow -type User = { - permissions: { - global: Array - } -}; - -export const isUserAdmin = (user: User): boolean => user.permissions.global.includes('admin'); -- 2.39.5