roleUtils.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import * as _ from 'lodash'
  2. import { AbilitiesType, AnyJson } from '~/types/interfaces'
  3. const roles_by_function: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 roles_to_change: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 action_map: AnyJson = {
  41. '': 'manage',
  42. _VIEW: 'read'
  43. }
  44. /**
  45. * @category Services/droits
  46. * @class RoleUtils
  47. * Classe permettant de mener des opérations sur les roles
  48. */
  49. class RoleUtils {
  50. /**
  51. * Test si une personne possède un profil suivant ses roles
  52. * @param {string} profil_name
  53. * @param {Array<string>} roles
  54. * @return {boolean}
  55. */
  56. isA (profil_name:string, roles:Array<string>): boolean {
  57. return roles.includes('ROLE_' + profil_name + '_CORE')
  58. }
  59. /**
  60. * Filtre les roles afin d'en exclure les "Roles fonctions"
  61. * @param {Array<string>} roles
  62. * @return {Array<string>}
  63. */
  64. filterFunctionRoles (roles:Array<string>):Array<string> {
  65. return roles.filter((role) => {
  66. return !roles_by_function.includes(role)
  67. })
  68. }
  69. /**
  70. * Avant la migration complète, quelque role disposent d'underscore en trop, on corrige cela...
  71. * @param {Array<string>} roles
  72. * @return {Array<string>}
  73. */
  74. transformUnderscoreToHyphenBeforeCompleteMigration (roles: Array<string>): Array<string> {
  75. const regex = /(ROLE_)([A-Z]*_[A-Z]*)([A-Z_]*)*/i
  76. let match
  77. roles = roles.map((role) => {
  78. if (roles_to_change.includes(role)) {
  79. if ((match = regex.exec(role)) !== null) {
  80. const role = match[1]
  81. const subject = match[2].replace('_', '-')
  82. const action = match[3]
  83. return role + subject + (action || '')
  84. }
  85. }
  86. return role
  87. })
  88. return roles
  89. }
  90. /**
  91. * On transforme les ROLES Symfony en Abilities
  92. * @param {Array<string>} roles
  93. * @return {Array<AbilitiesType>}
  94. */
  95. transformRoleToAbilities (roles: Array<string>): [] | Array<AbilitiesType> {
  96. const abilities:Array<AbilitiesType> = []
  97. const regex = /(ROLE_)([A-Z-]*)([_A-Z]*)/i
  98. let match
  99. _.each(roles, (role) => {
  100. if ((match = regex.exec(role)) !== null) {
  101. const subject = match[2]
  102. const action = match[3]
  103. abilities.push({
  104. action: action_map[action],
  105. subject: subject.toLowerCase()
  106. })
  107. }
  108. })
  109. return abilities
  110. }
  111. }
  112. export const $roleUtils = new RoleUtils()