user.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module Import
  3. module OTRS
  4. class User
  5. include Import::Helper
  6. include Import::OTRS::Helper
  7. MAPPING = {
  8. ChangeTime: :updated_at,
  9. CreateTime: :created_at,
  10. CreateBy: :created_by_id,
  11. ChangeBy: :updated_by_id,
  12. UserID: :id,
  13. Comment: :note,
  14. UserEmail: :email,
  15. UserFirstname: :firstname,
  16. UserLastname: :lastname,
  17. UserLogin: :login,
  18. }.freeze
  19. def initialize(user)
  20. import(user)
  21. end
  22. private
  23. def import(user)
  24. create_or_update(map(user))
  25. end
  26. def create_or_update(user)
  27. ensure_unique_login(user)
  28. return if updated?(user)
  29. create(user)
  30. end
  31. def updated?(user)
  32. @local_user = ::User.find_by(id: user[:id])
  33. return false if !@local_user
  34. # only update roles if different (reduce sql statements)
  35. if user[:role_ids]&.sort == @local_user.role_ids.sort
  36. user.delete(:role_ids)
  37. end
  38. log "update User.find_by(id: #{user[:id]})"
  39. @local_user.update!(user)
  40. true
  41. end
  42. def create(user)
  43. log "add User.find_by(id: #{user[:id]})"
  44. @local_user = ::User.new(user)
  45. @local_user.id = user[:id]
  46. @local_user.save
  47. reset_primary_key_sequence('users')
  48. end
  49. def ensure_unique_login(user)
  50. user[:login] = unique_login(user)
  51. end
  52. def unique_login(user)
  53. login = user[:login]
  54. return login if ::User.where('login = ? AND id != ?', login.downcase, user[:id]).count.zero?
  55. "#{login}_#{user[:id]}"
  56. end
  57. def map(user)
  58. mapped = map_default(user)
  59. mapped[:email].downcase!
  60. mapped[:login].downcase!
  61. mapped
  62. end
  63. def map_default(user)
  64. {
  65. created_by_id: 1,
  66. updated_by_id: 1,
  67. active: active?(user),
  68. source: 'OTRS Import',
  69. role_ids: role_ids(user),
  70. group_ids: group_ids(user),
  71. password: password(user),
  72. }
  73. .merge(from_mapping(user))
  74. end
  75. def password(user)
  76. return if !user['UserPw']
  77. "{sha2}#{user['UserPw']}"
  78. end
  79. def group_ids(user)
  80. result = []
  81. queues = Import::OTRS::Requester.load('Queue')
  82. queues.each do |queue|
  83. permissions = user['GroupIDs'][ queue['GroupID'] ]
  84. permissions ||= user['GroupIDs'][ queue['GroupID'].to_s ]
  85. next if !permissions
  86. next if permissions.exclude?('rw')
  87. result.push queue['QueueID']
  88. end
  89. # lookup by roles
  90. # roles of user
  91. # groups of roles
  92. # queues of group
  93. result
  94. end
  95. def role_ids(user)
  96. local_role_ids = []
  97. roles(user).each do |role|
  98. role_lookup = Role.lookup(name: role)
  99. next if !role_lookup
  100. local_role_ids.push role_lookup.id
  101. end
  102. local_role_ids
  103. end
  104. def roles(user)
  105. local_roles = ['Agent']
  106. local_roles += groups_from_otrs_groups(user)
  107. local_roles += groups_from_otrs_roles(user)
  108. local_roles.uniq
  109. end
  110. def groups_from_otrs_groups(role_object)
  111. groups = Import::OTRS::Requester.load('Group')
  112. groups_from_groups(role_object, groups)
  113. end
  114. def groups_from_groups(role_object, groups)
  115. result = []
  116. groups.each do |group|
  117. result += groups_from_otrs_group(role_object, group)
  118. end
  119. result
  120. end
  121. def groups_from_otrs_group(role_object, group)
  122. result = []
  123. return result if role_object.blank?
  124. return result if role_object['GroupIDs'].blank?
  125. permissions = role_object['GroupIDs'][ group['ID'] ]
  126. permissions ||= role_object['GroupIDs'][ group['ID'].to_s ]
  127. return result if !permissions
  128. if group['Name'] == 'admin' && permissions.include?('rw')
  129. result.push 'Admin'
  130. end
  131. return result if !group['Name'].match?(%r{^(stats|report)})
  132. return result if !(permissions.include?('ro') || permissions.include?('rw'))
  133. result.push 'Report'
  134. result
  135. end
  136. def groups_from_otrs_roles(user)
  137. result = []
  138. roles = Import::OTRS::Requester.load('Role')
  139. roles.each do |role|
  140. next if user['RoleIDs'].exclude?(role['ID'])
  141. result += groups_from_otrs_groups(role)
  142. end
  143. result
  144. end
  145. end
  146. end
  147. end