roleUtils.ts 3.3 KB

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