user.rb 4.5 KB

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