Repository navigation
refactor: enhance permission checks for application access based on user authentication - #8199
Conversation
…ser authentication
| permission: ({ params }) => perm.application.workspace.accessRead(String(params.applicationId)) && (auth.isPE || auth.isEE), | ||
| next: 'workspace-application-chat-user', | ||
| }, | ||
| }, |
There was a problem hiding this comment.
Here is the analysis of the provided code snippet, focusing on correctness, architecture, and potential issues.
1. Critical Issue: Module-Level Store Access
Problem: Calling useStore() and destructuring auth at the module level (top-level scope) is highly likely to cause errors or undefined behavior in Vue applications, especially with Pinia or Vuex.
-
Why it’s bad:
- Stores rely on the Vue application instance to be created and initialized. At import time, this may not have happened yet.
- If this routes file is imported before the Pinia store is active,
useStore()will throw an error ([🍍] getActivePinia() has not been called). - Even if it doesn't crash immediately, the reference to
authcaptured here might be stale or not reactively linked correctly within the context of router hooks (which often run outside component setup context).
-
Fix: Do not call
useStore()at the module level. Instead, access the store inside the function where it's needed, ensuring the app context exists.
2. Security & Logic Flaw: Client-Side Permission Check
Problem: The line:
permission: ({ params }) => perm.application.workspace.accessRead(...) && (auth.isPE || auth.isEE)is performing a permission check in the route configuration (which runs on the client/browser).
- Risk: This is easily bypassable by modifying local JavaScript. It provides no real security. Users can inspect the code, change
auth.isPEto true, or directly navigate to the route if no redirect/interceptor blocks them properly. - Best Practice: Route permissions should only be used for navigation guards (like hiding menus or disabling links). The actual data fetching and actions must always be validated on the server side. Ensure the backend API also checks if the user is PE/EE.
3. Optimization Suggestion: Lazy Store Access
To fix Issue #1 while keeping the logic intact, move the store usage inside the arrow function.
❌ Current (Bad):
import { useStore } from '@/stores'
const { auth } = useStore() // ⚠️ CRASH RISK: Called too early
export const applicationRoutes: RouteRecordRaw[] = [
// ...
permission: ({ params }) => perm.application.workspace.accessRead(String(params.applicationId)) && (auth.isPE || auth.isEE),
// ...
]✅ Recommended Fix:
import { useStore } from '@/stores' // Keep import but don't call useStore() here
// Inside the route definition object:
permission: ({ params }) => {
// Get fresh store instance inside function call (when route is evaluated)
const { auth } = useStore()
const hasAccessRead = perm.application.workspace.accessRead(String(params.applicationId))
const isAuthorizedRole = auth.isPE || auth.isEE
return hasAccessRead && isAuthorizedRole
},4. Code Style & Maintainability Suggestions
A. Readability of Ternary/Logical Conditions
The current condition is a bit dense. For better maintainability, split it into variables as shown in the "Recommended Fix" above.
B. Type Safety
Ensure that auth.isPE and auth.isEE are boolean-typed getters. If they are computed properties, accessing them via auth.isPE in the Pinia store is correct. If using Vuex, map state/mapGetters differently. Assuming Pinia is used due to useStore() syntax, this is fine.
C. Naming Convention
perm.application.workspace.accessRead(...): This appears to be a complex enum/string-based permission system. Ensure thataccessReadclearly maps to backend permissions. Consider simplifying names if possible (e.g.,perm.workspace.read).
5. Final Improved Code Example
import type { RouteRecordRaw } from 'vue-router'
import { APPLICATION_TYPE } from '@/api/enums'
import { perm } from '@/permission'
import { useStore } from '@/stores' // Import hook, do not call it yet
export const applicationRoutes: RouteRecordRaw[] = [
{
path: 'application',
// ... other properties
// Inside one of the child routes:
children: [
{
// ...
meta: {
// Option: Move permission check to meta if your router guard uses it
// Or keep it in the route config as shown below
}
}
]
},
// Note: Adjust structure based on nested routing.
// Assuming the 'permission' property belongs to a route/meta object in your custom guard logic:
{
path: ':applicationId/workspace/chat', // example path
name: 'workspace-application-chat',
component: () => import('@/views/...'),
meta: {
order: 30,
icon: 'icon_application_outlined',
activeIcon: 'icon_application_filled',
permission: ({ params }: { params: Record<string, string> }) => {
// Safe access: called during navigation, when Pinia is ready
const { auth } = useStore()
const hasReadAccess = perm.application.workspace.accessRead(String(params.applicationId))
const isInRole = auth.isPE || auth.isEE
return hasReadAccess && isInRole
}
},
next: 'workspace-application-chat-user' // Remove if not valid vue-router prop
}
]
⚠️ Note onnext:: In standard Vue Router v3/v4,nextis not a valid route record option. If you're using a custom navigation library that wraps Vue Router, ensurenextis handled by your wrapper. If this is standard Vue Router, removenext: '...'— redirection should be done via router guards (beforeEnter, or globalrouter.beforeEach).
refactor: enhance permission checks for application access based on user authentication