Files
Ferme/frontend/stores/auth.ts
tristan 52925f7896 feat : ajout du rôle ROLE_BUREAU et hiérarchie de rôles
- Hiérarchie Symfony : ROLE_ADMIN -> ROLE_BUREAU -> ROLE_USER
- ROLE_BUREAU autorise : export inventaire bovin, sync EDNOTIF, visibilité des colonnes Prix/kg et Prix total
- Front : utility roles.ts qui réplique la hiérarchie + auth store étendu (hasRole, isBureau)
- Refactor useBovineColumns : variants withPrices/withoutPrices × inventory/case, gate par isBureau
- Inventory page : Export/Rafraîchir conditionnés par isBureau
- Ajout du rôle dans la const ROLE pour le form admin user

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-28 12:18:44 +02:00

83 lines
2.5 KiB
TypeScript

import {defineStore} from 'pinia'
import type {UserData} from '~/services/dto/user-data'
import {getCurrentUser, createUser, updateUser, login, logout} from '~/services/auth'
import type {UserPayload} from "~/services/dto/user-data";
import {userHasRole} from '~/utils/roles'
export const useAuthStore = defineStore('auth', {
state: () => ({
user: null as UserData | null,
isLoading: false,
checked: false
}),
getters: {
isAuthenticated: (state) => Boolean(state.user),
hasRole: (state) => (role: string): boolean => userHasRole(state.user?.roles, role),
isAdmin: (state) => userHasRole(state.user?.roles, 'ROLE_ADMIN'),
isBureau: (state) => userHasRole(state.user?.roles, 'ROLE_BUREAU')
},
actions: {
clearSession() {
this.user = null
this.checked = true
this.isLoading = false
},
async ensureSession() {
if (this.checked) {
return this.user
}
this.checked = true
try {
const me = await getCurrentUser()
this.user = me
return me
} catch {
this.user = null
return null
}
},
async login(username: string, password: string) {
this.isLoading = true
try {
await login(username, password)
const me = await getCurrentUser()
this.user = me
this.checked = true
return me
} finally {
this.isLoading = false
}
},
async createUser(payload: UserPayload = {}) {
this.isLoading = true
const result = await createUser(payload).finally(() => {
this.isLoading = false
})
return result
},
async updateUser(id: number, payload: UserPayload) {
this.isLoading = true
const result = await updateUser(id, payload).finally(() => {
this.isLoading = false
})
return result
},
async logout() {
this.isLoading = true
try {
await logout()
} catch {
// Ignore logout errors so we can still clear local auth state.
} finally {
this.user = null
this.checked = true
this.isLoading = false
}
},
}
})