roleUtils.ts 3.3 KB

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