Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | 193x 193x 149x 149x 149x 11x 11x 1696x 281x 1696x 193x 951x 951x 951x 484x 951x 951x 1595x 951x | import { computed } from 'vue'
import { useFlowsStore, type FlowMembership } from '@/stores/flows'
import { useAuthStore } from '@/stores/auth'
/**
* Checks whether a granted permission (which may contain wildcards) covers a required permission.
* Mirrors backend matchesPermission logic.
*/
function matchesPermission(granted: string, required: string): boolean {
Iif (!granted || !required) return false
if (granted === required) return true
const pattern = granted.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*')
try {
return new RegExp(`^${pattern}$`).test(required)
} catch {
return false
}
}
/**
* Whether `granted` reaches a restricted entity: only `*:*` and a permission
* naming the entity's id do — never `entities-*:…` nor a flat shorthand.
* Mirrors the backend's isExplicitEntityGrant (UC-ENTITY-007.2 AC-3).
*/
function reachesRestrictedEntity(granted: string, required: string): boolean {
const entityId = /^entities-([^:*]+):/.exec(required)?.[1]
return !entityId || granted === '*:*' || granted.startsWith(`entities-${entityId}:`)
}
/** The bit of an entity a permission check needs. */
export interface PermissionTarget {
visibility?: string
}
/**
* Plain (non-reactive) permission check against a flow membership, for use
* outside component setup where Vue's `computed` shouldn't be created (e.g.
* one-off background fetches). Flow owners always return true.
* Pass the entity an entity-scoped permission is about, so a restricted
* entity ignores wildcards like the backend does.
*/
export function hasFlowPermission(
membership: FlowMembership | undefined,
required: string,
entity?: PermissionTarget | null
): boolean {
if (membership?.isOwner) return true
const restricted = entity?.visibility === 'restricted'
return (membership?.permissions ?? []).some(
(granted) =>
matchesPermission(granted, required) &&
(!restricted || reachesRestrictedEntity(granted, required))
)
}
/**
* Provides permission checking for the current active flow.
* Mirrors the wildcard matching logic from the backend PermissionChecker.
*
* Permissions come straight from the membership returned by GET /v1/auth/my-flows,
* where the backend already resolves the user's roleIds into an effective permission
* set. This avoids depending on `roles:list` here: a member who can create roles,
* entities, items or API keys but can't list roles must still see those actions.
*/
export function usePermissions() {
const flowsStore = useFlowsStore()
const authStore = useAuthStore()
const currentMembership = computed(() =>
flowsStore.myFlows.find((f) => f.flowId === authStore.currentFlow)
)
const isOwner = computed(() => currentMembership.value?.isOwner ?? false)
const userPermissions = computed((): string[] => {
if (isOwner.value) return ['*:*']
return currentMembership.value?.permissions ?? []
})
/**
* Returns true if the current user has the required permission in the active flow.
* Flow owners always return true. See hasFlowPermission for `entity`.
*/
function hasPermission(required: string, entity?: PermissionTarget | null): boolean {
return hasFlowPermission(currentMembership.value, required, entity)
}
return { hasPermission, isOwner, userPermissions }
}
|