Some checks failed
Auto Tag Develop / tag (push) Has been cancelled
| Numéro du ticket | Titre du ticket | |------------------|-----------------| | | | ## Description de la PR ## Modification du .env ## Check list - [ ] Pas de régression - [ ] TU/TI/TF rédigée - [ ] TU/TI/TF OK - [ ] CHANGELOG modifié Reviewed-on: #52 Co-authored-by: tristan <tristan@yuno.malio.fr> Co-committed-by: tristan <tristan@yuno.malio.fr>
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)
|
|
}
|