@quazardous/qdadm 2.39.0 → 2.40.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quazardous/qdadm",
3
- "version": "2.39.0",
3
+ "version": "2.40.0",
4
4
  "description": "Vue 3 framework for admin dashboards with PrimeVue",
5
5
  "author": "quazardous",
6
6
  "license": "MIT",
@@ -112,7 +112,11 @@ export function useNavigation(): UseNavigationReturn {
112
112
  function canAccessNavItem(item: NavItem): boolean {
113
113
  // Its own permission, on any entry (#3675, #4318): a link out has nothing
114
114
  // else to check, a route entry no longer needs `entity` to follow rights.
115
- if (item.permission && securityChecker && !securityChecker.isGranted(item.permission)) return false
115
+ // Without one, a route entry takes its route's (#4332), so the menu shows
116
+ // what the router guard lets through.
117
+ const permissions = item.permission ?? (item.route ? routePermission(item.route) : undefined)
118
+ const required = (Array.isArray(permissions) ? permissions : [permissions]).filter((p): p is string => !!p)
119
+ if (securityChecker && required.some((p) => !securityChecker.isGranted(p))) return false
116
120
  if (item.href || !item.entity || !orchestrator) return true
117
121
  const manager = orchestrator.get(item.entity)
118
122
  if (!manager) return true
@@ -123,6 +127,17 @@ export function useNavigation(): UseNavigationReturn {
123
127
  return manager.canRead()
124
128
  }
125
129
 
130
+ function routePermission(name: string): string | string[] | undefined {
131
+ try {
132
+ return router.resolve({ name }).matched.flatMap((r) => {
133
+ const p = r.meta?.permission as string | string[] | undefined
134
+ return p === undefined ? [] : Array.isArray(p) ? p : [p]
135
+ })
136
+ } catch {
137
+ return undefined
138
+ }
139
+ }
140
+
126
141
  function routeAction(name: string): string | undefined {
127
142
  try {
128
143
  return router.resolve({ name }).meta?.entityAction as string | undefined
@@ -253,21 +253,50 @@ export function applyRoutingMethods(KernelClass: { prototype: Kernel }): void {
253
253
  // allowed pass-through; a failing access check must fail CLOSED,
254
254
  // not silently allow (#1190).
255
255
  const orchestrator = this.orchestrator
256
- if (!entity || !orchestrator || !orchestrator.isRegistered(entity)) return
256
+ const entityToCheck = !!entity && !!orchestrator && orchestrator.isRegistered(entity)
257
+
258
+ // Page permissions (#4332): every matched record's meta.permission, so a
259
+ // parent's covers its children; a list needs every entry. Without a
260
+ // security checker (an app without security) nothing is checked, as for
261
+ // the menu.
262
+ const permissions = to.matched.flatMap((record: RouteRecordNormalized) => {
263
+ const p = record.meta.permission
264
+ return (Array.isArray(p) ? p : [p]).filter((x): x is string => typeof x === 'string' && x !== '')
265
+ })
266
+ const checker = this.securityChecker
267
+ const permissionsToCheck = permissions.length > 0 && !!checker
268
+
269
+ if (!entityToCheck && !permissionsToCheck) return
270
+
271
+ const checkPermissions = (): { path: string } | undefined => {
272
+ if (!permissionsToCheck) return undefined
273
+ try {
274
+ const missing = permissions.find((p) => !checker!.isGranted(p))
275
+ if (missing === undefined) return undefined
276
+ console.warn(`[qdadm] Access denied to ${to.path} (permission: ${missing})`)
277
+ orchestrator?.toast.error('Access Denied', "You don't have permission to access this page", 'Kernel')
278
+ this.signals!.emit('auth:access-denied', { path: to.path, permission: missing })
279
+ return { path: '/' }
280
+ } catch (err) {
281
+ console.error(`[qdadm] Permission check failed for ${to.path} — denying navigation:`, err)
282
+ return { path: '/' }
283
+ }
284
+ }
257
285
 
258
286
  // The action the route declares (#2497): a list route is governed by
259
287
  // `list`. A route that declares none — any custom page — checks `read`.
260
288
  const action = to.meta?.entityAction === 'list' ? 'list' : 'read'
261
289
 
262
290
  const checkEntity = (): { path: string } | undefined => {
291
+ if (!entityToCheck) return undefined
263
292
  try {
264
- const manager = orchestrator.get(entity)
293
+ const manager = orchestrator!.get(entity!)
265
294
  const allowed = manager && (action === 'list' ? manager.canList() : manager.canRead())
266
295
  if (manager && !allowed) {
267
296
  console.warn(
268
297
  `[qdadm] Access denied to ${to.path} (entity: ${entity}, action: ${action})`
269
298
  )
270
- orchestrator.toast.error(
299
+ orchestrator!.toast.error(
271
300
  'Access Denied',
272
301
  `You don't have permission to access ${manager.labelPlural || entity}`,
273
302
  'Kernel'
@@ -294,12 +323,13 @@ export function applyRoutingMethods(KernelClass: { prototype: Kernel }): void {
294
323
  // miss would fall through to an empty role matrix and read as a refusal:
295
324
  // wait for its answers instead of deciding early. Still fail closed — a
296
325
  // judge that rejects or never settles denies.
326
+ const decide = (): { path: string } | undefined => checkPermissions() ?? checkEntity()
297
327
  const judge = this.securityChecker?.grant
298
- if (typeof judge?.ready !== 'function') return checkEntity()
328
+ if (typeof judge?.ready !== 'function') return decide()
299
329
  const timeoutMs = this.options.security?.readyTimeoutMs ?? 10_000
300
- return waitForJudge(judge as { ready(): Promise<void> }, timeoutMs).then(checkEntity, (reason: Error) => {
330
+ return waitForJudge(judge as { ready(): Promise<void> }, timeoutMs).then(decide, (reason: Error) => {
301
331
  console.error(
302
- `[qdadm] security.grant never became ready for ${to.path} (entity: ${entity}): ${reason.message} — denying navigation`
332
+ `[qdadm] security.grant never became ready for ${to.path}: ${reason.message} — denying navigation`
303
333
  )
304
334
  return { path: '/' }
305
335
  })
@@ -132,6 +132,8 @@ export interface RouteOptions {
132
132
  parent?: ParentConfig
133
133
  label?: string
134
134
  layout?: string
135
+ /** Every route of the group needs it to open (#4332), unless a route declares its own `meta.permission` */
136
+ permission?: string | string[]
135
137
  }
136
138
 
137
139
  /** Parent route configuration */
@@ -50,6 +50,8 @@ export interface ModuleRouteMeta {
50
50
  navLabel?: string
51
51
  layout?: 'list' | 'form' | 'dashboard' | 'base' | string
52
52
  title?: string
53
+ /** Who may open the page (#4332): enforced by the router guard; a list needs every entry */
54
+ permission?: string | string[]
53
55
  [key: string]: unknown
54
56
  }
55
57
 
@@ -113,6 +115,8 @@ export interface AddRoutesOptions {
113
115
  parent?: ParentConfig
114
116
  label?: string
115
117
  layout?: 'list' | 'form' | 'dashboard' | 'base' | string
118
+ /** Every route of the group needs it (#4332), unless a route declares its own */
119
+ permission?: string | string[]
116
120
  }
117
121
 
118
122
  /**
@@ -192,7 +196,7 @@ const registry = {
192
196
  moduleRoutes: (ModuleRoute | RouteRecordRaw)[],
193
197
  options: AddRoutesOptions = {}
194
198
  ): void {
195
- const { entity, parent, label, layout } = options
199
+ const { entity, parent, label, layout, permission } = options
196
200
  const prefixedRoutes = moduleRoutes.map((route) => {
197
201
  const r = route as ModuleRoute
198
202
  const routePath = r.path || ''
@@ -207,6 +211,7 @@ const registry = {
207
211
  ...(label && { navLabel: label }),
208
212
  // Layout can be set per-route or inherited from options
209
213
  ...((routeLayout || layout) && { layout: routeLayout || layout }),
214
+ ...(r.meta?.permission === undefined && permission !== undefined && { permission }),
210
215
  },
211
216
  } as ModuleRoute
212
217
  })