Kaynağa Gözat

fix: only allow staff to add products at their own store
refactor: router guard if-else

Nivek 2 yıl önce
ebeveyn
işleme
c6518e231d
3 değiştirilmiş dosya ile 61 ekleme ve 20 silme
  1. 54 19
      src/router/index.ts
  2. 2 1
      src/views/store/StoreDetail.vue
  3. 5 0
      src/views/user/Login.vue

+ 54 - 19
src/router/index.ts

@@ -97,34 +97,69 @@ const router = createRouter({
 
 router.beforeEach((to, _, next) => {
     const token:string|null = sessionStorage.getItem('token');
-    const role:string|null = sessionStorage.getItem('role')
+    const role:string|null = sessionStorage.getItem('role');
+    const storeId:string|null = sessionStorage.getItem('storeId');
 
     if (to.meta.title) {
         document.title = to.meta.title;
     }
 
-    if (token) {
-        if(to.meta.permission) {
-            if(to.meta.permission.includes(role!)) {
-                next();
-            }
-            else {
-                next('/404');
-            }
-        } else {
-            next()
-        }
-    } else {
-        if(to.path === '/login') {
-            next();
-        }
-        else if(to.path === '/register') {
+    // if (token) {
+    //     if(to.meta.permission) {
+    //         if(to.meta.permission.includes(role!)) {
+    //             if (to.name === 'createProduct') {
+    //                 if (to.params.storeId === storeId) {
+    //                     next();
+    //                 } else {
+    //                     next('/404')
+    //                 }
+    //             } else {
+    //                 next();
+    //             }
+    //         }
+    //         else {
+    //             next('/404');
+    //         }
+    //     } else {
+    //         next()
+    //     }
+    // } else {
+    //     if(to.path === '/login') {
+    //         next();
+    //     }
+    //     else if(to.path === '/register') {
+    //         next();
+    //     }
+    //     else {
+    //         next('/login');
+    //     }
+    // }
+
+    if (!token) {
+        if (to.path === '/login' || to.path === '/register') {
             next();
-        }
-        else {
+        } else {
             next('/login');
         }
+        return;
     }
+
+    if (!to.meta.permission) {
+        next();
+        return;
+    }
+
+    if (!to.meta.permission.includes(role!)) {
+        next('/404');
+        return;
+    }
+
+    if (to.name === 'createProduct' && to.params.storeId !== storeId) {
+        next('/404');
+        return;
+    }
+
+
 });
 
 

+ 2 - 1
src/views/store/StoreDetail.vue

@@ -9,6 +9,7 @@ import ProductItem from "../../components/ProductItem.vue"
 const role = sessionStorage.getItem("role")
 // 从当前router地址中获得storeId参数值
 const storeId = router.currentRoute.value.params.storeId
+const actualStoreId = sessionStorage.getItem("storeId")
 const storeVO = ref()
 const name = ref('')
 const logoUrl = ref('')
@@ -77,7 +78,7 @@ function toBackPage() {
         </el-descriptions>
 
         <el-button class="create-product-button" type="primary" plain
-                   @click="toCreateProductPage()" v-if="role === 'STAFF' || role === 'MANAGER'">新增商品
+                   @click="toCreateProductPage()" v-if="role === 'STAFF' && storeId === actualStoreId">新增商品
         </el-button>
       </div>
     </el-aside>

+ 5 - 0
src/views/user/Login.vue

@@ -41,6 +41,11 @@ function handleLogin() {
       userInfo().then(res => {
         sessionStorage.setItem('name', res.data.result.name)
         sessionStorage.setItem('role', res.data.result.role)
+
+        if (res.data.result.role === 'STAFF' ) {
+          sessionStorage.setItem('storeId', res.data.result.storeId)
+        }
+
         router.push({path: "/allStore"})
       })
     } else if (res.data.code === '400') {