- 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>
39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
/**
|
|
* Hiérarchie des rôles côté front. Doit rester synchronisée avec
|
|
* `role_hierarchy` dans config/packages/security.yaml côté back.
|
|
*
|
|
* Pour ajouter un nouveau rôle :
|
|
* 1. Ajouter une entrée ici (son rôle parent dans la chaîne)
|
|
* 2. Ajouter `ROLE_X: ROLE_Y` dans security.yaml côté back
|
|
* 3. Ajouter le rôle dans `ROLE` (utils/constants.ts) pour le form admin
|
|
*/
|
|
export const ROLE_HIERARCHY: Record<string, string[]> = {
|
|
ROLE_ADMIN: ['ROLE_BUREAU'],
|
|
ROLE_BUREAU: ['ROLE_USER'],
|
|
ROLE_USER: []
|
|
}
|
|
|
|
/**
|
|
* Retourne l'ensemble des rôles effectifs en expansant la hiérarchie.
|
|
* Ex : ['ROLE_ADMIN'] → Set { 'ROLE_ADMIN', 'ROLE_BUREAU', 'ROLE_USER' }.
|
|
*/
|
|
export const expandRoles = (roles: string[]): Set<string> => {
|
|
const expanded = new Set<string>(roles)
|
|
const visit = (role: string): void => {
|
|
const parents = ROLE_HIERARCHY[role] ?? []
|
|
for (const parent of parents) {
|
|
if (!expanded.has(parent)) {
|
|
expanded.add(parent)
|
|
visit(parent)
|
|
}
|
|
}
|
|
}
|
|
for (const r of roles) visit(r)
|
|
return expanded
|
|
}
|
|
|
|
export const userHasRole = (userRoles: string[] | null | undefined, role: string): boolean => {
|
|
if (!userRoles || userRoles.length === 0) return false
|
|
return expandRoles(userRoles).has(role)
|
|
}
|