roleUtils.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { AbilitiesType } from '~/types/interfaces'
  2. import {AnyJson} from "~/types/data";
  3. // TODO: peut-être passer ces constantes dans la config?
  4. const rolesByFunction: Array<string> = [
  5. 'ROLE_SUPER_ADMIN',
  6. 'ROLE_ADMIN',
  7. 'ROLE_ADMIN_CORE',
  8. 'ROLE_ADMINISTRATIF_MANAGER',
  9. 'ROLE_ADMINISTRATIF_MANAGER_CORE',
  10. 'ROLE_PEDAGOGICS_MANAGER',
  11. 'ROLE_PEDAGOGICS_MANAGER_CORE',
  12. 'ROLE_FINANCIAL_MANAGER',
  13. 'ROLE_FINANCIAL_MANAGER_CORE',
  14. 'ROLE_CA',
  15. 'ROLE_CA_CORE',
  16. 'ROLE_STUDENT',
  17. 'ROLE_STUDENT_CORE',
  18. 'ROLE_TEACHER',
  19. 'ROLE_TEACHER_CORE',
  20. 'ROLE_MEMBER',
  21. 'ROLE_MEMBER_CORE',
  22. 'ROLE_OTHER',
  23. 'ROLE_OTHER_CORE'
  24. ]
  25. const rolesToChange: Array<string> = [
  26. 'ROLE_GENERAL_CONFIG',
  27. 'ROLE_GENERAL_CONFIG_VIEW',
  28. 'ROLE_TAGG_ADVANCED',
  29. 'ROLE_TAGG_ADVANCED_VIEW',
  30. 'ROLE_PEDAGOGICS_ADMINISTRATION',
  31. 'ROLE_PEDAGOGICS_ADMINISTRATION_VIEW',
  32. 'ROLE_PEDAGOGICS_SEIZURE',
  33. 'ROLE_PEDAGOGICS_SEIZURE_VIEW',
  34. 'ROLE_BILLINGS_ADMINISTRATION',
  35. 'ROLE_BILLINGS_ADMINISTRATION_VIEW',
  36. 'ROLE_BILLINGS_SEIZURE',
  37. 'ROLE_BILLINGS_SEIZURE_VIEW',
  38. 'ROLE_ONLINEREGISTRATION_ADMINISTRATION',
  39. 'ROLE_ONLINEREGISTRATION_ADMINISTRATION_VIEW'
  40. ]
  41. const actionMap: AnyJson = {
  42. '': 'manage',
  43. _VIEW: 'read'
  44. }
  45. /**
  46. * Classe permettant de mener des opérations sur les rôles
  47. */
  48. class RoleUtils {
  49. /**
  50. * Teste si une personne possède un profil suivant ses rôles
  51. *
  52. * @param {string} profileName
  53. * @param {Array<string>} roles
  54. * @return {boolean}
  55. */
  56. isA (profileName: string, roles: Array<string>): boolean {
  57. profileName = profileName.toUpperCase()
  58. if (!profileName.match(/[A-Z_]+/)) {
  59. throw new Error('invalid role name')
  60. }
  61. return roles.includes('ROLE_' + profileName + '_CORE')
  62. }
  63. /**
  64. * Filtre les rôles afin d'en exclure les "Roles fonctions"
  65. *
  66. * @param {Array<string>} roles
  67. * @return {Array<string>}
  68. */
  69. filterFunctionRoles (roles: Array<string>): Array<string> {
  70. return roles.filter((role) => {
  71. return !rolesByFunction.includes(role)
  72. })
  73. }
  74. /**
  75. * Fix en attendant la migration complète, quelques rôles disposent d'underscore en trop, on corrige cela...
  76. *
  77. * @param {Array<string>} roles
  78. * @return {Array<string>}
  79. */
  80. transformUnderscoreToHyphenBeforeCompleteMigration (roles: Array<string>): Array<string> {
  81. const regex = /(ROLE_)([A-Z]*_[A-Z]*)([A-Z_]*)*/i
  82. let match
  83. roles = roles.map((role) => {
  84. if (rolesToChange.includes(role)) {
  85. if ((match = regex.exec(role)) !== null) {
  86. const role = match[1]
  87. const subject = match[2].replace('_', '-')
  88. const action = match[3]
  89. return role + subject + (action || '')
  90. }
  91. }
  92. return role
  93. })
  94. return roles
  95. }
  96. /**
  97. * On transforme les ROLES Symfony en Abilities
  98. *
  99. * @param {Array<string>} roles
  100. * @return {Array<AbilitiesType>}
  101. */
  102. transformRoleToAbilities (roles: Array<string>): [] | Array<AbilitiesType> {
  103. const abilities:Array<AbilitiesType> = []
  104. const regex = /(ROLE_)([A-Z-]*)([_A-Z]*)/i
  105. let match
  106. useEach(roles, (role) => {
  107. if ((match = regex.exec(role)) !== null) {
  108. const subject = match[2]
  109. const action = match[3]
  110. if(subject){
  111. abilities.push({
  112. action: actionMap[action],
  113. subject: subject.toLowerCase()
  114. })
  115. }
  116. }
  117. })
  118. return abilities
  119. }
  120. }
  121. export const $roleUtils = new RoleUtils()