Files
Inventory_frontend/app/composables/useProfiles.ts
Matthieu cc70fe2b29 feat(permissions) : add role-based UI guards and readonly mode for viewers
- Add usePermissions composable (isAdmin, canEdit, canView)
- Password-protected profile login with modal on profiles page
- Disable all form fields for ROLE_VIEWER across edit/create pages
- Show navigation buttons (Modifier/Consulter) for all roles, hide delete for viewers
- Add readonly prop to ModelTypeForm for category pages
- Disable modal fields (sites, constructeurs) for viewers
- Guard /admin routes in middleware

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-26 13:36:42 +01:00

50 lines
1.3 KiB
TypeScript

import { useState, useRuntimeConfig } from '#imports'
export interface Profile {
id: string
firstName: string
lastName: string
email?: string | null
isActive?: boolean
hasPassword?: boolean
roles?: string[]
[key: string]: unknown
}
const buildUrl = (path: string): string => {
const config = useRuntimeConfig()
const base = (config.public.apiBaseUrl as string)?.replace(/\/$/, '') || ''
return `${base}${path}`
}
export function useProfiles() {
const profiles = useState<Profile[]>('profiles:list', () => [])
const loadingProfiles = useState<boolean>('profiles:loading', () => false)
const profilesLoaded = useState<boolean>('profiles:loaded', () => false)
const fetchProfiles = async (): Promise<Profile[]> => {
loadingProfiles.value = true
try {
profiles.value = await $fetch<Profile[]>(buildUrl('/session/profiles'), {
method: 'GET',
credentials: 'include',
})
profilesLoaded.value = true
} catch (error) {
console.error('Erreur lors du chargement des profils', error)
profiles.value = []
profilesLoaded.value = false
} finally {
loadingProfiles.value = false
}
return profiles.value
}
return {
profiles,
loadingProfiles,
profilesLoaded,
fetchProfiles,
}
}