From aeb19816e67f6d3ffb1b37c95d813a25b8be1d7b Mon Sep 17 00:00:00 2001
From: Damian Celico <damianx.celico@intel.com>
Date: Thu, 24 Nov 2022 02:00:53 +0100
Subject: 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
---
 src/components/AppNavigation/AppNavigation.vue | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

(limited to 'src/components/AppNavigation/AppNavigation.vue')

diff --git a/src/components/AppNavigation/AppNavigation.vue b/src/components/AppNavigation/AppNavigation.vue
index acfabe76..a5f81051 100644
--- a/src/components/AppNavigation/AppNavigation.vue
+++ b/src/components/AppNavigation/AppNavigation.vue
@@ -29,7 +29,7 @@
               <b-collapse :id="navItem.id" tag="ul" class="nav-item__nav">
                 <li class="nav-item">
                   <router-link
-                    v-for="(subNavItem, i) of navItem.children"
+                    v-for="(subNavItem, i) of filteredNavItem(navItem.children)"
                     :key="i"
                     :to="subNavItem.route"
                     :data-test-id="`nav-item-${subNavItem.id}`"
@@ -67,6 +67,7 @@ export default {
   data() {
     return {
       isNavigationOpen: false,
+      currentUserRole: null,
     };
   },
   watch: {
@@ -78,12 +79,24 @@ export default {
     },
   },
   mounted() {
+    this.getPrivilege();
     this.$root.$on('toggle-navigation', () => this.toggleIsOpen());
   },
   methods: {
     toggleIsOpen() {
       this.isNavigationOpen = !this.isNavigationOpen;
     },
+    getPrivilege() {
+      this.currentUserRole = this.$store?.getters['global/userPrivilege'];
+    },
+    filteredNavItem(navItem) {
+      if (this.currentUserRole) {
+        return navItem.filter(({ exclusiveToRoles }) => {
+          if (!exclusiveToRoles?.length) return true;
+          return exclusiveToRoles.includes(this.currentUserRole);
+        });
+      } else return navItem;
+    },
   },
 };
 </script>
-- 
cgit v1.2.3

