From 935f49ee8a39366098bb4627a2e0aa700f762fa2 Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Tue, 13 Sep 2016 15:41:16 +0200 Subject: [PATCH] SONAR-8000 Redirect to login page if WS returns 401 (#1240) --- .../src/main/js/apps/code/actions/index.js | 19 +++++++------------ .../main/js/components/SelectList/index.js | 1 - server/sonar-web/src/main/js/helpers/l10n.js | 17 +++++++++++------ .../sonar-web/src/main/js/helpers/request.js | 6 +++++- server/sonar-web/src/main/js/libs/sonar.js | 9 +++++++++ .../sonar-web/src/main/js/main/processes.js | 1 - 6 files changed, 32 insertions(+), 21 deletions(-) diff --git a/server/sonar-web/src/main/js/apps/code/actions/index.js b/server/sonar-web/src/main/js/apps/code/actions/index.js index c765e9ed755..f8ace5adf1d 100644 --- a/server/sonar-web/src/main/js/apps/code/actions/index.js +++ b/server/sonar-web/src/main/js/apps/code/actions/index.js @@ -190,18 +190,13 @@ function requestTree (query, baseComponent, dispatch) { } async function getErrorMessage (response) { - switch (response.status) { - case 401: - return translate('not_authorized'); - default: - try { - const json = await response.json(); - return json['err_msg'] || - (json.errors && _.pluck(json.errors, 'msg').join('. ')) || - translate('default_error_message'); - } catch (e) { - return translate('default_error_message'); - } + try { + const json = await response.json(); + return json['err_msg'] || + (json.errors && _.pluck(json.errors, 'msg').join('. ')) || + translate('default_error_message'); + } catch (e) { + return translate('default_error_message'); } } diff --git a/server/sonar-web/src/main/js/components/SelectList/index.js b/server/sonar-web/src/main/js/components/SelectList/index.js index 5506d3e42a9..e99ea016146 100644 --- a/server/sonar-web/src/main/js/components/SelectList/index.js +++ b/server/sonar-web/src/main/js/components/SelectList/index.js @@ -131,7 +131,6 @@ const SelectListItemView = Backbone.View.extend({ statusCode: { // do not show global error 400: null, - 401: null, 403: null, 500: null } diff --git a/server/sonar-web/src/main/js/helpers/l10n.js b/server/sonar-web/src/main/js/helpers/l10n.js index 89954667d93..5287f6fd4f6 100644 --- a/server/sonar-web/src/main/js/helpers/l10n.js +++ b/server/sonar-web/src/main/js/helpers/l10n.js @@ -44,12 +44,17 @@ function makeRequest (params) { const url = `${window.baseUrl}/api/l10n/index?${stringify(params)}`; return fetch(url, { credentials: 'same-origin' }).then(response => { - if (response.status === 304) { - return JSON.parse(localStorage.getItem('l10n.bundle')); - } else if (response.status === 200) { - return response.json(); - } else { - throw new Error(response.status); + switch (response.status) { + case 200: + return response.json(); + case 304: + return JSON.parse(localStorage.getItem('l10n.bundle')); + case 401: + window.location = window.baseUrl + '/sessions/new?return_to=' + + encodeURIComponent(window.location.pathname + window.location.search + window.location.hash); + return {}; + default: + throw new Error('Unexpected status code: ' + response.status); } }); } diff --git a/server/sonar-web/src/main/js/helpers/request.js b/server/sonar-web/src/main/js/helpers/request.js index de67a630f8e..ac51e758f51 100644 --- a/server/sonar-web/src/main/js/helpers/request.js +++ b/server/sonar-web/src/main/js/helpers/request.js @@ -98,7 +98,11 @@ export function request (url) { * @returns {*} */ export function checkStatus (response) { - if (response.status >= 200 && response.status < 300) { + if (response.status === 401) { + window.location = window.baseUrl + '/sessions/new?return_to=' + + encodeURIComponent(window.location.pathname + window.location.search + window.location.hash); + return {}; + } else if (response.status >= 200 && response.status < 300) { return response; } else { const error = new Error(response.status); diff --git a/server/sonar-web/src/main/js/libs/sonar.js b/server/sonar-web/src/main/js/libs/sonar.js index 38e4b71e017..06f67c1a402 100644 --- a/server/sonar-web/src/main/js/libs/sonar.js +++ b/server/sonar-web/src/main/js/libs/sonar.js @@ -33,5 +33,14 @@ jQuery(function () { jQuery('.open-modal').modal(); }); +jQuery.ajaxSetup({ + statusCode: { + 401: function () { + window.location = window.baseUrl + '/sessions/new?return_to=' + + encodeURIComponent(window.location.pathname + window.location.search + window.location.hash); + } + } +}); + window.sonarqube = {}; window.sonarqube.el = '#content'; diff --git a/server/sonar-web/src/main/js/main/processes.js b/server/sonar-web/src/main/js/main/processes.js index 6c1d8b42991..a8ff4f63c69 100644 --- a/server/sonar-web/src/main/js/main/processes.js +++ b/server/sonar-web/src/main/js/main/processes.js @@ -174,7 +174,6 @@ $.ajaxSetup({ }, statusCode: { 400: handleAjaxError, - 401: handleAjaxError, 403: handleAjaxError, 500: handleAjaxError } -- 2.39.5