hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBc35a83aBurak Boduroğlu4 months ago440 commits
| 1 | import type { Role } from "@/api/client"; |
| 2 | |
| 3 | const RANK: Record<Role, number> = { |
| 4 | parent: -1, |
| 5 | student: 0, |
| 6 | teacher: 1, |
| 7 | manager: 2, |
| 8 | admin: 3, |
| 9 | }; |
| 10 | |
| 11 | export function hasMinRole(role: Role | undefined | null, min: Role): boolean { |
| 12 | if (!role) return false; |
| 13 | return RANK[role] >= RANK[min]; |
| 14 | } |
| 15 | |
| 16 | export function hasMaxRole(role: Role | undefined | null, max: Role): boolean { |
| 17 | if (!role) return false; |
| 18 | return RANK[role] <= RANK[max]; |
| 19 | } |
| 20 | |
| 21 | export function hasExactRole(role: Role | undefined | null, exact: Role): boolean { |
| 22 | return role === exact; |
| 23 | } |
| 24 | |
| 25 | /** Inclusive role band: minRole <= role <= maxRole. */ |
| 26 | export function roleInRange( |
| 27 | role: Role | undefined | null, |
| 28 | min?: Role, |
| 29 | max?: Role, |
| 30 | ): boolean { |
| 31 | if (!role) return false; |
| 32 | if (min && !hasMinRole(role, min)) return false; |
| 33 | if (max && !hasMaxRole(role, max)) return false; |
| 34 | return true; |
| 35 | } |
| 36 | |
| 37 | export const ROLES: Role[] = ["student", "parent", "teacher", "manager", "admin"]; |