20230912080534_group_hierarchy.rb 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class GroupHierarchy < ActiveRecord::Migration[6.1]
  3. def change
  4. # return if it's a new setup
  5. return if !Setting.exists?(name: 'system_init_done')
  6. rename_custom_object_attributes
  7. migrate_groups_table
  8. migrate_group_name
  9. update_name_last_column
  10. migrate_object_attributes
  11. change_group_field
  12. add_core_workflow_parent_id
  13. add_core_workflow_group_ids
  14. migrate_settings
  15. end
  16. def rename_custom_object_attributes
  17. MigrationHelper.rename_custom_object_attribute('Group', 'name_last')
  18. MigrationHelper.rename_custom_object_attribute('Group', 'parent_id')
  19. end
  20. def migrate_groups_table
  21. change_column :groups, :name, :string, limit: (160 * 6) + (2 * 5) # max depth of 6 and 5 delimiters inbetween
  22. add_column :groups, :name_last, :string, limit: 160, null: true # optional for now
  23. add_column :groups, :parent_id, :integer, null: true
  24. add_foreign_key :groups, :groups, column: :parent_id
  25. Group.reset_column_information
  26. end
  27. def migrate_group_name
  28. Group.all.each do |group|
  29. if group.name.exclude?('::')
  30. group.update!(name_last: group.name, updated_by_id: 1)
  31. next
  32. end
  33. try = 1
  34. new_name = group.name
  35. loop do
  36. new_name = group.name.gsub(%r{::}, '-' * try)
  37. break if !Group.exists?(name: new_name)
  38. try += 1
  39. end
  40. group.update!(name: new_name, name_last: new_name, updated_by_id: 1)
  41. end
  42. end
  43. def update_name_last_column
  44. change_column :groups, :name_last, :string, limit: 160, null: false
  45. Group.reset_column_information
  46. end
  47. def migrate_object_attributes
  48. name_attribute = ObjectManager::Attribute.for_object('Group').find_by(name: 'name')
  49. name_attribute.data_option = { type: 'text', maxlength: 255, readonly: 1 }
  50. name_attribute.screens = {}
  51. name_attribute.save!
  52. ObjectManager::Attribute.add(
  53. force: true,
  54. object: 'Group',
  55. name: 'name_last',
  56. display: 'Name',
  57. data_type: 'input',
  58. data_option: {
  59. type: 'text',
  60. maxlength: 150,
  61. null: false,
  62. },
  63. editable: false,
  64. active: true,
  65. screens: {
  66. create: {
  67. '-all-' => {
  68. null: false,
  69. },
  70. },
  71. edit: {
  72. '-all-' => {
  73. null: false,
  74. },
  75. },
  76. view: {
  77. '-all-' => {
  78. shown: true,
  79. },
  80. },
  81. },
  82. to_create: false,
  83. to_migrate: false,
  84. to_delete: false,
  85. position: 210,
  86. created_by_id: 1,
  87. updated_by_id: 1,
  88. )
  89. ObjectManager::Attribute.add(
  90. force: true,
  91. object: 'Group',
  92. name: 'parent_id',
  93. display: 'Parent group',
  94. data_type: 'tree_select',
  95. data_option: {
  96. default: '',
  97. null: true,
  98. relation: 'Group',
  99. nulloption: true,
  100. do_not_log: true,
  101. },
  102. editable: false,
  103. active: true,
  104. screens: {
  105. create: {
  106. '-all-' => {
  107. null: true,
  108. },
  109. },
  110. edit: {
  111. '-all-' => {
  112. null: true,
  113. },
  114. },
  115. },
  116. to_create: false,
  117. to_migrate: false,
  118. to_delete: false,
  119. position: 250,
  120. created_by_id: 1,
  121. updated_by_id: 1,
  122. )
  123. ObjectManager::Attribute.add(
  124. force: true,
  125. object: 'User',
  126. name: 'group_ids',
  127. display: 'Group permissions',
  128. data_type: 'group_permissions',
  129. data_option: {
  130. null: false,
  131. item_class: 'checkbox',
  132. permission: ['admin.user'],
  133. },
  134. editable: false,
  135. active: true,
  136. screens: {
  137. signup: {},
  138. invite_agent: {
  139. '-all-' => {
  140. null: false,
  141. },
  142. },
  143. invite_customer: {},
  144. edit: {
  145. '-all-' => {
  146. null: true,
  147. },
  148. },
  149. create: {
  150. '-all-' => {
  151. null: true,
  152. },
  153. },
  154. view: {
  155. '-all-' => {
  156. shown: false,
  157. },
  158. },
  159. },
  160. to_create: false,
  161. to_migrate: false,
  162. to_delete: false,
  163. position: 1700,
  164. created_by_id: 1,
  165. updated_by_id: 1,
  166. )
  167. ObjectManager::Attribute
  168. .get(
  169. object: 'User',
  170. name: 'role_ids',
  171. )
  172. .update!(display: 'Roles')
  173. end
  174. def change_group_field
  175. ObjectManager::Attribute.for_object('Ticket').find_by(name: 'group_id').update(data_type: 'tree_select')
  176. end
  177. def add_core_workflow_parent_id
  178. CoreWorkflow.create_if_not_exists(
  179. name: 'base - remove current and child groups from parent id',
  180. object: 'Group',
  181. condition_saved: {
  182. 'custom.module': {
  183. operator: 'match all modules',
  184. value: [
  185. 'CoreWorkflow::Custom::AdminGroupParentId',
  186. ],
  187. },
  188. },
  189. perform: {
  190. 'custom.module': {
  191. execute: ['CoreWorkflow::Custom::AdminGroupParentId']
  192. },
  193. },
  194. changeable: false,
  195. created_by_id: 1,
  196. updated_by_id: 1,
  197. )
  198. end
  199. def add_core_workflow_group_ids
  200. CoreWorkflow.create_if_not_exists(
  201. name: 'base - show group list for agents',
  202. condition_saved: {
  203. 'custom.module': {
  204. operator: 'match all modules',
  205. value: [
  206. 'CoreWorkflow::Custom::AdminShowGroupListForAgents',
  207. ],
  208. },
  209. },
  210. perform: {
  211. 'custom.module': {
  212. execute: ['CoreWorkflow::Custom::AdminShowGroupListForAgents']
  213. },
  214. },
  215. changeable: false,
  216. created_by_id: 1,
  217. updated_by_id: 1,
  218. )
  219. end
  220. def migrate_settings
  221. setting = Setting.find_by(name: 'customer_ticket_create_group_ids')
  222. return if setting.blank?
  223. setting.options['form'][0]['tag'] = 'tree_select'
  224. setting.save!
  225. end
  226. end