Skip to content

Commit

Permalink
Deduplicate and simplify RoleId handling
Browse files Browse the repository at this point in the history
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 <fercerpav@gmail.com>
  • Loading branch information
paulfertser authored and Gunnar Mills committed Apr 25, 2024
1 parent 51abe87 commit bceaffa
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
10 changes: 3 additions & 7 deletions src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions src/store/modules/Authentication/AuthenticanStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand Down
5 changes: 1 addition & 4 deletions src/views/Login/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit bceaffa

Please sign in to comment.