summaryrefslogtreecommitdiff
path: root/src/router/index.js
diff options
context:
space:
mode:
authorDamian Celico <damianx.celico@intel.com>2022-11-24 04:00:53 +0300
committerKiran Kumar Ballapalli <kirankumarb@ami.com>2023-03-27 19:39:34 +0300
commitaeb19816e67f6d3ffb1b37c95d813a25b8be1d7b (patch)
tree0b760854f8adc45a3e909a3b1ccd88655a139705 /src/router/index.js
parent568b8a93af49d35891f042f73add850cfa651308 (diff)
downloadwebui-vue-aeb19816e67f6d3ffb1b37c95d813a25b8be1d7b.tar.xz
Added route restrictions based on user privilege
This commit allows us to add 'exclusiveToRoles' field to route config files, with the list of roles that can access this resource, if needed. In this case, only Administrator can access Virtual-Media page and SOL console, and it is blocked for other users. Signed-off-by: Sivaprabu Ganesan <sivaprabug@ami.com> Change-Id: Ibcee18bd92d97c34414ecaf2caf6af28070c5538
Diffstat (limited to 'src/router/index.js')
-rw-r--r--src/router/index.js32
1 files changed, 30 insertions, 2 deletions
diff --git a/src/router/index.js b/src/router/index.js
index 3cd52264..bcb2c7a2 100644
--- a/src/router/index.js
+++ b/src/router/index.js
@@ -8,16 +8,25 @@ import store from '../store';
import routes from './routes';
Vue.use(VueRouter);
-
const router = new VueRouter({
base: process.env.BASE_URL,
routes,
linkExactActiveClass: 'nav-link--current',
});
-router.beforeEach((to, from, next) => {
+function allowRouterToNavigate(to, next, currentUserRole) {
if (to.matched.some((record) => record.meta.requiresAuth)) {
if (store.getters['authentication/isLoggedIn']) {
+ if (to.meta.exclusiveToRoles) {
+ // The privilege for the specific router was verified using the
+ // exclusiveToRoles roles in the router.
+ if (to.meta.exclusiveToRoles.includes(currentUserRole)) {
+ next();
+ } else {
+ next('*');
+ }
+ return;
+ }
next();
return;
}
@@ -25,6 +34,25 @@ router.beforeEach((to, from, next) => {
} else {
next();
}
+}
+
+router.beforeEach((to, from, next) => {
+ let currentUserRole = store.getters['global/userPrivilege'];
+ // condition will get satisfied if user refreshed after login
+ 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);
+ }
+ });
+ } else {
+ allowRouterToNavigate(to, next, currentUserRole);
+ }
});
export default router;