From bceafface3899539006e8f04717e7fd5bf491ac5 Mon Sep 17 00:00:00 2001 From: Paul Fertser Date: Wed, 10 Apr 2024 16:27:53 +0000 Subject: Deduplicate and simplify RoleId handling To improve UX for users of accounts with restricted permissions the frontend determines the current RoleId. Knowing that it can hide menus and inhibit transitions that are not allowed by the backend in any case. This patch unifies the handling by moving processing of the API reply containing RoleId in the single place, right where `authentication/getUserInfo` store gets it. This makes the program flow easier to understand and change if needed without worrying of where another copy of the code might be and how it would need to be amended. No functional change. Tested: logging in and out, navigating the pages, getting an error message when wrong credentials are used, reloading the page with an established session. All while observing Network and Console tabs in Web Developer tools, no unexpected API requests are made and no unexpected errors reported. Confirmed in debugger that the retrieved role gets stored and used for routing restrictions. Change-Id: Ia8782f44cb6bf813954d30b8bf3a620a626ad455 Signed-off-by: Paul Fertser --- src/router/index.js | 10 +++------- src/store/modules/Authentication/AuthenticanStore.js | 7 +++++-- src/views/Login/Login.vue | 5 +---- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/router/index.js b/src/router/index.js index bcb2c7a2..5b6d9099 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -42,13 +42,9 @@ router.beforeEach((to, from, next) => { if (!currentUserRole && store.getters['authentication/isLoggedIn']) { // invoke API call to get the role ID let username = localStorage.getItem('storedUsername'); - store.dispatch('authentication/getUserInfo', username).then((response) => { - if (response?.RoleId) { - // set role ID - store.commit('global/setPrivilege', response.RoleId); - // allow the route to continue - allowRouterToNavigate(to, next, response.RoleId); - } + store.dispatch('authentication/getUserInfo', username).then(() => { + let currentUserRole = store.getters['global/userPrivilege']; + allowRouterToNavigate(to, next, currentUserRole); }); } else { allowRouterToNavigate(to, next, currentUserRole); diff --git a/src/store/modules/Authentication/AuthenticanStore.js b/src/store/modules/Authentication/AuthenticanStore.js index 0dca1832..57270159 100644 --- a/src/store/modules/Authentication/AuthenticanStore.js +++ b/src/store/modules/Authentication/AuthenticanStore.js @@ -61,10 +61,13 @@ const AuthenticationStore = { .then(() => router.push('/login')) .catch((error) => console.log(error)); }, - getUserInfo(_, username) { + getUserInfo({ commit }, username) { return api .get(`/redfish/v1/AccountService/Accounts/${username}`) - .then(({ data }) => data) + .then(({ data }) => { + commit('global/setPrivilege', data.RoleId, { root: true }); + return data; + }) .catch((error) => console.log(error)); }, resetStoreState({ state }) { diff --git a/src/views/Login/Login.vue b/src/views/Login/Login.vue index 96b4c9e8..db475c56 100644 --- a/src/views/Login/Login.vue +++ b/src/views/Login/Login.vue @@ -124,15 +124,12 @@ export default { this.$store.commit('global/setLanguagePreference', i18n.locale); return this.$store.dispatch('authentication/getUserInfo', username); }) - .then(({ PasswordChangeRequired, RoleId }) => { + .then(({ PasswordChangeRequired }) => { if (PasswordChangeRequired) { this.$router.push('/change-password'); } else { this.$router.push('/'); } - if (RoleId) { - this.$store.commit('global/setPrivilege', RoleId); - } }) .catch((error) => console.log(error)) .finally(() => (this.disableSubmitButton = false)); -- cgit v1.2.3